diff options
Diffstat (limited to 'ext/gd')
-rw-r--r-- | ext/gd/gd.c | 212 | ||||
-rw-r--r-- | ext/gd/gd.stub.php | 23 | ||||
-rw-r--r-- | ext/gd/gd_arginfo.h | 37 | ||||
-rw-r--r-- | ext/gd/tests/imagetruecolortopalette_error3.phpt | 2 |
4 files changed, 165 insertions, 109 deletions
diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 46704c60cf..fe672a1aee 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -57,8 +57,6 @@ #include "gd_compat.h" -static int le_gd_font; - #include <gd.h> #include <gd_errors.h> #include <gdfontt.h> /* 1 Tiny font */ @@ -219,10 +217,7 @@ void php_gd_assign_libgdimageptr_as_extgdimage(zval *val, gdImagePtr image) static void php_gd_object_minit_helper() { - zend_class_entry ce; - INIT_CLASS_ENTRY(ce, "GdImage", class_GdImage_methods); - gd_image_ce = zend_register_internal_class(&ce); - gd_image_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES; + gd_image_ce = register_class_GdImage(); gd_image_ce->create_object = php_gd_image_object_create; gd_image_ce->serialize = zend_class_serialize_deny; gd_image_ce->unserialize = zend_class_unserialize_deny; @@ -236,6 +231,72 @@ static void php_gd_object_minit_helper() php_gd_image_object_handlers.offset = XtOffsetOf(php_gd_image_object, std); } +static zend_class_entry *gd_font_ce = NULL; +static zend_object_handlers php_gd_font_object_handlers; + +typedef struct _php_gd_font_object { + gdFontPtr font; + zend_object std; +} php_gd_font_object; + +static php_gd_font_object *php_gd_font_object_from_zend_object(zend_object *zobj) +{ + return ((php_gd_font_object*)(zobj + 1)) - 1; +} + +static zend_object *php_gd_font_object_to_zend_object(php_gd_font_object *obj) +{ + return ((zend_object*)(obj + 1)) - 1; +} + +static zend_object *php_gd_font_object_create(zend_class_entry *ce) +{ + php_gd_font_object *obj = zend_object_alloc(sizeof(php_gd_font_object), ce); + zend_object *zobj = php_gd_font_object_to_zend_object(obj); + + obj->font = NULL; + zend_object_std_init(zobj, ce); + object_properties_init(zobj, ce); + zobj->handlers = &php_gd_font_object_handlers; + + return zobj; +} + +static void php_gd_font_object_free(zend_object *zobj) +{ + php_gd_font_object *obj = php_gd_font_object_from_zend_object(zobj); + + if (obj->font) { + if (obj->font->data) { + efree(obj->font->data); + } + efree(obj->font); + obj->font = NULL; + } + + zend_object_std_dtor(zobj); +} + +static zend_function *php_gd_font_object_get_constructor(zend_object *object) +{ + zend_throw_error(NULL, "You cannot initialize a GdFont object except through helper functions"); + return NULL; +} + +static void php_gd_font_minit_helper() +{ + gd_font_ce = register_class_GdFont(); + gd_font_ce->create_object = php_gd_font_object_create; + gd_font_ce->serialize = zend_class_serialize_deny; + gd_font_ce->unserialize = zend_class_unserialize_deny; + + /* setting up the object handlers for the GdFont class */ + memcpy(&php_gd_font_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); + php_gd_font_object_handlers.clone_obj = NULL; + php_gd_font_object_handlers.free_obj = php_gd_font_object_free; + php_gd_font_object_handlers.get_constructor = php_gd_font_object_get_constructor; + php_gd_font_object_handlers.offset = XtOffsetOf(php_gd_font_object, std); +} /********************************************************* * @@ -266,19 +327,6 @@ PHP_INI_BEGIN() PHP_INI_END() /* }}} */ -/* {{{ php_free_gd_font */ -static void php_free_gd_font(zend_resource *rsrc) -{ - gdFontPtr fp = (gdFontPtr) rsrc->ptr; - - if (fp->data) { - efree(fp->data); - } - - efree(fp); -} -/* }}} */ - /* {{{ php_gd_error_method */ void php_gd_error_method(int type, const char *format, va_list args) { @@ -303,8 +351,8 @@ void php_gd_error_method(int type, const char *format, va_list args) /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(gd) { - le_gd_font = zend_register_list_destructors_ex(php_free_gd_font, NULL, "gd font", module_number); php_gd_object_minit_helper(); + php_gd_font_minit_helper(); #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED) gdFontCacheMutexSetup(); @@ -615,7 +663,6 @@ PHP_FUNCTION(gd_info) /* {{{ Load a new font */ PHP_FUNCTION(imageloadfont) { - zval *ind; zend_string *file; int hdr_size = sizeof(gdFont) - sizeof(char *); int body_size, n = 0, b, i, body_size_check; @@ -626,7 +673,7 @@ PHP_FUNCTION(imageloadfont) RETURN_THROWS(); } - stream = php_stream_open_wrapper(ZSTR_VAL(file), "rb", IGNORE_PATH | IGNORE_URL_WIN | REPORT_ERRORS, NULL); + stream = php_stream_open_wrapper(ZSTR_VAL(file), "rb", IGNORE_PATH | REPORT_ERRORS, NULL); if (stream == NULL) { RETURN_FALSE; } @@ -705,13 +752,8 @@ PHP_FUNCTION(imageloadfont) } php_stream_close(stream); - ind = zend_list_insert(font, le_gd_font); - - /* Adding 5 to the font index so we will never have font indices - * that overlap with the old fonts (with indices 1-5). The first - * list index given out is always 1. - */ - RETURN_LONG(Z_RES_HANDLE_P(ind) + 5); + object_init_ex(return_value, gd_font_ce); + php_gd_font_object_from_zend_object(Z_OBJ_P(return_value))->font = font; } /* }}} */ @@ -801,7 +843,7 @@ PHP_FUNCTION(imageistruecolor) PHP_FUNCTION(imagetruecolortopalette) { zval *IM; - zend_bool dither; + bool dither; zend_long ncolors; gdImagePtr im; @@ -954,7 +996,7 @@ PHP_FUNCTION(imagefilledarc) PHP_FUNCTION(imagealphablending) { zval *IM; - zend_bool blend; + bool blend; gdImagePtr im; if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &blend) == FAILURE) { @@ -973,7 +1015,7 @@ PHP_FUNCTION(imagealphablending) PHP_FUNCTION(imagesavealpha) { zval *IM; - zend_bool save; + bool save; gdImagePtr im; if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &save) == FAILURE) { @@ -1141,7 +1183,7 @@ PHP_FUNCTION(imagecopyresampled) PHP_FUNCTION(imagegrabwindow) { HWND window; - zend_bool client_area = 0; + bool client_area = 0; RECT rc = {0}; int Width, Height; HDC hdc; @@ -1270,7 +1312,7 @@ PHP_FUNCTION(imagerotate) gdImagePtr im_dst, im_src; double degrees; zend_long color; - zend_bool ignoretransparent = 0; + bool ignoretransparent = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odl|b", &SIM, gd_image_ce, °rees, &color, &ignoretransparent) == FAILURE) { RETURN_THROWS(); @@ -1586,7 +1628,7 @@ static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, } - stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); + stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH, NULL); if (stream == NULL) { RETURN_FALSE; } @@ -1878,7 +1920,7 @@ PHP_FUNCTION(imagexbm) char *file = NULL; size_t file_len = 0; zend_long foreground_color; - zend_bool foreground_color_is_null = 1; + bool foreground_color_is_null = 1; gdImagePtr im; int i; gdIOCtx *ctx = NULL; @@ -1891,7 +1933,7 @@ PHP_FUNCTION(imagexbm) im = php_gd_libgdimageptr_from_zval_p(imgind); if (file != NULL) { - stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); + stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH, NULL); if (stream == NULL) { RETURN_FALSE; } @@ -2016,7 +2058,7 @@ PHP_FUNCTION(imagegd2) PHP_FUNCTION(imagebmp) { zval *imgind; - zend_bool compressed = 1; + bool compressed = 1; gdImagePtr im; gdIOCtx *ctx = NULL; zval *to_zval = NULL; @@ -2566,7 +2608,7 @@ PHP_FUNCTION(imagecolortransparent) { zval *IM; zend_long COL = 0; - zend_bool COL_IS_NULL = 1; + bool COL_IS_NULL = 1; gdImagePtr im; if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) { @@ -2587,8 +2629,8 @@ PHP_FUNCTION(imagecolortransparent) PHP_FUNCTION(imageinterlace) { zval *IM; - zend_bool INT = 0; - zend_bool INT_IS_NULL = 1; + bool INT = 0; + bool INT_IS_NULL = 1; gdImagePtr im; if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) { @@ -2614,7 +2656,7 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled) { zval *IM, *POINTS; zend_long NPOINTS, COL; - zend_bool COL_IS_NULL = 1; + bool COL_IS_NULL = 1; zval *var = NULL; gdImagePtr im; gdPointPtr points; @@ -2703,42 +2745,21 @@ PHP_FUNCTION(imagefilledpolygon) /* }}} */ /* {{{ php_find_gd_font */ -static gdFontPtr php_find_gd_font(int size) +static gdFontPtr php_find_gd_font(zend_object *font_obj, zend_long font_int) { - gdFontPtr font; + if (font_obj) { + return php_gd_font_object_from_zend_object(font_obj)->font; + } - switch (size) { - case 1: - font = gdFontTiny; - break; - case 2: - font = gdFontSmall; - break; - case 3: - font = gdFontMediumBold; - break; - case 4: - font = gdFontLarge; - break; - case 5: - font = gdFontGiant; - break; - default: { - zval *zv = zend_hash_index_find(&EG(regular_list), size - 5); - if (!zv || (Z_RES_P(zv))->type != le_gd_font) { - if (size < 1) { - font = gdFontTiny; - } else { - font = gdFontGiant; - } - } else { - font = (gdFontPtr)Z_RES_P(zv)->ptr; - } - } - break; - } - - return font; + switch (font_int) { + case 1: return gdFontTiny; + case 2: return gdFontSmall; + case 3: return gdFontMediumBold; + case 4: return gdFontLarge; + case 5: return gdFontGiant; + } + + return font_int < 1 ? gdFontTiny : gdFontGiant; } /* }}} */ @@ -2748,14 +2769,15 @@ static gdFontPtr php_find_gd_font(int size) */ static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg) { - zend_long SIZE; + zend_object *font_obj; + zend_long font_int; gdFontPtr font; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &SIZE) == FAILURE) { - RETURN_THROWS(); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int) + ZEND_PARSE_PARAMETERS_END(); - font = php_find_gd_font(SIZE); + font = php_find_gd_font(font_obj, font_int); RETURN_LONG(arg ? font->h : font->w); } /* }}} */ @@ -2809,17 +2831,24 @@ static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, i static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode) { zval *IM; - zend_long SIZE, X, Y, COL; + zend_long X, Y, COL; char *C; size_t C_len; gdImagePtr im; - int ch = 0, col, x, y, size, i, l = 0; + int ch = 0, col, x, y, i, l = 0; unsigned char *str = NULL; + zend_object *font_obj; + zend_long font_int; gdFontPtr font; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllsl", &IM, gd_image_ce, &SIZE, &X, &Y, &C, &C_len, &COL) == FAILURE) { - RETURN_THROWS(); - } + ZEND_PARSE_PARAMETERS_START(6, 6) + Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce) + Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int) + Z_PARAM_LONG(X) + Z_PARAM_LONG(Y) + Z_PARAM_STRING(C, C_len) + Z_PARAM_LONG(COL) + ZEND_PARSE_PARAMETERS_END(); im = php_gd_libgdimageptr_from_zval_p(IM); @@ -2834,9 +2863,8 @@ static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode) y = Y; x = X; - size = SIZE; - font = php_find_gd_font(size); + font = php_find_gd_font(font_obj, font_int); switch (mode) { case 0: @@ -3363,7 +3391,7 @@ static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS) zval *IM; gdImagePtr im; zend_long tmp, blocksize; - zend_bool mode = 0; + bool mode = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll|b", &IM, gd_image_ce, &tmp, &blocksize, &mode) == FAILURE) { RETURN_THROWS(); @@ -3541,7 +3569,7 @@ PHP_FUNCTION(imageflip) PHP_FUNCTION(imageantialias) { zval *IM; - zend_bool alias; + bool alias; gdImagePtr im; if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &alias) == FAILURE) { @@ -3992,7 +4020,7 @@ PHP_FUNCTION(imageresolution) zval *IM; gdImagePtr im; zend_long res_x, res_y; - zend_bool res_x_is_null = 1, res_y_is_null = 1; + bool res_x_is_null = 1, res_y_is_null = 1; if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!l!", &IM, gd_image_ce, &res_x, &res_x_is_null, &res_y, &res_y_is_null) == FAILURE) { RETURN_THROWS(); @@ -4100,7 +4128,7 @@ static gdIOCtx *create_stream_context_from_zval(zval *to_zval) { return NULL; } - stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); + stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH, NULL); if (stream == NULL) { return NULL; } diff --git a/ext/gd/gd.stub.php b/ext/gd/gd.stub.php index 89a3f0e0f3..531ccb7989 100644 --- a/ext/gd/gd.stub.php +++ b/ext/gd/gd.stub.php @@ -1,14 +1,15 @@ <?php -/** @generate-function-entries */ +/** @generate-class-entries */ -final class GdImage -{ -} +/** @strict-properties */ +final class GdImage {} +/** @strict-properties */ +final class GdFont {} function gd_info(): array {} -function imageloadfont(string $filename): int|false {} +function imageloadfont(string $filename): GdFont|false {} function imagesetstyle(GdImage $image, array $style): bool {} @@ -187,17 +188,17 @@ function imageopenpolygon(GdImage $image, array $points, int $num_points_or_colo function imagefilledpolygon(GdImage $image, array $points, int $num_points_or_color, ?int $color = null): bool {} -function imagefontwidth(int $font): int {} +function imagefontwidth(GdFont|int $font): int {} -function imagefontheight(int $font): int {} +function imagefontheight(GdFont|int $font): int {} -function imagechar(GdImage $image, int $font, int $x, int $y, string $char, int $color): bool {} +function imagechar(GdImage $image, GdFont|int $font, int $x, int $y, string $char, int $color): bool {} -function imagecharup(GdImage $image, int $font, int $x, int $y, string $char, int $color): bool {} +function imagecharup(GdImage $image, GdFont|int $font, int $x, int $y, string $char, int $color): bool {} -function imagestring(GdImage $image, int $font, int $x, int $y, string $string, int $color): bool {} +function imagestring(GdImage $image, GdFont|int $font, int $x, int $y, string $string, int $color): bool {} -function imagestringup(GdImage $image, int $font, int $x, int $y, string $string, int $color): bool {} +function imagestringup(GdImage $image, GdFont|int $font, int $x, int $y, string $string, int $color): bool {} function imagecopy(GdImage $dst_image, GdImage $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_width, int $src_height): bool {} diff --git a/ext/gd/gd_arginfo.h b/ext/gd/gd_arginfo.h index ffa8b5565c..fdc509901c 100644 --- a/ext/gd/gd_arginfo.h +++ b/ext/gd/gd_arginfo.h @@ -1,10 +1,10 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 155175c4f5b60aaed37df2210ae6a5e7f979dae2 */ + * Stub hash: a2e5f8b612433e77d623bbab6b1d3d32fca7966c */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gd_info, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_imageloadfont, 0, 1, MAY_BE_LONG|MAY_BE_FALSE) +ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_imageloadfont, 0, 1, GdFont, MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -380,14 +380,14 @@ ZEND_END_ARG_INFO() #define arginfo_imagefilledpolygon arginfo_imagepolygon ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagefontwidth, 0, 1, IS_LONG, 0) - ZEND_ARG_TYPE_INFO(0, font, IS_LONG, 0) + ZEND_ARG_OBJ_TYPE_MASK(0, font, GdFont, MAY_BE_LONG, NULL) ZEND_END_ARG_INFO() #define arginfo_imagefontheight arginfo_imagefontwidth ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagechar, 0, 6, _IS_BOOL, 0) ZEND_ARG_OBJ_INFO(0, image, GdImage, 0) - ZEND_ARG_TYPE_INFO(0, font, IS_LONG, 0) + ZEND_ARG_OBJ_TYPE_MASK(0, font, GdFont, MAY_BE_LONG, NULL) ZEND_ARG_TYPE_INFO(0, x, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, y, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, char, IS_STRING, 0) @@ -398,7 +398,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagestring, 0, 6, _IS_BOOL, 0) ZEND_ARG_OBJ_INFO(0, image, GdImage, 0) - ZEND_ARG_TYPE_INFO(0, font, IS_LONG, 0) + ZEND_ARG_OBJ_TYPE_MASK(0, font, GdFont, MAY_BE_LONG, NULL) ZEND_ARG_TYPE_INFO(0, x, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, y, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0) @@ -831,3 +831,30 @@ static const zend_function_entry ext_functions[] = { static const zend_function_entry class_GdImage_methods[] = { ZEND_FE_END }; + + +static const zend_function_entry class_GdFont_methods[] = { + ZEND_FE_END +}; + +static zend_class_entry *register_class_GdImage(void) +{ + zend_class_entry ce, *class_entry; + + INIT_CLASS_ENTRY(ce, "GdImage", class_GdImage_methods); + class_entry = zend_register_internal_class_ex(&ce, NULL); + class_entry->ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES; + + return class_entry; +} + +static zend_class_entry *register_class_GdFont(void) +{ + zend_class_entry ce, *class_entry; + + INIT_CLASS_ENTRY(ce, "GdFont", class_GdFont_methods); + class_entry = zend_register_internal_class_ex(&ce, NULL); + class_entry->ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES; + + return class_entry; +} diff --git a/ext/gd/tests/imagetruecolortopalette_error3.phpt b/ext/gd/tests/imagetruecolortopalette_error3.phpt index 0af49bf38c..58bd1cf1e8 100644 --- a/ext/gd/tests/imagetruecolortopalette_error3.phpt +++ b/ext/gd/tests/imagetruecolortopalette_error3.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/func.inc'; $image = imagecreatetruecolor(50, 50); trycatch_dump( - fn() => imagetruecolortopalette($image, true, null) + fn() => imagetruecolortopalette($image, true, 0) ); ?> |