/** * Draws a polygon * *<code> * $coordinates = array( array( 'x' => 4, 'y' => 6 ), array( 'x' => 8, 'y' => 10 ) ); * $image->polygon($coordinates); *</code> * * @param array $coordinates array of x and y * @param string $color * @return Phalcon\Image\Adapter\GD */ PHP_METHOD(Phalcon_Image_Adapter_GD, polygon){ zval *coordinates, *color = NULL, image = {}, rgb = {}, r = {}, g = {}, b = {}, imagecolor = {}, *point, points = {}, num_points = {}; phalcon_fetch_params(0, 1, 1, &coordinates, &color); if (!color) { color = &PHALCON_GLOBAL(z_null); } if (!phalcon_fast_count_ev(coordinates)) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates must be not empty"); return; } phalcon_return_property(&image, getThis(), SL("_image")); if (Z_TYPE(image) == IS_RESOURCE) { PHALCON_CALL_METHODW(&rgb, getThis(), "getcolorrbg", color); phalcon_array_fetch_long(&r, &rgb, 0, PH_NOISY); phalcon_array_fetch_long(&g, &rgb, 1, PH_NOISY); phalcon_array_fetch_long(&b, &rgb, 2, PH_NOISY); PHALCON_CALL_FUNCTIONW(&imagecolor, "imagecolorallocate", &image, &r, &g, &b); array_init(&points); ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(coordinates), point) { zval x = {}, y = {}; if (Z_TYPE_P(point) == IS_ARRAY) { if (phalcon_fast_count_int(point) != 2) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates point error"); return; } if (!phalcon_array_isset_fetch_long(&x, point, 0)) { phalcon_array_fetch_str(&x, point, SL("x"), PH_NOISY); } if (!phalcon_array_isset_fetch_long(&y, point, 0)) { phalcon_array_fetch_str(&y, point, SL("y"), PH_NOISY); } phalcon_array_append(&points, &x, PH_COPY); phalcon_array_append(&points, &y, PH_COPY); } else { phalcon_array_append(&points, &_p->val, PH_COPY); _p++; phalcon_array_append(&points, &_p->val, PH_COPY); } phalcon_increment(&num_points); } ZEND_HASH_FOREACH_END();
/** * Checks if GD is enabled * * @return boolean */ PHP_METHOD(Phalcon_Image_Adapter_GD, check){ zval gd_version = {}, ret = {}, gd_info = {}, version = {}, exception_message = {}, pattern = {}, matches = {}; if (phalcon_function_exists_ex(SL("gd_info")) == FAILURE) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "GD is either not installed or not enabled, check your configuration"); return; } if (!phalcon_get_constant(&gd_version, SL("GD_VERSION"))) { PHALCON_CALL_FUNCTIONW(&gd_info, "gd_info"); if (phalcon_array_isset_fetch_str(&gd_version, &gd_info, SL("GD Version"))) { ZVAL_STRING(&pattern, "#\\d+\\.\\d+(?:\\.\\d+)?#"); ZVAL_NULL(&matches); ZVAL_MAKE_REF(&matches); RETURN_ON_FAILURE(phalcon_preg_match(&ret, &pattern, &gd_version, &matches)); ZVAL_UNREF(&matches); if (zend_is_true(&ret)) { if (!phalcon_array_isset_fetch_long(&version, &matches, 0)) { ZVAL_EMPTY_STRING(&version); } } else { ZVAL_EMPTY_STRING(&version); } } else { PHALCON_CPY_WRT_CTOR(&version, &gd_version); } } if (-1 == php_version_compare(Z_STRVAL_P(&gd_version), "2.0.1")) { PHALCON_CONCAT_SV(&exception_message, "Phalcon\\Image\\Adapter\\GD requires GD version '2.0.1' or greater, you have '", &gd_version); PHALCON_THROW_EXCEPTION_ZVALW(phalcon_image_exception_ce, &exception_message); return; } phalcon_update_static_property_ce(phalcon_image_adapter_gd_ce, SL("_checked"), &PHALCON_GLOBAL(z_true)); RETURN_TRUE; }
/** * Magic method to get or set services using setters/getters * * @param string $method * @param array $arguments * @return mixed */ PHP_METHOD(Phalcon_DI, __call){ zval *method, *arguments = NULL, possible_service = {}, name = {}, definition = {}; phalcon_fetch_params(0, 1, 1, &method, &arguments); PHALCON_ENSURE_IS_STRING(method); if (!arguments) { arguments = &PHALCON_GLOBAL(z_null); } phalcon_substr(&possible_service, method, 3, 0); phalcon_lcfirst(&name, &possible_service); /** * If the magic method starts with "get" we try to get a service with that name */ if (phalcon_start_with_str(method, SL("get"))) { if (phalcon_isset_property_array(getThis(), SL("_services"), &name)) { PHALCON_RETURN_CALL_SELFW("get", &name, arguments); return; } } /** * If the magic method starts with "set" we try to set a service using that name */ if (phalcon_start_with_str(method, SL("set"))) { if (phalcon_array_isset_fetch_long(&definition, arguments, 0)) { PHALCON_CALL_SELFW(NULL, "set", &name, &definition); return; } } /** * The method doesn't start with set/get throw an exception */ zend_throw_exception_ex(phalcon_di_exception_ce, 0, "Call to undefined method or service '%s'", Z_STRVAL_P(method)); }
/** * Gets the type of PHQL queries * * * @return int */ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder_Where, setConditions){ zval *conditions, *bind_params = NULL, *bind_types = NULL, *type = NULL, merge = {}, merged_conditions = {}, merged_bind_params = {}, merged_bind_types; zval joind_condition = {}, *single_condition_array = NULL, current_conditions = {}, new_conditions = {}; zend_string *str_key; ulong idx; phalcon_fetch_params(0, 1, 3, &conditions, &bind_params, &bind_types, &type); if (!bind_params) { bind_params = &PHALCON_GLOBAL(z_null); } if (!bind_types) { bind_types = &PHALCON_GLOBAL(z_null); } if (!type) { type = &PHALCON_GLOBAL(z_null); } ZVAL_BOOL(&merge, Z_TYPE_P(type) != IS_NULL ? 1 : 0); if (Z_TYPE_P(conditions) == IS_ARRAY) { /* ----------- INITIALIZING LOOP VARIABLES ----------- */ /* * array containing single condition for example: * array( * array( * 'status = :status:', * array('status' => 5), * array('status' => PDO::PARAM_INT), * ), * 'name' => 'Dreamsxin', * ) */ array_init(&merged_conditions); array_init(&merged_bind_params); array_init(&merged_bind_types); ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(conditions), idx, str_key, single_condition_array) { zval single_condition_key = {}, condition_string = {}, tmp_bind_params = {}, tmp_bind_types = {}; if (str_key) { ZVAL_STR(&single_condition_key, str_key); } else { ZVAL_LONG(&single_condition_key, idx); } if (Z_TYPE_P(single_condition_array) == IS_ARRAY && phalcon_array_isset_fetch_long(&condition_string, single_condition_array, 0) && phalcon_array_isset_fetch_long(&tmp_bind_params, single_condition_array, 1) && Z_TYPE(condition_string) == IS_STRING && Z_TYPE(tmp_bind_params) == IS_ARRAY ) { phalcon_array_update_zval(&merged_conditions, &condition_string, &condition_string, PH_COPY); phalcon_array_merge_recursive_n(&merged_bind_params, &tmp_bind_params); if (phalcon_array_isset_fetch_long(&tmp_bind_types, single_condition_array, 2) && Z_TYPE(tmp_bind_types) == IS_ARRAY) { phalcon_array_merge_recursive_n(&merged_bind_types, &tmp_bind_types); } } else if (Z_TYPE(single_condition_key) == IS_STRING) { PHALCON_CONCAT_VSVS(&condition_string, &single_condition_key, " = :", &single_condition_key, ":"); phalcon_array_update_zval(&merged_conditions, &single_condition_key, &condition_string, PH_COPY); if (Z_TYPE_P(single_condition_array) == IS_ARRAY) { phalcon_array_merge_recursive_n(&merged_bind_params, single_condition_array); } else { phalcon_array_update_zval(&merged_bind_params, &single_condition_key, single_condition_array, PH_COPY); } } } ZEND_HASH_FOREACH_END();
/** * Executes the validation * * @param string $file * @param int $minsize * @param int $maxsize * @param array $mimes * @param int $minwidth * @param int $maxwidth * @param int $minheight * @param int $maxheight * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_File, valid) { zval *value, *minsize = NULL, *maxsize = NULL, *mimes = NULL, *minwidth = NULL, *maxwidth = NULL, *minheight = NULL, *maxheight = NULL; zval file = {}, size = {}, *constant, finfo = {}, pathname = {}, mime = {}, image = {}, imageinfo = {}, width = {}, height = {}, valid = {}; zend_class_entry *imagick_ce; phalcon_fetch_params(0, 1, 7, &value, &minsize, &maxsize, &mimes, &minwidth, &maxwidth, &minheight, &maxheight); if (Z_TYPE_P(value) == IS_STRING) { object_init_ex(&file, spl_ce_SplFileInfo); if (phalcon_has_constructor(&file)) { PHALCON_CALL_METHODW(NULL, &file, "__construct", value); } } else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function_ex(Z_OBJCE_P(value), spl_ce_SplFileInfo, 0)) { PHALCON_CPY_WRT(&file, value); } if (Z_TYPE(file) <= IS_NULL) { phalcon_update_property_str(getThis(), SL("_type"), SL("TypeUnknow")); RETURN_FALSE; } if (!minsize) { minsize = &PHALCON_GLOBAL(z_null); } if (!maxsize) { maxsize = &PHALCON_GLOBAL(z_null); } if (!mimes) { mimes = &PHALCON_GLOBAL(z_null); } if (!minwidth) { minwidth = &PHALCON_GLOBAL(z_null); } if (!maxwidth) { maxwidth = &PHALCON_GLOBAL(z_null); } if (!minheight) { minheight = &PHALCON_GLOBAL(z_null); } if (!maxheight) { maxheight = &PHALCON_GLOBAL(z_null); } PHALCON_CALL_METHODW(&valid, &file, "isfile"); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("FileValid")); RETURN_FALSE; } PHALCON_CALL_METHODW(&size, &file, "getsize"); if (!PHALCON_IS_EMPTY(minsize)) { is_smaller_or_equal_function(&valid, minsize, &size); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("TooSmall")); RETURN_FALSE; } } if (!PHALCON_IS_EMPTY(maxsize)) { is_smaller_or_equal_function(&valid, &size, maxsize); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("TooLarge")); RETURN_FALSE; } } PHALCON_CALL_METHODW(&pathname, &file, "getpathname"); if (Z_TYPE_P(mimes) == IS_ARRAY) { if ((constant = zend_get_constant_str(SL("FILEINFO_MIME_TYPE"))) == NULL) { PHALCON_THROW_EXCEPTION_STRW(phalcon_validation_exception_ce, "Undefined constant `FILEINFO_MIME_TYPE`"); return; } PHALCON_CALL_FUNCTIONW(&finfo, "finfo_open", constant); if (Z_TYPE(finfo) != IS_RESOURCE) { PHALCON_THROW_EXCEPTION_STRW(phalcon_validation_exception_ce, "Opening fileinfo database failed"); return; } PHALCON_CALL_FUNCTIONW(&mime, "finfo_file", &finfo, &pathname); PHALCON_CALL_FUNCTIONW(NULL, "finfo_close", &finfo); if (!phalcon_fast_in_array(&mime, mimes)) { phalcon_update_property_str(getThis(), SL("_type"), SL("MimeValid")); RETURN_FALSE; } } if (phalcon_class_str_exists(SL("imagick"), 0) != NULL) { imagick_ce = phalcon_fetch_str_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO); object_init_ex(&image, imagick_ce); PHALCON_CALL_METHODW(NULL, &image, "__construct", &pathname); PHALCON_CALL_METHODW(&width, &image, "getImageWidth"); PHALCON_CALL_METHODW(&height, &image, "getImageHeight"); } else if (phalcon_function_exists_ex(SL("getimagesize")) != FAILURE) { PHALCON_CALL_FUNCTIONW(&imageinfo, "getimagesize", &pathname); if (!phalcon_array_isset_fetch_long(&width, &imageinfo, 0)) { ZVAL_LONG(&width, -1); } if (!phalcon_array_isset_fetch_long(&height, &imageinfo, 1)) { ZVAL_LONG(&height, -1); } } else { ZVAL_LONG(&width, -1); ZVAL_LONG(&height, -1); } if (!PHALCON_IS_EMPTY(minwidth)) { is_smaller_or_equal_function(&valid, minwidth, &width); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("TooNarrow")); RETURN_FALSE; } } if (!PHALCON_IS_EMPTY(maxwidth)) { is_smaller_or_equal_function(&valid, &width, maxwidth); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("TooWide")); RETURN_FALSE; } } if (!PHALCON_IS_EMPTY(minheight)) { is_smaller_or_equal_function(&valid, minheight, &height); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("TooShort")); RETURN_FALSE; } } if (!PHALCON_IS_EMPTY(maxheight)) { is_smaller_or_equal_function(&valid, &height, maxheight); if (!zend_is_true(&valid)) { phalcon_update_property_str(getThis(), SL("_type"), SL("TooLong")); RETURN_FALSE; } } RETURN_TRUE; }
/** * Execute a text * * @param string text * @param int $offset_x * @param int $offset_y * @param int $opacity * @param int $r * @param int $g * @param int $b * @param int $size * @param string $fontfile */ PHP_METHOD(Phalcon_Image_Adapter_GD, _text) { zval *text, *offset_x, *offset_y, *opacity, *r, *g, *b, *size, *fontfile = NULL, image = {}, image_width = {}; zval image_height = {}, tmp = {}, space = {}, s0 = {}, s1 = {}, s4 = {}, s5 = {}, width = {}, height = {}, color = {}; int w, h, w1, h1, x, y, i; phalcon_fetch_params(0, 9, 0, &text, &offset_x, &offset_y, &opacity, &r, &g, &b, &size, &fontfile); PHALCON_SEPARATE_PARAM(offset_x); PHALCON_SEPARATE_PARAM(offset_y); PHALCON_SEPARATE_PARAM(opacity); phalcon_return_property(&image, getThis(), SL("_image")); phalcon_return_property(&image_width, getThis(), SL("_width")); phalcon_return_property(&image_height, getThis(), SL("_height")); w = phalcon_get_intval(&image_width); h = phalcon_get_intval(&image_height); i = Z_LVAL_P(opacity); i = (int)((i * 127 / 100) - 127); if (i < 0) { i *= -1; } ZVAL_LONG(opacity, i); ZVAL_LONG(&tmp, 0); if (Z_TYPE_P(fontfile) == IS_STRING) { PHALCON_CALL_FUNCTIONW(&space, "imagettfbbox", size, &tmp, fontfile, text); if ( !phalcon_array_isset_fetch_long(&s0, &space, 0) || !phalcon_array_isset_fetch_long(&s1, &space, 1) || !phalcon_array_isset_fetch_long(&s4, &space, 4) || !phalcon_array_isset_fetch_long(&s5, &space, 5) ) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Call to imagettfbbox() failed"); return; } w1 = phalcon_get_intval(&s4) - phalcon_get_intval(&s0); if (w1 < 0) { w1 = -w1; } w1 += 10; ZVAL_LONG(&width, w1); h1 = phalcon_get_intval(&s5) - phalcon_get_intval(&s1); if (h1 < 0) { h1 = -h1; } h1 += 10; ZVAL_LONG(&height, h1); if (Z_TYPE_P(offset_x) == IS_LONG ) { x = phalcon_get_intval(offset_x); if (x < 0) { x = (int)(w - w1 + x + 0.5); } } else if (zend_is_true(offset_x)) { x = (int)(w - w1); } else { x = (int)(((w - w1) / 2) + 0.5); } ZVAL_LONG(offset_x, x); if (Z_TYPE_P(offset_y) == IS_LONG ) { y = phalcon_get_intval(offset_y); if (y < 0) { y = (int)(h - h1 + y + 0.5); } } else if (zend_is_true(offset_y)) { y = (int)(h - h1); } else { y = (int)(((h - h1) / 2) + 0.5); } ZVAL_LONG(offset_y, y); PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &image, r, g, b, opacity); PHALCON_CALL_FUNCTIONW(NULL, "imagettftext", &image, size, &tmp, offset_x, offset_y, &color, fontfile, text); } else { PHALCON_CALL_FUNCTIONW(&width, "imagefontwidth", size); PHALCON_CALL_FUNCTIONW(&height, "imagefontheight", size); i = Z_STRLEN_P(text); w1 = phalcon_get_intval(&width) * i; h1 = phalcon_get_intval(&height); ZVAL_LONG(&width, w1); ZVAL_LONG(&height, h1); if (Z_TYPE_P(offset_x) == IS_LONG ) { x = phalcon_get_intval(offset_x); if (x < 0) { x = (int)(w - w1 + x); } } else if (zend_is_true(offset_x)) { x = (int)(w - w1); } else { x = (int)((w - w1) / 2); } ZVAL_LONG(offset_x, x); if (Z_TYPE_P(offset_y) == IS_LONG ) { y = phalcon_get_intval(offset_y); if (y < 0) { y = (int)(h - h1 + y); } } else if (zend_is_true(offset_y)) { y = (int)(h - h1); } else { y = (int)((h - h1) / 2); } ZVAL_LONG(offset_y, y); PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &image, r, g, b, opacity); PHALCON_CALL_FUNCTIONW(NULL, "imagestring", &image, size, offset_x, offset_y, text, &color); } }
/** * Phalcon\Image\GD constructor * * @param string $file */ PHP_METHOD(Phalcon_Image_Adapter_GD, __construct){ zval *file, *width = NULL, *height = NULL, exception_message = {}, checked = {}, realpath = {}, img_width = {}, img_height = {}, type = {}, mime = {}, format = {}, image = {}, imageinfo = {}, saveflag = {}, blendmode = {}; phalcon_fetch_params(0, 1, 2, &file, &width, &height); if (Z_TYPE_P(file) != IS_STRING) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "file parameter should be a string"); return; } phalcon_return_static_property_ce(&checked, phalcon_image_adapter_gd_ce, SL("_checked")); if (!zend_is_true(&checked)) { PHALCON_CALL_CE_STATICW(NULL, phalcon_image_adapter_gd_ce, "check"); } phalcon_update_property_zval(getThis(), SL("_file"), file); if (phalcon_file_exists(file) != FAILURE) { phalcon_file_realpath(&realpath, file); if (unlikely(Z_TYPE(realpath) != IS_STRING)) { convert_to_string(&realpath); } phalcon_update_property_zval(getThis(), SL("_realpath"), &realpath); PHALCON_CALL_FUNCTIONW(&imageinfo, "getimagesize", &realpath); if (phalcon_array_isset_fetch_long(&img_width, &imageinfo, 0)) { phalcon_update_property_zval(getThis(), SL("_width"), &img_width); } if (phalcon_array_isset_fetch_long(&img_height, &imageinfo, 1)) { phalcon_update_property_zval(getThis(), SL("_height"), &img_height); } if (phalcon_array_isset_fetch_long(&type, &imageinfo, 2)) { convert_to_long(&type); phalcon_update_property_zval(getThis(), SL("_type"), &type); } else { ZVAL_LONG(&type, -1); } if (phalcon_array_isset_fetch_str(&mime, &imageinfo, SL("mime"))) { convert_to_string(&mime); phalcon_update_property_zval(getThis(), SL("_mime"), &mime); } assert(Z_TYPE(type) == IS_LONG); switch (Z_LVAL(type)) { case 1: // GIF ZVAL_STRING(&format, "gif"); PHALCON_CALL_FUNCTIONW(&image, "imagecreatefromgif", &realpath); break; case 2: // JPEG ZVAL_STRING(&format, "jpg"); PHALCON_CALL_FUNCTIONW(&image, "imagecreatefromjpeg", &realpath); break; case 3: // PNG ZVAL_STRING(&format, "png"); PHALCON_CALL_FUNCTIONW(&image, "imagecreatefrompng", &realpath); break; default: if (PHALCON_IS_NOT_EMPTY(&mime)) { zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support '%s' images", Z_STRVAL(mime)); } else { zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support such images"); } return; } if (Z_TYPE(image) != IS_RESOURCE) { assert(Z_TYPE(realpath) == IS_STRING); zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Failed to create image from file '%s'", Z_STRVAL(realpath)); return; } phalcon_update_property_zval(getThis(), SL("_format"), &format); ZVAL_TRUE(&saveflag); PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &image, &saveflag); } else if (width && height) { PHALCON_CALL_FUNCTIONW(&image, "imagecreatetruecolor", width, height); if (Z_TYPE(image) != IS_RESOURCE) { PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "imagecreatetruecolor() failed"); return; } ZVAL_TRUE(&blendmode); ZVAL_TRUE(&saveflag); PHALCON_CALL_FUNCTIONW(NULL, "imagealphablending", &image, &blendmode); PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &image, &saveflag); phalcon_update_property_zval(getThis(), SL("_realpath"), file); phalcon_update_property_zval(getThis(), SL("_width"), width); phalcon_update_property_zval(getThis(), SL("_height"), height); ZVAL_LONG(&type, 3); phalcon_update_property_zval(getThis(), SL("_type"), &type); ZVAL_STRING(&format, "png"); phalcon_update_property_zval(getThis(), SL("_format"), &format); ZVAL_STRING(&mime, "image/png"); phalcon_update_property_zval(getThis(), SL("_mime"), &mime); } else { PHALCON_CONCAT_SVS(&exception_message, "Failed to create image from file '", file, "'"); PHALCON_THROW_EXCEPTION_ZVALW(phalcon_image_exception_ce, &exception_message); return; } phalcon_update_property_zval(getThis(), SL("_image"), &image); }