1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
14    |          Stig Bakken <ssb@php.net>                                   |
15    |          Jim Winstead <jimw@php.net>                                 |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center,
20    Cold Spring Harbor Labs. */
21 
22 /* Note that there is no code from the gd package in this file */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include "php.h"
29 #include "php_ini.h"
30 #include "ext/standard/head.h"
31 #include <math.h>
32 #include "SAPI.h"
33 #include "php_gd.h"
34 #include "ext/standard/php_image.h"
35 #include "ext/standard/info.h"
36 #include "php_open_temporary_file.h"
37 #include "php_memory_streams.h"
38 #include "zend_object_handlers.h"
39 
40 #ifdef HAVE_SYS_WAIT_H
41 # include <sys/wait.h>
42 #endif
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifdef PHP_WIN32
47 # include <io.h>
48 # include <fcntl.h>
49 # include <windows.h>
50 # include <Winuser.h>
51 # include <Wingdi.h>
52 #endif
53 
54 #if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
55 # include <X11/xpm.h>
56 #endif
57 
58 #include "gd_compat.h"
59 
60 #ifdef HAVE_GD_BUNDLED
61 # include "libgd/gd.h"
62 # include "libgd/gd_errors.h"
63 # include "libgd/gdfontt.h"  /* 1 Tiny font */
64 # include "libgd/gdfonts.h"  /* 2 Small font */
65 # include "libgd/gdfontmb.h" /* 3 Medium bold font */
66 # include "libgd/gdfontl.h"  /* 4 Large font */
67 # include "libgd/gdfontg.h"  /* 5 Giant font */
68 #else
69 # include <gd.h>
70 # include <gd_errors.h>
71 # include <gdfontt.h>  /* 1 Tiny font */
72 # include <gdfonts.h>  /* 2 Small font */
73 # include <gdfontmb.h> /* 3 Medium bold font */
74 # include <gdfontl.h>  /* 4 Large font */
75 # include <gdfontg.h>  /* 5 Giant font */
76 #endif
77 
78 #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
79 # include <ft2build.h>
80 # include FT_FREETYPE_H
81 #endif
82 
83 #if defined(HAVE_XPM) && defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
84 # include "X11/xpm.h"
85 #endif
86 
87 #ifndef M_PI
88 #define M_PI 3.14159265358979323846
89 #endif
90 
91 /* don't used libgd constants, not used, so going to be removed */
92 #define PHP_GD_FLIP_HORIZONTAL 1
93 #define PHP_GD_FLIP_VERTICAL   2
94 #define PHP_GD_FLIP_BOTH       3
95 
96 #ifdef HAVE_GD_FREETYPE
97 static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int);
98 #endif
99 
100 #include "gd_arginfo.h"
101 
102 /* as it is not really public, duplicate declaration here to avoid
103    pointless warnings */
104 int overflow2(int a, int b);
105 
106 /* Section Filters Declarations */
107 /* IMPORTANT NOTE FOR NEW FILTER
108  * Do not forget to update:
109  * IMAGE_FILTER_MAX: define the last filter index
110  * IMAGE_FILTER_MAX_ARGS: define the biggest amount of arguments
111  * image_filter array in PHP_FUNCTION(imagefilter)
112  * */
113 #define IMAGE_FILTER_NEGATE         0
114 #define IMAGE_FILTER_GRAYSCALE      1
115 #define IMAGE_FILTER_BRIGHTNESS     2
116 #define IMAGE_FILTER_CONTRAST       3
117 #define IMAGE_FILTER_COLORIZE       4
118 #define IMAGE_FILTER_EDGEDETECT     5
119 #define IMAGE_FILTER_EMBOSS         6
120 #define IMAGE_FILTER_GAUSSIAN_BLUR  7
121 #define IMAGE_FILTER_SELECTIVE_BLUR 8
122 #define IMAGE_FILTER_MEAN_REMOVAL   9
123 #define IMAGE_FILTER_SMOOTH         10
124 #define IMAGE_FILTER_PIXELATE       11
125 #define IMAGE_FILTER_SCATTER		12
126 #define IMAGE_FILTER_MAX            12
127 #define IMAGE_FILTER_MAX_ARGS       6
128 static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
129 static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
130 static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
131 static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
132 static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
133 static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
134 static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
135 static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
136 static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
137 static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
138 static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
139 static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS);
140 static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS);
141 
142 /* End Section filters declarations */
143 static gdImagePtr _php_image_create_from_string(zend_string *Data, char *tn, gdImagePtr (*ioctx_func_p)(gdIOCtxPtr));
144 static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(FILE *), gdImagePtr (*ioctx_func_p)(gdIOCtxPtr));
145 static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn);
146 static gdIOCtx *create_stream_context_from_zval(zval *to_zval);
147 static gdIOCtx *create_stream_context(php_stream *stream, int close_stream);
148 static gdIOCtx *create_output_context(void);
149 static int _php_image_type(zend_string *data);
150 
151 /* output streaming (formerly gd_ctx.c) */
152 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn);
153 
154 /*********************************************************
155  *
156  * GD Object Representation
157  *
158  ********************************************************/
159 
160 zend_class_entry *gd_image_ce;
161 
162 typedef struct _gd_ext_image_object {
163 	gdImagePtr image;
164 	zend_object std;
165 } php_gd_image_object;
166 
167 static zend_object_handlers php_gd_image_object_handlers;
168 
php_gd_image_object_get_constructor(zend_object * object)169 static zend_function *php_gd_image_object_get_constructor(zend_object *object)
170 {
171 	zend_throw_error(NULL, "You cannot initialize a GdImage object except through helper functions");
172 	return NULL;
173 }
174 
175 /**
176  * Returns the underlying php_gd_image_object from a zend_object
177  */
178 
php_gd_exgdimage_from_zobj_p(zend_object * obj)179 static zend_always_inline php_gd_image_object* php_gd_exgdimage_from_zobj_p(zend_object* obj)
180 {
181 	return (php_gd_image_object *) ((char *) (obj) - XtOffsetOf(php_gd_image_object, std));
182 }
183 
184 /**
185  * Converts an extension GdImage instance contained within a zval into the gdImagePtr
186  * for use with library APIs
187  */
php_gd_libgdimageptr_from_zval_p(zval * zp)188 PHP_GD_API gdImagePtr php_gd_libgdimageptr_from_zval_p(zval* zp)
189 {
190 	return php_gd_exgdimage_from_zobj_p(Z_OBJ_P(zp))->image;
191 }
192 
193 
php_gd_image_object_create(zend_class_entry * class_type)194 zend_object *php_gd_image_object_create(zend_class_entry *class_type)
195 {
196 	size_t block_len = sizeof(php_gd_image_object) + zend_object_properties_size(class_type);
197 	php_gd_image_object *intern = emalloc(block_len);
198 	memset(intern, 0, block_len);
199 
200 	zend_object_std_init(&intern->std, class_type);
201 	object_properties_init(&intern->std, class_type);
202 	intern->std.handlers = &php_gd_image_object_handlers;
203 
204 	return &intern->std;
205 }
206 
php_gd_image_object_free(zend_object * intern)207 static void php_gd_image_object_free(zend_object *intern)
208 {
209 	php_gd_image_object *img_obj_ptr = php_gd_exgdimage_from_zobj_p(intern);
210 	if (img_obj_ptr->image) {
211 		gdImageDestroy(img_obj_ptr->image);
212 	}
213 	zend_object_std_dtor(intern);
214 }
215 
216 /**
217  * Creates a new GdImage object wrapping the gdImagePtr and attaches it
218  * to the zval (usually return_value).
219  *
220  * This function must only be called once per valid gdImagePtr
221  */
php_gd_assign_libgdimageptr_as_extgdimage(zval * val,gdImagePtr image)222 void php_gd_assign_libgdimageptr_as_extgdimage(zval *val, gdImagePtr image)
223 {
224 	object_init_ex(val, gd_image_ce);
225 	php_gd_exgdimage_from_zobj_p(Z_OBJ_P(val))->image = image;
226 }
227 
php_gd_object_minit_helper(void)228 static void php_gd_object_minit_helper(void)
229 {
230 	gd_image_ce = register_class_GdImage();
231 	gd_image_ce->create_object = php_gd_image_object_create;
232 
233 	/* setting up the object handlers for the GdImage class */
234 	memcpy(&php_gd_image_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
235 	php_gd_image_object_handlers.clone_obj = NULL;
236 	php_gd_image_object_handlers.free_obj = php_gd_image_object_free;
237 	php_gd_image_object_handlers.get_constructor = php_gd_image_object_get_constructor;
238 	php_gd_image_object_handlers.compare = zend_objects_not_comparable;
239 	php_gd_image_object_handlers.offset = XtOffsetOf(php_gd_image_object, std);
240 }
241 
242 static zend_class_entry *gd_font_ce = NULL;
243 static zend_object_handlers php_gd_font_object_handlers;
244 
245 typedef struct _php_gd_font_object {
246 	gdFontPtr font;
247 	zend_object std;
248 } php_gd_font_object;
249 
php_gd_font_object_from_zend_object(zend_object * zobj)250 static php_gd_font_object *php_gd_font_object_from_zend_object(zend_object *zobj)
251 {
252 	return ((php_gd_font_object*)(zobj + 1)) - 1;
253 }
254 
php_gd_font_object_to_zend_object(php_gd_font_object * obj)255 static zend_object *php_gd_font_object_to_zend_object(php_gd_font_object *obj)
256 {
257 	return ((zend_object*)(obj + 1)) - 1;
258 }
259 
php_gd_font_object_create(zend_class_entry * ce)260 static zend_object *php_gd_font_object_create(zend_class_entry *ce)
261 {
262 	php_gd_font_object *obj = zend_object_alloc(sizeof(php_gd_font_object), ce);
263 	zend_object *zobj = php_gd_font_object_to_zend_object(obj);
264 
265 	obj->font = NULL;
266 	zend_object_std_init(zobj, ce);
267 	object_properties_init(zobj, ce);
268 	zobj->handlers = &php_gd_font_object_handlers;
269 
270 	return zobj;
271 }
272 
php_gd_font_object_free(zend_object * zobj)273 static void php_gd_font_object_free(zend_object *zobj)
274 {
275 	php_gd_font_object *obj = php_gd_font_object_from_zend_object(zobj);
276 
277 	if (obj->font) {
278 		if (obj->font->data) {
279 			efree(obj->font->data);
280 		}
281 		efree(obj->font);
282 		obj->font = NULL;
283 	}
284 
285 	zend_object_std_dtor(zobj);
286 }
287 
php_gd_font_object_get_constructor(zend_object * object)288 static zend_function *php_gd_font_object_get_constructor(zend_object *object)
289 {
290 	zend_throw_error(NULL, "You cannot initialize a GdFont object except through helper functions");
291 	return NULL;
292 }
293 
php_gd_font_minit_helper(void)294 static void php_gd_font_minit_helper(void)
295 {
296 	gd_font_ce = register_class_GdFont();
297 	gd_font_ce->create_object = php_gd_font_object_create;
298 
299 	/* setting up the object handlers for the GdFont class */
300 	memcpy(&php_gd_font_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
301 	php_gd_font_object_handlers.clone_obj = NULL;
302 	php_gd_font_object_handlers.free_obj = php_gd_font_object_free;
303 	php_gd_font_object_handlers.get_constructor = php_gd_font_object_get_constructor;
304 	php_gd_font_object_handlers.offset = XtOffsetOf(php_gd_font_object, std);
305 }
306 
307 /*********************************************************
308  *
309  * Extension Implementation
310  *
311  ********************************************************/
312 
313 zend_module_entry gd_module_entry = {
314 	STANDARD_MODULE_HEADER,
315 	"gd",
316 	ext_functions,
317 	PHP_MINIT(gd),
318 	PHP_MSHUTDOWN(gd),
319 	NULL,
320 	PHP_RSHUTDOWN(gd),
321 	PHP_MINFO(gd),
322 	PHP_GD_VERSION,
323 	STANDARD_MODULE_PROPERTIES
324 };
325 
326 #ifdef COMPILE_DL_GD
327 ZEND_GET_MODULE(gd)
328 #endif
329 
330 /* {{{ PHP_INI_BEGIN */
PHP_INI_BEGIN()331 PHP_INI_BEGIN()
332 	PHP_INI_ENTRY("gd.jpeg_ignore_warning", "1", PHP_INI_ALL, NULL)
333 PHP_INI_END()
334 /* }}} */
335 
336 /* {{{ php_gd_error_method */
337 void php_gd_error_method(int type, const char *format, va_list args)
338 {
339 	switch (type) {
340 #ifndef PHP_WIN32
341 		case GD_DEBUG:
342 		case GD_INFO:
343 #endif
344 		case GD_NOTICE:
345 			type = E_NOTICE;
346 			break;
347 		case GD_WARNING:
348 			type = E_WARNING;
349 			break;
350 		default:
351 			type = E_ERROR;
352 	}
353 	php_verror(NULL, "", type, format, args);
354 }
355 /* }}} */
356 
357 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(gd)358 PHP_MINIT_FUNCTION(gd)
359 {
360 	php_gd_object_minit_helper();
361 	php_gd_font_minit_helper();
362 
363 #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
364 	gdFontCacheMutexSetup();
365 #endif
366 	gdSetErrorMethod(php_gd_error_method);
367 
368 	REGISTER_INI_ENTRIES();
369 
370 	REGISTER_LONG_CONSTANT("IMG_AVIF", PHP_IMG_AVIF, CONST_CS | CONST_PERSISTENT);
371 	REGISTER_LONG_CONSTANT("IMG_GIF", PHP_IMG_GIF, CONST_CS | CONST_PERSISTENT);
372 	REGISTER_LONG_CONSTANT("IMG_JPG", PHP_IMG_JPG, CONST_CS | CONST_PERSISTENT);
373 	REGISTER_LONG_CONSTANT("IMG_JPEG", PHP_IMG_JPEG, CONST_CS | CONST_PERSISTENT);
374 	REGISTER_LONG_CONSTANT("IMG_PNG", PHP_IMG_PNG, CONST_CS | CONST_PERSISTENT);
375 	REGISTER_LONG_CONSTANT("IMG_WBMP", PHP_IMG_WBMP, CONST_CS | CONST_PERSISTENT);
376 	REGISTER_LONG_CONSTANT("IMG_XPM", PHP_IMG_XPM, CONST_CS | CONST_PERSISTENT);
377 	REGISTER_LONG_CONSTANT("IMG_WEBP", PHP_IMG_WEBP, CONST_CS | CONST_PERSISTENT);
378 	REGISTER_LONG_CONSTANT("IMG_BMP", PHP_IMG_BMP, CONST_CS | CONST_PERSISTENT);
379 	REGISTER_LONG_CONSTANT("IMG_TGA", PHP_IMG_TGA, CONST_CS | CONST_PERSISTENT);
380 
381 #ifdef gdWebpLossless
382 	/* constant for webp encoding */
383 	REGISTER_LONG_CONSTANT("IMG_WEBP_LOSSLESS", gdWebpLossless, CONST_CS | CONST_PERSISTENT);
384 #endif
385 
386 	/* special colours for gd */
387 	REGISTER_LONG_CONSTANT("IMG_COLOR_TILED", gdTiled, CONST_CS | CONST_PERSISTENT);
388 	REGISTER_LONG_CONSTANT("IMG_COLOR_STYLED", gdStyled, CONST_CS | CONST_PERSISTENT);
389 	REGISTER_LONG_CONSTANT("IMG_COLOR_BRUSHED", gdBrushed, CONST_CS | CONST_PERSISTENT);
390 	REGISTER_LONG_CONSTANT("IMG_COLOR_STYLEDBRUSHED", gdStyledBrushed, CONST_CS | CONST_PERSISTENT);
391 	REGISTER_LONG_CONSTANT("IMG_COLOR_TRANSPARENT", gdTransparent, CONST_CS | CONST_PERSISTENT);
392 
393 	/* for imagefilledarc */
394 	REGISTER_LONG_CONSTANT("IMG_ARC_ROUNDED", gdArc, CONST_CS | CONST_PERSISTENT);
395 	REGISTER_LONG_CONSTANT("IMG_ARC_PIE", gdPie, CONST_CS | CONST_PERSISTENT);
396 	REGISTER_LONG_CONSTANT("IMG_ARC_CHORD", gdChord, CONST_CS | CONST_PERSISTENT);
397 	REGISTER_LONG_CONSTANT("IMG_ARC_NOFILL", gdNoFill, CONST_CS | CONST_PERSISTENT);
398 	REGISTER_LONG_CONSTANT("IMG_ARC_EDGED", gdEdged, CONST_CS | CONST_PERSISTENT);
399 
400 	/* GD2 image format types */
401 	REGISTER_LONG_CONSTANT("IMG_GD2_RAW", GD2_FMT_RAW, CONST_CS | CONST_PERSISTENT);
402 	REGISTER_LONG_CONSTANT("IMG_GD2_COMPRESSED", GD2_FMT_COMPRESSED, CONST_CS | CONST_PERSISTENT);
403 	REGISTER_LONG_CONSTANT("IMG_FLIP_HORIZONTAL", PHP_GD_FLIP_HORIZONTAL, CONST_CS | CONST_PERSISTENT);
404 	REGISTER_LONG_CONSTANT("IMG_FLIP_VERTICAL", PHP_GD_FLIP_VERTICAL, CONST_CS | CONST_PERSISTENT);
405 	REGISTER_LONG_CONSTANT("IMG_FLIP_BOTH", PHP_GD_FLIP_BOTH, CONST_CS | CONST_PERSISTENT);
406 	REGISTER_LONG_CONSTANT("IMG_EFFECT_REPLACE", gdEffectReplace, CONST_CS | CONST_PERSISTENT);
407 	REGISTER_LONG_CONSTANT("IMG_EFFECT_ALPHABLEND", gdEffectAlphaBlend, CONST_CS | CONST_PERSISTENT);
408 	REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
409 	REGISTER_LONG_CONSTANT("IMG_EFFECT_OVERLAY", gdEffectOverlay, CONST_CS | CONST_PERSISTENT);
410 #ifdef gdEffectMultiply
411 	REGISTER_LONG_CONSTANT("IMG_EFFECT_MULTIPLY", gdEffectMultiply, CONST_CS | CONST_PERSISTENT);
412 #endif
413 
414 	REGISTER_LONG_CONSTANT("IMG_CROP_DEFAULT", GD_CROP_DEFAULT, CONST_CS | CONST_PERSISTENT);
415 	REGISTER_LONG_CONSTANT("IMG_CROP_TRANSPARENT", GD_CROP_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
416 	REGISTER_LONG_CONSTANT("IMG_CROP_BLACK", GD_CROP_BLACK, CONST_CS | CONST_PERSISTENT);
417 	REGISTER_LONG_CONSTANT("IMG_CROP_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
418 	REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, CONST_CS | CONST_PERSISTENT);
419 	REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
420 
421 
422 	REGISTER_LONG_CONSTANT("IMG_BELL", GD_BELL, CONST_CS | CONST_PERSISTENT);
423 	REGISTER_LONG_CONSTANT("IMG_BESSEL", GD_BESSEL, CONST_CS | CONST_PERSISTENT);
424 	REGISTER_LONG_CONSTANT("IMG_BILINEAR_FIXED", GD_BILINEAR_FIXED, CONST_CS | CONST_PERSISTENT);
425 	REGISTER_LONG_CONSTANT("IMG_BICUBIC", GD_BICUBIC, CONST_CS | CONST_PERSISTENT);
426 	REGISTER_LONG_CONSTANT("IMG_BICUBIC_FIXED", GD_BICUBIC_FIXED, CONST_CS | CONST_PERSISTENT);
427 	REGISTER_LONG_CONSTANT("IMG_BLACKMAN", GD_BLACKMAN, CONST_CS | CONST_PERSISTENT);
428 	REGISTER_LONG_CONSTANT("IMG_BOX", GD_BOX, CONST_CS | CONST_PERSISTENT);
429 	REGISTER_LONG_CONSTANT("IMG_BSPLINE", GD_BSPLINE, CONST_CS | CONST_PERSISTENT);
430 	REGISTER_LONG_CONSTANT("IMG_CATMULLROM", GD_CATMULLROM, CONST_CS | CONST_PERSISTENT);
431 	REGISTER_LONG_CONSTANT("IMG_GAUSSIAN", GD_GAUSSIAN, CONST_CS | CONST_PERSISTENT);
432 	REGISTER_LONG_CONSTANT("IMG_GENERALIZED_CUBIC", GD_GENERALIZED_CUBIC, CONST_CS | CONST_PERSISTENT);
433 	REGISTER_LONG_CONSTANT("IMG_HERMITE", GD_HERMITE, CONST_CS | CONST_PERSISTENT);
434 	REGISTER_LONG_CONSTANT("IMG_HAMMING", GD_HAMMING, CONST_CS | CONST_PERSISTENT);
435 	REGISTER_LONG_CONSTANT("IMG_HANNING", GD_HANNING, CONST_CS | CONST_PERSISTENT);
436 	REGISTER_LONG_CONSTANT("IMG_MITCHELL", GD_MITCHELL, CONST_CS | CONST_PERSISTENT);
437 	REGISTER_LONG_CONSTANT("IMG_POWER", GD_POWER, CONST_CS | CONST_PERSISTENT);
438 	REGISTER_LONG_CONSTANT("IMG_QUADRATIC", GD_QUADRATIC, CONST_CS | CONST_PERSISTENT);
439 	REGISTER_LONG_CONSTANT("IMG_SINC", GD_SINC, CONST_CS | CONST_PERSISTENT);
440 	REGISTER_LONG_CONSTANT("IMG_NEAREST_NEIGHBOUR", GD_NEAREST_NEIGHBOUR, CONST_CS | CONST_PERSISTENT);
441 	REGISTER_LONG_CONSTANT("IMG_WEIGHTED4", GD_WEIGHTED4, CONST_CS | CONST_PERSISTENT);
442 	REGISTER_LONG_CONSTANT("IMG_TRIANGLE", GD_TRIANGLE, CONST_CS | CONST_PERSISTENT);
443 
444 	REGISTER_LONG_CONSTANT("IMG_AFFINE_TRANSLATE", GD_AFFINE_TRANSLATE, CONST_CS | CONST_PERSISTENT);
445 	REGISTER_LONG_CONSTANT("IMG_AFFINE_SCALE", GD_AFFINE_SCALE, CONST_CS | CONST_PERSISTENT);
446 	REGISTER_LONG_CONSTANT("IMG_AFFINE_ROTATE", GD_AFFINE_ROTATE, CONST_CS | CONST_PERSISTENT);
447 	REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_HORIZONTAL", GD_AFFINE_SHEAR_HORIZONTAL, CONST_CS | CONST_PERSISTENT);
448 	REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_VERTICAL", GD_AFFINE_SHEAR_VERTICAL, CONST_CS | CONST_PERSISTENT);
449 
450 #ifdef HAVE_GD_BUNDLED
451 	REGISTER_LONG_CONSTANT("GD_BUNDLED", 1, CONST_CS | CONST_PERSISTENT);
452 #else
453 	REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
454 #endif
455 
456 	/* Section Filters */
457 	REGISTER_LONG_CONSTANT("IMG_FILTER_NEGATE", IMAGE_FILTER_NEGATE, CONST_CS | CONST_PERSISTENT);
458 	REGISTER_LONG_CONSTANT("IMG_FILTER_GRAYSCALE", IMAGE_FILTER_GRAYSCALE, CONST_CS | CONST_PERSISTENT);
459 	REGISTER_LONG_CONSTANT("IMG_FILTER_BRIGHTNESS", IMAGE_FILTER_BRIGHTNESS, CONST_CS | CONST_PERSISTENT);
460 	REGISTER_LONG_CONSTANT("IMG_FILTER_CONTRAST", IMAGE_FILTER_CONTRAST, CONST_CS | CONST_PERSISTENT);
461 	REGISTER_LONG_CONSTANT("IMG_FILTER_COLORIZE", IMAGE_FILTER_COLORIZE, CONST_CS | CONST_PERSISTENT);
462 	REGISTER_LONG_CONSTANT("IMG_FILTER_EDGEDETECT", IMAGE_FILTER_EDGEDETECT, CONST_CS | CONST_PERSISTENT);
463 	REGISTER_LONG_CONSTANT("IMG_FILTER_GAUSSIAN_BLUR", IMAGE_FILTER_GAUSSIAN_BLUR, CONST_CS | CONST_PERSISTENT);
464 	REGISTER_LONG_CONSTANT("IMG_FILTER_SELECTIVE_BLUR", IMAGE_FILTER_SELECTIVE_BLUR, CONST_CS | CONST_PERSISTENT);
465 	REGISTER_LONG_CONSTANT("IMG_FILTER_EMBOSS", IMAGE_FILTER_EMBOSS, CONST_CS | CONST_PERSISTENT);
466 	REGISTER_LONG_CONSTANT("IMG_FILTER_MEAN_REMOVAL", IMAGE_FILTER_MEAN_REMOVAL, CONST_CS | CONST_PERSISTENT);
467 	REGISTER_LONG_CONSTANT("IMG_FILTER_SMOOTH", IMAGE_FILTER_SMOOTH, CONST_CS | CONST_PERSISTENT);
468 	REGISTER_LONG_CONSTANT("IMG_FILTER_PIXELATE", IMAGE_FILTER_PIXELATE, CONST_CS | CONST_PERSISTENT);
469 	REGISTER_LONG_CONSTANT("IMG_FILTER_SCATTER", IMAGE_FILTER_SCATTER, CONST_CS | CONST_PERSISTENT);
470 	/* End Section Filters */
471 
472 #ifdef GD_VERSION_STRING
473 	REGISTER_STRING_CONSTANT("GD_VERSION", GD_VERSION_STRING, CONST_CS | CONST_PERSISTENT);
474 #endif
475 
476 #if defined(GD_MAJOR_VERSION) && defined(GD_MINOR_VERSION) && defined(GD_RELEASE_VERSION) && defined(GD_EXTRA_VERSION)
477 	REGISTER_LONG_CONSTANT("GD_MAJOR_VERSION", GD_MAJOR_VERSION, CONST_CS | CONST_PERSISTENT);
478 	REGISTER_LONG_CONSTANT("GD_MINOR_VERSION", GD_MINOR_VERSION, CONST_CS | CONST_PERSISTENT);
479 	REGISTER_LONG_CONSTANT("GD_RELEASE_VERSION", GD_RELEASE_VERSION, CONST_CS | CONST_PERSISTENT);
480 	REGISTER_STRING_CONSTANT("GD_EXTRA_VERSION", GD_EXTRA_VERSION, CONST_CS | CONST_PERSISTENT);
481 #endif
482 
483 
484 #ifdef HAVE_GD_PNG
485 
486 	/*
487 	 * cannot include #include "png.h"
488 	 * /usr/include/pngconf.h:310:2: error: #error png.h already includes setjmp.h with some additional fixup.
489 	 * as error, use the values for now...
490 	 */
491 	REGISTER_LONG_CONSTANT("PNG_NO_FILTER",	    0x00, CONST_CS | CONST_PERSISTENT);
492 	REGISTER_LONG_CONSTANT("PNG_FILTER_NONE",   0x08, CONST_CS | CONST_PERSISTENT);
493 	REGISTER_LONG_CONSTANT("PNG_FILTER_SUB",    0x10, CONST_CS | CONST_PERSISTENT);
494 	REGISTER_LONG_CONSTANT("PNG_FILTER_UP",     0x20, CONST_CS | CONST_PERSISTENT);
495 	REGISTER_LONG_CONSTANT("PNG_FILTER_AVG",    0x40, CONST_CS | CONST_PERSISTENT);
496 	REGISTER_LONG_CONSTANT("PNG_FILTER_PAETH",  0x80, CONST_CS | CONST_PERSISTENT);
497 	REGISTER_LONG_CONSTANT("PNG_ALL_FILTERS",   0x08 | 0x10 | 0x20 | 0x40 | 0x80, CONST_CS | CONST_PERSISTENT);
498 #endif
499 
500 	return SUCCESS;
501 }
502 /* }}} */
503 
504 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(gd)505 PHP_MSHUTDOWN_FUNCTION(gd)
506 {
507 #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
508 	gdFontCacheMutexShutdown();
509 #endif
510 	UNREGISTER_INI_ENTRIES();
511 	return SUCCESS;
512 }
513 /* }}} */
514 
515 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(gd)516 PHP_RSHUTDOWN_FUNCTION(gd)
517 {
518 #ifdef HAVE_GD_FREETYPE
519 	gdFontCacheShutdown();
520 #endif
521 	return SUCCESS;
522 }
523 /* }}} */
524 
525 #ifdef HAVE_GD_BUNDLED
526 #define PHP_GD_VERSION_STRING "bundled (2.1.0 compatible)"
527 #else
528 # define PHP_GD_VERSION_STRING GD_VERSION_STRING
529 #endif
530 
531 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(gd)532 PHP_MINFO_FUNCTION(gd)
533 {
534 	php_info_print_table_start();
535 	php_info_print_table_row(2, "GD Support", "enabled");
536 
537 	/* need to use a PHPAPI function here because it is external module in windows */
538 
539 #ifdef HAVE_GD_BUNDLED
540 	php_info_print_table_row(2, "GD Version", PHP_GD_VERSION_STRING);
541 #else
542 	php_info_print_table_row(2, "GD headers Version", PHP_GD_VERSION_STRING);
543 #ifdef HAVE_GD_LIBVERSION
544 	php_info_print_table_row(2, "GD library Version", gdVersionString());
545 #endif
546 #endif
547 
548 #ifdef HAVE_GD_FREETYPE
549 	php_info_print_table_row(2, "FreeType Support", "enabled");
550 	php_info_print_table_row(2, "FreeType Linkage", "with freetype");
551 #ifdef HAVE_GD_BUNDLED
552 	{
553 		char tmp[256];
554 
555 #ifdef FREETYPE_PATCH
556 		snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
557 #elif defined(FREETYPE_MAJOR)
558 		snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
559 #else
560 		snprintf(tmp, sizeof(tmp), "1.x");
561 #endif
562 		php_info_print_table_row(2, "FreeType Version", tmp);
563 	}
564 #endif
565 #endif
566 
567 	php_info_print_table_row(2, "GIF Read Support", "enabled");
568 	php_info_print_table_row(2, "GIF Create Support", "enabled");
569 
570 #ifdef HAVE_GD_JPG
571 	{
572 		php_info_print_table_row(2, "JPEG Support", "enabled");
573 #ifdef HAVE_GD_BUNDLED
574 		php_info_print_table_row(2, "libJPEG Version", gdJpegGetVersionString());
575 #endif
576 	}
577 #endif
578 
579 #ifdef HAVE_GD_PNG
580 	php_info_print_table_row(2, "PNG Support", "enabled");
581 #ifdef HAVE_GD_BUNDLED
582 	php_info_print_table_row(2, "libPNG Version", gdPngGetVersionString());
583 #endif
584 #endif
585 	php_info_print_table_row(2, "WBMP Support", "enabled");
586 #ifdef HAVE_GD_XPM
587 	php_info_print_table_row(2, "XPM Support", "enabled");
588 #ifdef HAVE_GD_BUNDLED
589 	{
590 		char tmp[12];
591 		snprintf(tmp, sizeof(tmp), "%d", XpmLibraryVersion());
592 		php_info_print_table_row(2, "libXpm Version", tmp);
593 	}
594 #endif
595 #endif
596 	php_info_print_table_row(2, "XBM Support", "enabled");
597 #ifdef USE_GD_JISX0208
598 	php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
599 #endif
600 #ifdef HAVE_GD_WEBP
601 	php_info_print_table_row(2, "WebP Support", "enabled");
602 #endif
603 #ifdef HAVE_GD_BMP
604 	php_info_print_table_row(2, "BMP Support", "enabled");
605 #endif
606 #ifdef HAVE_GD_AVIF
607 	php_info_print_table_row(2, "AVIF Support", "enabled");
608 #endif
609 #ifdef HAVE_GD_TGA
610 	php_info_print_table_row(2, "TGA Read Support", "enabled");
611 #endif
612 	php_info_print_table_end();
613 	DISPLAY_INI_ENTRIES();
614 }
615 /* }}} */
616 
617 /* {{{ */
PHP_FUNCTION(gd_info)618 PHP_FUNCTION(gd_info)
619 {
620 	if (zend_parse_parameters_none() == FAILURE) {
621 		RETURN_THROWS();
622 	}
623 
624 	array_init(return_value);
625 
626 	add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING);
627 
628 #ifdef HAVE_GD_FREETYPE
629 	add_assoc_bool(return_value, "FreeType Support", 1);
630 	add_assoc_string(return_value, "FreeType Linkage", "with freetype");
631 #else
632 	add_assoc_bool(return_value, "FreeType Support", 0);
633 #endif
634 	add_assoc_bool(return_value, "GIF Read Support", 1);
635 	add_assoc_bool(return_value, "GIF Create Support", 1);
636 #ifdef HAVE_GD_JPG
637 	add_assoc_bool(return_value, "JPEG Support", 1);
638 #else
639 	add_assoc_bool(return_value, "JPEG Support", 0);
640 #endif
641 #ifdef HAVE_GD_PNG
642 	add_assoc_bool(return_value, "PNG Support", 1);
643 #else
644 	add_assoc_bool(return_value, "PNG Support", 0);
645 #endif
646 	add_assoc_bool(return_value, "WBMP Support", 1);
647 #ifdef HAVE_GD_XPM
648 	add_assoc_bool(return_value, "XPM Support", 1);
649 #else
650 	add_assoc_bool(return_value, "XPM Support", 0);
651 #endif
652 	add_assoc_bool(return_value, "XBM Support", 1);
653 #ifdef HAVE_GD_WEBP
654 	add_assoc_bool(return_value, "WebP Support", 1);
655 #else
656 	add_assoc_bool(return_value, "WebP Support", 0);
657 #endif
658 #ifdef HAVE_GD_BMP
659 	add_assoc_bool(return_value, "BMP Support", 1);
660 #else
661 	add_assoc_bool(return_value, "BMP Support", 0);
662 #endif
663 #ifdef HAVE_GD_AVIF
664 	add_assoc_bool(return_value, "AVIF Support", 1);
665 #else
666 	add_assoc_bool(return_value, "AVIF Support", 0);
667 #endif
668 #ifdef HAVE_GD_TGA
669 	add_assoc_bool(return_value, "TGA Read Support", 1);
670 #else
671 	add_assoc_bool(return_value, "TGA Read Support", 0);
672 #endif
673 #ifdef USE_GD_JISX0208
674 	add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
675 #else
676 	add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
677 #endif
678 }
679 /* }}} */
680 
681 #define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
682 
683 /* {{{ Load a new font */
PHP_FUNCTION(imageloadfont)684 PHP_FUNCTION(imageloadfont)
685 {
686 	zend_string *file;
687 	int hdr_size = sizeof(gdFont) - sizeof(char *);
688 	int body_size, n = 0, b, i, body_size_check;
689 	gdFontPtr font;
690 	php_stream *stream;
691 
692 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file) == FAILURE) {
693 		RETURN_THROWS();
694 	}
695 
696 	stream = php_stream_open_wrapper(ZSTR_VAL(file), "rb", IGNORE_PATH | REPORT_ERRORS, NULL);
697 	if (stream == NULL) {
698 		RETURN_FALSE;
699 	}
700 
701 	/* Only supports a architecture-dependent binary dump format
702 	 * at the moment.
703 	 * The file format is like this on machines with 32-byte integers:
704 	 *
705 	 * byte 0-3:   (int) number of characters in the font
706 	 * byte 4-7:   (int) value of first character in the font (often 32, space)
707 	 * byte 8-11:  (int) pixel width of each character
708 	 * byte 12-15: (int) pixel height of each character
709 	 * bytes 16-:  (char) array with character data, one byte per pixel
710 	 *                    in each character, for a total of
711 	 *                    (nchars*width*height) bytes.
712 	 */
713 	font = (gdFontPtr) emalloc(sizeof(gdFont));
714 	b = 0;
715 	while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b)) > 0) {
716 		b += n;
717 	}
718 
719 	if (n <= 0) {
720 		efree(font);
721 		if (php_stream_eof(stream)) {
722 			php_error_docref(NULL, E_WARNING, "End of file while reading header");
723 		} else {
724 			php_error_docref(NULL, E_WARNING, "Error while reading header");
725 		}
726 		php_stream_close(stream);
727 		RETURN_FALSE;
728 	}
729 	i = php_stream_tell(stream);
730 	php_stream_seek(stream, 0, SEEK_END);
731 	body_size_check = php_stream_tell(stream) - hdr_size;
732 	php_stream_seek(stream, i, SEEK_SET);
733 
734 	if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
735 		php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
736 		efree(font);
737 		php_stream_close(stream);
738 		RETURN_FALSE;
739 	}
740 
741 	body_size = font->w * font->h * font->nchars;
742 	if (body_size != body_size_check) {
743 		font->w = FLIPWORD(font->w);
744 		font->h = FLIPWORD(font->h);
745 		font->nchars = FLIPWORD(font->nchars);
746 		body_size = font->w * font->h * font->nchars;
747 	}
748 
749 	if (body_size != body_size_check) {
750 		php_error_docref(NULL, E_WARNING, "Error reading font");
751 		efree(font);
752 		php_stream_close(stream);
753 		RETURN_FALSE;
754 	}
755 
756 	font->data = emalloc(body_size);
757 	b = 0;
758 	while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b)) > 0) {
759 		b += n;
760 	}
761 
762 	if (n <= 0) {
763 		efree(font->data);
764 		efree(font);
765 		if (php_stream_eof(stream)) {
766 			php_error_docref(NULL, E_WARNING, "End of file while reading body");
767 		} else {
768 			php_error_docref(NULL, E_WARNING, "Error while reading body");
769 		}
770 		php_stream_close(stream);
771 		RETURN_FALSE;
772 	}
773 	php_stream_close(stream);
774 
775 	object_init_ex(return_value, gd_font_ce);
776 	php_gd_font_object_from_zend_object(Z_OBJ_P(return_value))->font = font;
777 }
778 /* }}} */
779 
780 /* {{{ Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
PHP_FUNCTION(imagesetstyle)781 PHP_FUNCTION(imagesetstyle)
782 {
783 	zval *IM, *styles, *item;
784 	gdImagePtr im;
785 	int *stylearr;
786 	int index = 0;
787 	uint32_t num_styles;
788 
789 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &styles) == FAILURE)  {
790 		RETURN_THROWS();
791 	}
792 
793 	im = php_gd_libgdimageptr_from_zval_p(IM);
794 
795 	num_styles = zend_hash_num_elements(Z_ARRVAL_P(styles));
796 	if (num_styles == 0) {
797 		zend_argument_value_error(2, "cannot be empty");
798 		RETURN_THROWS();
799 	}
800 
801 	/* copy the style values in the stylearr */
802 	stylearr = safe_emalloc(sizeof(int), num_styles, 0);
803 
804 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(styles), item) {
805 		stylearr[index++] = zval_get_long(item);
806 	} ZEND_HASH_FOREACH_END();
807 
808 	gdImageSetStyle(im, stylearr, index);
809 
810 	efree(stylearr);
811 
812 	RETURN_TRUE;
813 }
814 /* }}} */
815 
816 /* {{{ Create a new true color image */
PHP_FUNCTION(imagecreatetruecolor)817 PHP_FUNCTION(imagecreatetruecolor)
818 {
819 	zend_long x_size, y_size;
820 	gdImagePtr im;
821 
822 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
823 		RETURN_THROWS();
824 	}
825 
826 	if (x_size <= 0 || x_size >= INT_MAX) {
827 		zend_argument_value_error(1, "must be greater than 0");
828 		RETURN_THROWS();
829 	}
830 
831 	if (y_size <= 0 || y_size >= INT_MAX) {
832 		zend_argument_value_error(2, "must be greater than 0");
833 		RETURN_THROWS();
834 	}
835 
836 	im = gdImageCreateTrueColor(x_size, y_size);
837 
838 	if (!im) {
839 		RETURN_FALSE;
840 	}
841 
842 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
843 }
844 /* }}} */
845 
846 /* {{{ return true if the image uses truecolor */
PHP_FUNCTION(imageistruecolor)847 PHP_FUNCTION(imageistruecolor)
848 {
849 	zval *IM;
850 	gdImagePtr im;
851 
852 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
853 		RETURN_THROWS();
854 	}
855 
856 	im = php_gd_libgdimageptr_from_zval_p(IM);
857 
858 	RETURN_BOOL(im->trueColor);
859 }
860 /* }}} */
861 
862 /* {{{ Convert a true color image to a palette based image with a number of colors, optionally using dithering. */
PHP_FUNCTION(imagetruecolortopalette)863 PHP_FUNCTION(imagetruecolortopalette)
864 {
865 	zval *IM;
866 	bool dither;
867 	zend_long ncolors;
868 	gdImagePtr im;
869 
870 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Obl", &IM, gd_image_ce, &dither, &ncolors) == FAILURE)  {
871 		RETURN_THROWS();
872 	}
873 
874 	im = php_gd_libgdimageptr_from_zval_p(IM);
875 
876 	if (ncolors <= 0 || ZEND_LONG_INT_OVFL(ncolors)) {
877 		zend_argument_value_error(3, "must be greater than 0 and less than %d", INT_MAX);
878 		RETURN_THROWS();
879 	}
880 
881 	if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) {
882 		RETURN_TRUE;
883 	} else {
884 		php_error_docref(NULL, E_WARNING, "Couldn't convert to palette");
885 		RETURN_FALSE;
886 	}
887 }
888 /* }}} */
889 
890 /* {{{ Convert a palette based image to a true color image. */
PHP_FUNCTION(imagepalettetotruecolor)891 PHP_FUNCTION(imagepalettetotruecolor)
892 {
893 	zval *IM;
894 	gdImagePtr im;
895 
896 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE)  {
897 		RETURN_THROWS();
898 	}
899 
900 	im = php_gd_libgdimageptr_from_zval_p(IM);
901 
902 	if (gdImagePaletteToTrueColor(im) == 0) {
903 		RETURN_FALSE;
904 	}
905 
906 	RETURN_TRUE;
907 }
908 /* }}} */
909 
910 /* {{{ Makes the colors of the palette version of an image more closely match the true color version */
PHP_FUNCTION(imagecolormatch)911 PHP_FUNCTION(imagecolormatch)
912 {
913 	zval *IM1, *IM2;
914 	gdImagePtr im1, im2;
915 	int result;
916 
917 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM1, gd_image_ce, &IM2, gd_image_ce) == FAILURE) {
918 		RETURN_THROWS();
919 	}
920 
921 	im1 = php_gd_libgdimageptr_from_zval_p(IM1);
922 	im2 = php_gd_libgdimageptr_from_zval_p(IM2);
923 
924 	result = gdImageColorMatch(im1, im2);
925 	switch (result) {
926 		case -1:
927 			zend_argument_value_error(1, "must be TrueColor");
928 			RETURN_THROWS();
929 			break;
930 		case -2:
931 			zend_argument_value_error(2, "must be Palette");
932 			RETURN_THROWS();
933 			break;
934 		case -3:
935 			zend_argument_value_error(2, "must be the same size as argument #1 ($im1)");
936 			RETURN_THROWS();
937 			break;
938 		case -4:
939 			zend_argument_value_error(2, "must have at least one color");
940 			RETURN_THROWS();
941 			break;
942 	}
943 
944 	RETURN_TRUE;
945 }
946 /* }}} */
947 
948 /* {{{ Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
PHP_FUNCTION(imagesetthickness)949 PHP_FUNCTION(imagesetthickness)
950 {
951 	zval *IM;
952 	zend_long thick;
953 	gdImagePtr im;
954 
955 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &thick) == FAILURE) {
956 		RETURN_THROWS();
957 	}
958 
959 	im = php_gd_libgdimageptr_from_zval_p(IM);
960 
961 	gdImageSetThickness(im, thick);
962 
963 	RETURN_TRUE;
964 }
965 /* }}} */
966 
967 /* {{{ Draw an ellipse */
PHP_FUNCTION(imagefilledellipse)968 PHP_FUNCTION(imagefilledellipse)
969 {
970 	zval *IM;
971 	zend_long cx, cy, w, h, color;
972 	gdImagePtr im;
973 
974 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &color) == FAILURE) {
975 		RETURN_THROWS();
976 	}
977 
978 	im = php_gd_libgdimageptr_from_zval_p(IM);
979 
980 	gdImageFilledEllipse(im, cx, cy, w, h, color);
981 	RETURN_TRUE;
982 }
983 /* }}} */
984 
985 /* {{{ Draw a filled partial ellipse */
PHP_FUNCTION(imagefilledarc)986 PHP_FUNCTION(imagefilledarc)
987 {
988 	zval *IM;
989 	zend_long cx, cy, w, h, ST, E, col, style;
990 	gdImagePtr im;
991 	int e, st;
992 
993 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
994 		RETURN_THROWS();
995 	}
996 
997 	im = php_gd_libgdimageptr_from_zval_p(IM);
998 
999 	e = E;
1000 	if (e < 0) {
1001 		e %= 360;
1002 	}
1003 
1004 	st = ST;
1005 	if (st < 0) {
1006 		st %= 360;
1007 	}
1008 
1009 	gdImageFilledArc(im, cx, cy, w, h, st, e, col, style);
1010 
1011 	RETURN_TRUE;
1012 }
1013 /* }}} */
1014 
1015 /* {{{ Turn alpha blending mode on or off for the given image */
PHP_FUNCTION(imagealphablending)1016 PHP_FUNCTION(imagealphablending)
1017 {
1018 	zval *IM;
1019 	bool blend;
1020 	gdImagePtr im;
1021 
1022 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &blend) == FAILURE) {
1023 		RETURN_THROWS();
1024 	}
1025 
1026 	im = php_gd_libgdimageptr_from_zval_p(IM);
1027 
1028 	gdImageAlphaBlending(im, blend);
1029 
1030 	RETURN_TRUE;
1031 }
1032 /* }}} */
1033 
1034 /* {{{ Include alpha channel to a saved image */
PHP_FUNCTION(imagesavealpha)1035 PHP_FUNCTION(imagesavealpha)
1036 {
1037 	zval *IM;
1038 	bool save;
1039 	gdImagePtr im;
1040 
1041 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &save) == FAILURE) {
1042 		RETURN_THROWS();
1043 	}
1044 
1045 	im = php_gd_libgdimageptr_from_zval_p(IM);
1046 
1047 	gdImageSaveAlpha(im, save);
1048 
1049 	RETURN_TRUE;
1050 }
1051 /* }}} */
1052 
1053 /* {{{ Set the alpha blending flag to use the bundled libgd layering effects */
PHP_FUNCTION(imagelayereffect)1054 PHP_FUNCTION(imagelayereffect)
1055 {
1056 	zval *IM;
1057 	zend_long effect;
1058 	gdImagePtr im;
1059 
1060 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &effect) == FAILURE) {
1061 		RETURN_THROWS();
1062 	}
1063 
1064 	im = php_gd_libgdimageptr_from_zval_p(IM);
1065 
1066 	gdImageAlphaBlending(im, effect);
1067 
1068 	RETURN_TRUE;
1069 }
1070 /* }}} */
1071 
1072 #define CHECK_RGBA_RANGE(component, name, argument_number) \
1073 	if (component < 0 || component > gd##name##Max) { \
1074 		zend_argument_value_error(argument_number, "must be between 0 and %d (inclusive)", gd##name##Max); \
1075 		RETURN_THROWS(); \
1076 	}
1077 
1078 /* {{{ Allocate a color with an alpha level.  Works for true color and palette based images */
PHP_FUNCTION(imagecolorallocatealpha)1079 PHP_FUNCTION(imagecolorallocatealpha)
1080 {
1081 	zval *IM;
1082 	zend_long red, green, blue, alpha;
1083 	gdImagePtr im;
1084 	int ct = (-1);
1085 
1086 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1087 		RETURN_THROWS();
1088 	}
1089 
1090 	im = php_gd_libgdimageptr_from_zval_p(IM);
1091 
1092 	CHECK_RGBA_RANGE(red, Red, 2);
1093 	CHECK_RGBA_RANGE(green, Green, 3);
1094 	CHECK_RGBA_RANGE(blue, Blue, 4);
1095 	CHECK_RGBA_RANGE(alpha, Alpha, 5);
1096 
1097 	ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
1098 	if (ct < 0) {
1099 		RETURN_FALSE;
1100 	}
1101 	RETURN_LONG((zend_long)ct);
1102 }
1103 /* }}} */
1104 
1105 /* {{{ Resolve/Allocate a colour with an alpha level.  Works for true colour and palette based images */
PHP_FUNCTION(imagecolorresolvealpha)1106 PHP_FUNCTION(imagecolorresolvealpha)
1107 {
1108 	zval *IM;
1109 	zend_long red, green, blue, alpha;
1110 	gdImagePtr im;
1111 
1112 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1113 		RETURN_THROWS();
1114 	}
1115 
1116 	im = php_gd_libgdimageptr_from_zval_p(IM);
1117 
1118 	CHECK_RGBA_RANGE(red, Red, 2);
1119 	CHECK_RGBA_RANGE(green, Green, 3);
1120 	CHECK_RGBA_RANGE(blue, Blue, 4);
1121 	CHECK_RGBA_RANGE(alpha, Alpha, 5);
1122 
1123 	RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha));
1124 }
1125 /* }}} */
1126 
1127 /* {{{ Find the closest matching colour with alpha transparency */
PHP_FUNCTION(imagecolorclosestalpha)1128 PHP_FUNCTION(imagecolorclosestalpha)
1129 {
1130 	zval *IM;
1131 	zend_long red, green, blue, alpha;
1132 	gdImagePtr im;
1133 
1134 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1135 		RETURN_THROWS();
1136 	}
1137 
1138 	im = php_gd_libgdimageptr_from_zval_p(IM);
1139 
1140 	CHECK_RGBA_RANGE(red, Red, 2);
1141 	CHECK_RGBA_RANGE(green, Green, 3);
1142 	CHECK_RGBA_RANGE(blue, Blue, 4);
1143 	CHECK_RGBA_RANGE(alpha, Alpha, 5);
1144 
1145 	RETURN_LONG(gdImageColorClosestAlpha(im, red, green, blue, alpha));
1146 }
1147 /* }}} */
1148 
1149 /* {{{ Find exact match for colour with transparency */
PHP_FUNCTION(imagecolorexactalpha)1150 PHP_FUNCTION(imagecolorexactalpha)
1151 {
1152 	zval *IM;
1153 	zend_long red, green, blue, alpha;
1154 	gdImagePtr im;
1155 
1156 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1157 		RETURN_THROWS();
1158 	}
1159 
1160 	im = php_gd_libgdimageptr_from_zval_p(IM);
1161 
1162 	CHECK_RGBA_RANGE(red, Red, 2);
1163 	CHECK_RGBA_RANGE(green, Green, 3);
1164 	CHECK_RGBA_RANGE(blue, Blue, 4);
1165 	CHECK_RGBA_RANGE(alpha, Alpha, 5);
1166 
1167 	RETURN_LONG(gdImageColorExactAlpha(im, red, green, blue, alpha));
1168 }
1169 /* }}} */
1170 
1171 /* {{{ Copy and resize part of an image using resampling to help ensure clarity */
PHP_FUNCTION(imagecopyresampled)1172 PHP_FUNCTION(imagecopyresampled)
1173 {
1174 	zval *SIM, *DIM;
1175 	zend_long SX, SY, SW, SH, DX, DY, DW, DH;
1176 	gdImagePtr im_dst, im_src;
1177 	int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
1178 
1179 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
1180 		RETURN_THROWS();
1181 	}
1182 
1183 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
1184 	im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
1185 
1186 	srcX = SX;
1187 	srcY = SY;
1188 	srcH = SH;
1189 	srcW = SW;
1190 	dstX = DX;
1191 	dstY = DY;
1192 	dstH = DH;
1193 	dstW = DW;
1194 
1195 	gdImageCopyResampled(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
1196 
1197 	RETURN_TRUE;
1198 }
1199 /* }}} */
1200 
1201 #ifdef PHP_WIN32
1202 /* {{{ Grab a window or its client area using a windows handle (HWND property in COM instance) */
PHP_FUNCTION(imagegrabwindow)1203 PHP_FUNCTION(imagegrabwindow)
1204 {
1205 	HWND window;
1206 	bool client_area = 0;
1207 	RECT rc = {0};
1208 	int Width, Height;
1209 	HDC		hdc;
1210 	HDC memDC;
1211 	HBITMAP memBM;
1212 	HBITMAP hOld;
1213 	zend_long lwindow_handle;
1214 	gdImagePtr im = NULL;
1215 
1216 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &lwindow_handle, &client_area) == FAILURE) {
1217 		RETURN_THROWS();
1218 	}
1219 
1220 	window = (HWND) lwindow_handle;
1221 
1222 	if (!IsWindow(window)) {
1223 		php_error_docref(NULL, E_NOTICE, "Invalid window handle");
1224 		RETURN_FALSE;
1225 	}
1226 
1227 	hdc		= GetDC(0);
1228 
1229 	if (client_area) {
1230 		GetClientRect(window, &rc);
1231 		Width = rc.right;
1232 		Height = rc.bottom;
1233 	} else {
1234 		GetWindowRect(window, &rc);
1235 		Width	= rc.right - rc.left;
1236 		Height	= rc.bottom - rc.top;
1237 	}
1238 
1239 	Width		= (Width/4)*4;
1240 
1241 	memDC	= CreateCompatibleDC(hdc);
1242 	memBM	= CreateCompatibleBitmap(hdc, Width, Height);
1243 	hOld	= (HBITMAP) SelectObject (memDC, memBM);
1244 
1245 	PrintWindow(window, memDC, (UINT) client_area);
1246 
1247 	im = gdImageCreateTrueColor(Width, Height);
1248 	if (im) {
1249 		int x,y;
1250 		for (y=0; y <= Height; y++) {
1251 			for (x=0; x <= Width; x++) {
1252 				int c = GetPixel(memDC, x,y);
1253 				gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
1254 			}
1255 		}
1256 	}
1257 
1258 	SelectObject(memDC,hOld);
1259 	DeleteObject(memBM);
1260 	DeleteDC(memDC);
1261 	ReleaseDC( 0, hdc );
1262 
1263 	if (!im) {
1264 		RETURN_FALSE;
1265 	}
1266 
1267 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1268 }
1269 /* }}} */
1270 
1271 /* {{{ Grab a screenshot */
PHP_FUNCTION(imagegrabscreen)1272 PHP_FUNCTION(imagegrabscreen)
1273 {
1274 	HWND window = GetDesktopWindow();
1275 	RECT rc = {0};
1276 	int Width, Height;
1277 	HDC		hdc;
1278 	HDC memDC;
1279 	HBITMAP memBM;
1280 	HBITMAP hOld;
1281 	gdImagePtr im;
1282 	hdc		= GetDC(0);
1283 
1284 	if (zend_parse_parameters_none() == FAILURE) {
1285 		RETURN_THROWS();
1286 	}
1287 
1288 	if (!hdc) {
1289 		RETURN_FALSE;
1290 	}
1291 
1292 	GetWindowRect(window, &rc);
1293 	Width	= rc.right - rc.left;
1294 	Height	= rc.bottom - rc.top;
1295 
1296 	Width		= (Width/4)*4;
1297 
1298 	memDC	= CreateCompatibleDC(hdc);
1299 	memBM	= CreateCompatibleBitmap(hdc, Width, Height);
1300 	hOld	= (HBITMAP) SelectObject (memDC, memBM);
1301 	BitBlt( memDC, 0, 0, Width, Height , hdc, rc.left, rc.top , SRCCOPY );
1302 
1303 	im = gdImageCreateTrueColor(Width, Height);
1304 	if (im) {
1305 		int x,y;
1306 		for (y=0; y <= Height; y++) {
1307 			for (x=0; x <= Width; x++) {
1308 				int c = GetPixel(memDC, x,y);
1309 				gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
1310 			}
1311 		}
1312 	}
1313 
1314 	SelectObject(memDC,hOld);
1315 	DeleteObject(memBM);
1316 	DeleteDC(memDC);
1317 	ReleaseDC( 0, hdc );
1318 
1319 	if (!im) {
1320 		RETURN_FALSE;
1321 	}
1322 
1323 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1324 }
1325 /* }}} */
1326 #endif /* PHP_WIN32 */
1327 
1328 /* {{{ Rotate an image using a custom angle */
PHP_FUNCTION(imagerotate)1329 PHP_FUNCTION(imagerotate)
1330 {
1331 	zval *SIM;
1332 	gdImagePtr im_dst, im_src;
1333 	double degrees;
1334 	zend_long color;
1335 	bool ignoretransparent = 0;
1336 
1337 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odl|b", &SIM, gd_image_ce,  &degrees, &color, &ignoretransparent) == FAILURE) {
1338 		RETURN_THROWS();
1339 	}
1340 
1341 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
1342 	im_dst = gdImageRotateInterpolated(im_src, (const float)degrees, color);
1343 
1344 	if (im_dst == NULL) {
1345 		RETURN_FALSE;
1346 	}
1347 
1348 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_dst);
1349 }
1350 /* }}} */
1351 
1352 /* {{{ Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color */
PHP_FUNCTION(imagesettile)1353 PHP_FUNCTION(imagesettile)
1354 {
1355 	zval *IM, *TILE;
1356 	gdImagePtr im, tile;
1357 
1358 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM, gd_image_ce, &TILE, gd_image_ce) == FAILURE) {
1359 		RETURN_THROWS();
1360 	}
1361 
1362 	im = php_gd_libgdimageptr_from_zval_p(IM);
1363 	tile = php_gd_libgdimageptr_from_zval_p(TILE);
1364 
1365 	gdImageSetTile(im, tile);
1366 
1367 	RETURN_TRUE;
1368 }
1369 /* }}} */
1370 
1371 /* {{{ Set the brush image to $brush when filling $image with the "IMG_COLOR_BRUSHED" color */
PHP_FUNCTION(imagesetbrush)1372 PHP_FUNCTION(imagesetbrush)
1373 {
1374 	zval *IM, *TILE;
1375 	gdImagePtr im, tile;
1376 
1377 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM, gd_image_ce, &TILE, gd_image_ce) == FAILURE) {
1378 		RETURN_THROWS();
1379 	}
1380 
1381 	im = php_gd_libgdimageptr_from_zval_p(IM);
1382 	tile = php_gd_libgdimageptr_from_zval_p(TILE);
1383 
1384 	gdImageSetBrush(im, tile);
1385 
1386 	RETURN_TRUE;
1387 }
1388 /* }}} */
1389 
1390 /* {{{ Create a new image */
PHP_FUNCTION(imagecreate)1391 PHP_FUNCTION(imagecreate)
1392 {
1393 	zend_long x_size, y_size;
1394 	gdImagePtr im;
1395 
1396 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
1397 		RETURN_THROWS();
1398 	}
1399 
1400 	if (x_size <= 0 || x_size >= INT_MAX) {
1401 		zend_argument_value_error(1, "must be greater than 0");
1402 		RETURN_THROWS();
1403 	}
1404 
1405 	if (y_size <= 0 || y_size >= INT_MAX) {
1406 		zend_argument_value_error(2, "must be greater than 0");
1407 		RETURN_THROWS();
1408 	}
1409 
1410 	im = gdImageCreate(x_size, y_size);
1411 
1412 	if (!im) {
1413 		RETURN_FALSE;
1414 	}
1415 
1416 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1417 }
1418 /* }}} */
1419 
1420 /* {{{ Return the types of images supported in a bitfield - 1=GIF, 2=JPEG, 4=PNG, 8=WBMP, 16=XPM, etc */
PHP_FUNCTION(imagetypes)1421 PHP_FUNCTION(imagetypes)
1422 {
1423 	int ret = 0;
1424 	ret = PHP_IMG_GIF;
1425 #ifdef HAVE_GD_JPG
1426 	ret |= PHP_IMG_JPG;
1427 #endif
1428 #ifdef HAVE_GD_PNG
1429 	ret |= PHP_IMG_PNG;
1430 #endif
1431 	ret |= PHP_IMG_WBMP;
1432 #ifdef HAVE_GD_XPM
1433 	ret |= PHP_IMG_XPM;
1434 #endif
1435 #ifdef HAVE_GD_WEBP
1436 	ret |= PHP_IMG_WEBP;
1437 #endif
1438 #ifdef HAVE_GD_BMP
1439 	ret |= PHP_IMG_BMP;
1440 #endif
1441 #ifdef HAVE_GD_TGA
1442 	ret |= PHP_IMG_TGA;
1443 #endif
1444 #ifdef HAVE_GD_AVIF
1445 	ret |= PHP_IMG_AVIF;
1446 #endif
1447 
1448 	if (zend_parse_parameters_none() == FAILURE) {
1449 		RETURN_THROWS();
1450 	}
1451 
1452 	RETURN_LONG(ret);
1453 }
1454 /* }}} */
1455 
1456 /* {{{ _php_ctx_getmbi */
1457 
_php_ctx_getmbi(gdIOCtx * ctx)1458 static int _php_ctx_getmbi(gdIOCtx *ctx)
1459 {
1460 	int i, mbi = 0;
1461 
1462 	do {
1463 		i = (ctx->getC)(ctx);
1464 		if (i < 0) {
1465 			return -1;
1466 		}
1467 		mbi = (mbi << 7) | (i & 0x7f);
1468 	} while (i & 0x80);
1469 
1470 	return mbi;
1471 }
1472 /* }}} */
1473 
1474 /* {{{ _php_image_type
1475  * Based on ext/standard/image.c
1476  */
1477 static const char php_sig_gd2[3] = {'g', 'd', '2'};
1478 
_php_image_type(zend_string * data)1479 static int _php_image_type(zend_string *data)
1480 {
1481 	if (ZSTR_LEN(data) < 12) {
1482 		/* Handle this the same way as an unknown image type. */
1483 		return -1;
1484 	}
1485 
1486 	if (!memcmp(ZSTR_VAL(data), php_sig_gd2, sizeof(php_sig_gd2))) {
1487 		return PHP_GDIMG_TYPE_GD2;
1488 	} else if (!memcmp(ZSTR_VAL(data), php_sig_jpg, sizeof(php_sig_jpg))) {
1489 		return PHP_GDIMG_TYPE_JPG;
1490 	} else if (!memcmp(ZSTR_VAL(data), php_sig_png, sizeof(php_sig_png))) {
1491 		return PHP_GDIMG_TYPE_PNG;
1492 	} else if (!memcmp(ZSTR_VAL(data), php_sig_gif, sizeof(php_sig_gif))) {
1493 		return PHP_GDIMG_TYPE_GIF;
1494 	} else if (!memcmp(ZSTR_VAL(data), php_sig_bmp, sizeof(php_sig_bmp))) {
1495 		return PHP_GDIMG_TYPE_BMP;
1496 	} else if(!memcmp(ZSTR_VAL(data), php_sig_riff, sizeof(php_sig_riff)) && !memcmp(ZSTR_VAL(data) + sizeof(php_sig_riff) + sizeof(uint32_t), php_sig_webp, sizeof(php_sig_webp))) {
1497 		return PHP_GDIMG_TYPE_WEBP;
1498 	}
1499 
1500 	php_stream *image_stream = php_stream_memory_open(TEMP_STREAM_READONLY, data);
1501 
1502 	if (image_stream != NULL) {
1503 		bool is_avif = php_is_image_avif(image_stream);
1504 		php_stream_close(image_stream);
1505 
1506 		if (is_avif) {
1507 			return PHP_GDIMG_TYPE_AVIF;
1508 		}
1509 	}
1510 
1511 	gdIOCtx *io_ctx;
1512 	io_ctx = gdNewDynamicCtxEx(8, ZSTR_VAL(data), 0);
1513 	if (io_ctx) {
1514 		if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
1515 			io_ctx->gd_free(io_ctx);
1516 			return PHP_GDIMG_TYPE_WBM;
1517 		} else {
1518 			io_ctx->gd_free(io_ctx);
1519 		}
1520 	}
1521 
1522 	return -1;
1523 }
1524 /* }}} */
1525 
1526 /* {{{ _php_image_create_from_string */
_php_image_create_from_string(zend_string * data,char * tn,gdImagePtr (* ioctx_func_p)(gdIOCtxPtr))1527 gdImagePtr _php_image_create_from_string(zend_string *data, char *tn, gdImagePtr (*ioctx_func_p)(gdIOCtxPtr))
1528 {
1529 	gdImagePtr im;
1530 	gdIOCtx *io_ctx;
1531 
1532 	io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(data), ZSTR_VAL(data), 0);
1533 
1534 	if (!io_ctx) {
1535 		return NULL;
1536 	}
1537 
1538 	im = (*ioctx_func_p)(io_ctx);
1539 	if (!im) {
1540 		php_error_docref(NULL, E_WARNING, "Passed data is not in \"%s\" format", tn);
1541 		io_ctx->gd_free(io_ctx);
1542 		return NULL;
1543 	}
1544 
1545 	io_ctx->gd_free(io_ctx);
1546 
1547 	return im;
1548 }
1549 /* }}} */
1550 
1551 /* {{{ Create a new image from the image stream in the string */
PHP_FUNCTION(imagecreatefromstring)1552 PHP_FUNCTION(imagecreatefromstring)
1553 {
1554 	zend_string *data;
1555 	gdImagePtr im;
1556 	int imtype;
1557 
1558 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &data) == FAILURE) {
1559 		RETURN_THROWS();
1560 	}
1561 
1562 	imtype = _php_image_type(data);
1563 
1564 	switch (imtype) {
1565 		case PHP_GDIMG_TYPE_JPG:
1566 #ifdef HAVE_GD_JPG
1567 			im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx);
1568 #else
1569 			php_error_docref(NULL, E_WARNING, "No JPEG support in this PHP build");
1570 			RETURN_FALSE;
1571 #endif
1572 			break;
1573 
1574 		case PHP_GDIMG_TYPE_PNG:
1575 #ifdef HAVE_GD_PNG
1576 			im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx);
1577 #else
1578 			php_error_docref(NULL, E_WARNING, "No PNG support in this PHP build");
1579 			RETURN_FALSE;
1580 #endif
1581 			break;
1582 
1583 		case PHP_GDIMG_TYPE_GIF:
1584 			im = _php_image_create_from_string(data, "GIF", gdImageCreateFromGifCtx);
1585 			break;
1586 
1587 		case PHP_GDIMG_TYPE_WBM:
1588 			im = _php_image_create_from_string(data, "WBMP", gdImageCreateFromWBMPCtx);
1589 			break;
1590 
1591 		case PHP_GDIMG_TYPE_GD2:
1592 			im = _php_image_create_from_string(data, "GD2", gdImageCreateFromGd2Ctx);
1593 			break;
1594 
1595 		case PHP_GDIMG_TYPE_BMP:
1596 			im = _php_image_create_from_string(data, "BMP", gdImageCreateFromBmpCtx);
1597 			break;
1598 
1599 		case PHP_GDIMG_TYPE_WEBP:
1600 #ifdef HAVE_GD_WEBP
1601 			im = _php_image_create_from_string(data, "WEBP", gdImageCreateFromWebpCtx);
1602 			break;
1603 #else
1604 			php_error_docref(NULL, E_WARNING, "No WEBP support in this PHP build");
1605 			RETURN_FALSE;
1606 #endif
1607 
1608 		case PHP_GDIMG_TYPE_AVIF:
1609 #ifdef HAVE_GD_AVIF
1610 			im = _php_image_create_from_string(data, "AVIF", gdImageCreateFromAvifCtx);
1611 			break;
1612 #else
1613 			php_error_docref(NULL, E_WARNING, "No AVIF support in this PHP build");
1614 			RETURN_FALSE;
1615 #endif
1616 
1617 		default:
1618 			php_error_docref(NULL, E_WARNING, "Data is not in a recognized format");
1619 			RETURN_FALSE;
1620 	}
1621 
1622 	if (!im) {
1623 		php_error_docref(NULL, E_WARNING, "Couldn't create GD Image Stream out of Data");
1624 		RETURN_FALSE;
1625 	}
1626 
1627 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1628 }
1629 /* }}} */
1630 
1631 /* {{{ _php_image_create_from */
_php_image_create_from(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn,gdImagePtr (* func_p)(FILE *),gdImagePtr (* ioctx_func_p)(gdIOCtxPtr))1632 static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(FILE *), gdImagePtr (*ioctx_func_p)(gdIOCtxPtr))
1633 {
1634 	char *file;
1635 	size_t file_len;
1636 	zend_long srcx, srcy, width, height;
1637 	gdImagePtr im = NULL;
1638 	php_stream *stream;
1639 	FILE * fp = NULL;
1640 #ifdef HAVE_GD_JPG
1641 	long ignore_warning;
1642 #endif
1643 
1644 	if (image_type == PHP_GDIMG_TYPE_GD2PART) {
1645 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "pllll", &file, &file_len, &srcx, &srcy, &width, &height) == FAILURE) {
1646 			RETURN_THROWS();
1647 		}
1648 
1649 		if (width < 1) {
1650 			zend_argument_value_error(4, "must be greater than or equal to 1");
1651 			RETURN_THROWS();
1652 		}
1653 
1654 		if (height < 1) {
1655 			zend_argument_value_error(5, "must be greater than or equal to 1");
1656 			RETURN_THROWS();
1657 		}
1658 
1659 	} else {
1660 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &file, &file_len) == FAILURE) {
1661 			RETURN_THROWS();
1662 		}
1663 	}
1664 
1665 
1666 	stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH, NULL);
1667 	if (stream == NULL)	{
1668 		RETURN_FALSE;
1669 	}
1670 
1671 	/* try and avoid allocating a FILE* if the stream is not naturally a FILE* */
1672 	if (php_stream_is(stream, PHP_STREAM_IS_STDIO))	{
1673 		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
1674 			goto out_err;
1675 		}
1676 	} else if (ioctx_func_p || image_type == PHP_GDIMG_TYPE_GD2PART) {
1677 		/* we can create an io context */
1678 		gdIOCtx* io_ctx;
1679 		zend_string *buff;
1680 		char *pstr;
1681 
1682 		buff = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
1683 
1684 		if (!buff) {
1685 			php_error_docref(NULL, E_WARNING,"Cannot read image data");
1686 			goto out_err;
1687 		}
1688 
1689 		/* needs to be malloc (persistent) - GD will free() it later */
1690 		pstr = pestrndup(ZSTR_VAL(buff), ZSTR_LEN(buff), 1);
1691 		io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(buff), pstr, 0);
1692 		if (!io_ctx) {
1693 			pefree(pstr, 1);
1694 			zend_string_release_ex(buff, 0);
1695 			php_error_docref(NULL, E_WARNING,"Cannot allocate GD IO context");
1696 			goto out_err;
1697 		}
1698 
1699 		if (image_type == PHP_GDIMG_TYPE_GD2PART) {
1700 			im = gdImageCreateFromGd2PartCtx(io_ctx, srcx, srcy, width, height);
1701 		} else {
1702 			im = (*ioctx_func_p)(io_ctx);
1703 		}
1704 		io_ctx->gd_free(io_ctx);
1705 		pefree(pstr, 1);
1706 		zend_string_release_ex(buff, 0);
1707 	}
1708 	else if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO)) {
1709 		/* try and force the stream to be FILE* */
1710 		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {
1711 			goto out_err;
1712 		}
1713 	}
1714 
1715 	if (!im && fp) {
1716 		switch (image_type) {
1717 			case PHP_GDIMG_TYPE_GD2PART:
1718 				im = gdImageCreateFromGd2Part(fp, srcx, srcy, width, height);
1719 				break;
1720 #ifdef HAVE_GD_XPM
1721 			case PHP_GDIMG_TYPE_XPM:
1722 				im = gdImageCreateFromXpm(file);
1723 				break;
1724 #endif
1725 
1726 #ifdef HAVE_GD_JPG
1727 			case PHP_GDIMG_TYPE_JPG:
1728 				ignore_warning = INI_INT("gd.jpeg_ignore_warning");
1729 				im = gdImageCreateFromJpegEx(fp, ignore_warning);
1730 			break;
1731 #endif
1732 
1733 			default:
1734 				im = (*func_p)(fp);
1735 				break;
1736 		}
1737 
1738 		fflush(fp);
1739 	}
1740 
1741 /* register_im: */
1742 	if (im) {
1743 		php_stream_close(stream);
1744 		php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1745 		return;
1746 	}
1747 
1748 	php_error_docref(NULL, E_WARNING, "\"%s\" is not a valid %s file", file, tn);
1749 out_err:
1750 	php_stream_close(stream);
1751 	RETURN_FALSE;
1752 
1753 }
1754 /* }}} */
1755 
1756 /* {{{ Create a new image from GIF file or URL */
PHP_FUNCTION(imagecreatefromgif)1757 PHP_FUNCTION(imagecreatefromgif)
1758 {
1759 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
1760 }
1761 /* }}} */
1762 
1763 #ifdef HAVE_GD_JPG
1764 /* {{{ Create a new image from JPEG file or URL */
PHP_FUNCTION(imagecreatefromjpeg)1765 PHP_FUNCTION(imagecreatefromjpeg)
1766 {
1767 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
1768 }
1769 /* }}} */
1770 #endif /* HAVE_GD_JPG */
1771 
1772 #ifdef HAVE_GD_PNG
1773 /* {{{ Create a new image from PNG file or URL */
PHP_FUNCTION(imagecreatefrompng)1774 PHP_FUNCTION(imagecreatefrompng)
1775 {
1776 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
1777 }
1778 /* }}} */
1779 #endif /* HAVE_GD_PNG */
1780 
1781 #ifdef HAVE_GD_WEBP
1782 /* {{{ Create a new image from WEBP file or URL */
PHP_FUNCTION(imagecreatefromwebp)1783 PHP_FUNCTION(imagecreatefromwebp)
1784 {
1785 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageCreateFromWebp, gdImageCreateFromWebpCtx);
1786 }
1787 /* }}} */
1788 #endif /* HAVE_GD_WEBP */
1789 
1790 /* {{{ Create a new image from XBM file or URL */
PHP_FUNCTION(imagecreatefromxbm)1791 PHP_FUNCTION(imagecreatefromxbm)
1792 {
1793 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);
1794 }
1795 /* }}} */
1796 
1797 #ifdef HAVE_GD_AVIF
1798 /* {{{ Create a new image from AVIF file or URL */
PHP_FUNCTION(imagecreatefromavif)1799 PHP_FUNCTION(imagecreatefromavif)
1800 {
1801 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF", gdImageCreateFromAvif, gdImageCreateFromAvifCtx);
1802 }
1803 /* }}} */
1804 #endif /* HAVE_GD_AVIF */
1805 
1806 #ifdef HAVE_GD_XPM
1807 /* {{{ Create a new image from XPM file or URL */
PHP_FUNCTION(imagecreatefromxpm)1808 PHP_FUNCTION(imagecreatefromxpm)
1809 {
1810 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", NULL, NULL);
1811 }
1812 /* }}} */
1813 #endif
1814 
1815 /* {{{ Create a new image from WBMP file or URL */
PHP_FUNCTION(imagecreatefromwbmp)1816 PHP_FUNCTION(imagecreatefromwbmp)
1817 {
1818 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);
1819 }
1820 /* }}} */
1821 
1822 /* {{{ Create a new image from GD file or URL */
PHP_FUNCTION(imagecreatefromgd)1823 PHP_FUNCTION(imagecreatefromgd)
1824 {
1825 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);
1826 }
1827 /* }}} */
1828 
1829 /* {{{ Create a new image from GD2 file or URL */
PHP_FUNCTION(imagecreatefromgd2)1830 PHP_FUNCTION(imagecreatefromgd2)
1831 {
1832 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);
1833 }
1834 /* }}} */
1835 
1836 /* {{{ Create a new image from a given part of GD2 file or URL */
PHP_FUNCTION(imagecreatefromgd2part)1837 PHP_FUNCTION(imagecreatefromgd2part)
1838 {
1839 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", NULL, NULL);
1840 }
1841 /* }}} */
1842 
1843 #ifdef HAVE_GD_BMP
1844 /* {{{ Create a new image from BMP file or URL */
PHP_FUNCTION(imagecreatefrombmp)1845 PHP_FUNCTION(imagecreatefrombmp)
1846 {
1847 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_BMP, "BMP", gdImageCreateFromBmp, gdImageCreateFromBmpCtx);
1848 }
1849 /* }}} */
1850 #endif
1851 
1852 #ifdef HAVE_GD_TGA
1853 /* {{{ Create a new image from TGA file or URL */
PHP_FUNCTION(imagecreatefromtga)1854 PHP_FUNCTION(imagecreatefromtga)
1855 {
1856 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_TGA, "TGA", gdImageCreateFromTga, gdImageCreateFromTgaCtx);
1857 }
1858 /* }}} */
1859 #endif
1860 
1861 /* {{{ _php_image_output */
_php_image_output(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn)1862 static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn)
1863 {
1864 	zval *imgind;
1865 	char *file = NULL;
1866 	zend_long quality = 0, type = 0;
1867 	gdImagePtr im;
1868 	FILE *fp;
1869 	size_t file_len = 0;
1870 	int argc = ZEND_NUM_ARGS();
1871 	int q = -1, t = 1;
1872 
1873 	/* The quality parameter for gd2 stands for chunk size */
1874 
1875 	switch (image_type) {
1876 		case PHP_GDIMG_TYPE_GD:
1877 			if (zend_parse_parameters(argc, "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
1878 				RETURN_THROWS();
1879 			}
1880 			break;
1881 		case PHP_GDIMG_TYPE_GD2:
1882 			if (zend_parse_parameters(argc, "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
1883 				RETURN_THROWS();
1884 			}
1885 			break;
1886 		EMPTY_SWITCH_DEFAULT_CASE()
1887 	}
1888 
1889 	im = php_gd_libgdimageptr_from_zval_p(imgind);
1890 
1891 	if (argc >= 3) {
1892 		q = quality;
1893 		if (argc == 4) {
1894 			t = type;
1895 		}
1896 	}
1897 
1898 	if (file_len) {
1899 		PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
1900 
1901 		fp = VCWD_FOPEN(file, "wb");
1902 		if (!fp) {
1903 			php_error_docref(NULL, E_WARNING, "Unable to open \"%s\" for writing", file);
1904 			RETURN_FALSE;
1905 		}
1906 
1907 		switch (image_type) {
1908 			case PHP_GDIMG_TYPE_GD:
1909 				gdImageGd(im, fp);
1910 				break;
1911 			case PHP_GDIMG_TYPE_GD2:
1912 				if (q == -1) {
1913 					q = 128;
1914 				}
1915 				gdImageGd2(im, fp, q, t);
1916 				break;
1917 			EMPTY_SWITCH_DEFAULT_CASE()
1918 		}
1919 		fflush(fp);
1920 		fclose(fp);
1921 	} else {
1922 		int   b;
1923 		FILE *tmp;
1924 		char  buf[4096];
1925 		zend_string *path;
1926 
1927 		tmp = php_open_temporary_file(NULL, NULL, &path);
1928 		if (tmp == NULL) {
1929 			php_error_docref(NULL, E_WARNING, "Unable to open temporary file");
1930 			RETURN_FALSE;
1931 		}
1932 
1933 		switch (image_type) {
1934 			case PHP_GDIMG_TYPE_GD:
1935 				gdImageGd(im, tmp);
1936 				break;
1937 			case PHP_GDIMG_TYPE_GD2:
1938 				if (q == -1) {
1939 					q = 128;
1940 				}
1941 				gdImageGd2(im, tmp, q, t);
1942 				break;
1943 			EMPTY_SWITCH_DEFAULT_CASE()
1944 		}
1945 
1946 		fseek(tmp, 0, SEEK_SET);
1947 
1948 		while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
1949 			php_write(buf, b);
1950 		}
1951 
1952 		fclose(tmp);
1953 		VCWD_UNLINK((const char *)ZSTR_VAL(path)); /* make sure that the temporary file is removed */
1954 		zend_string_release_ex(path, 0);
1955 	}
1956 	RETURN_TRUE;
1957 }
1958 /* }}} */
1959 
1960 /* {{{ Output XBM image to browser or file */
PHP_FUNCTION(imagexbm)1961 PHP_FUNCTION(imagexbm)
1962 {
1963 	zval *imgind;
1964 	char *file = NULL;
1965 	size_t file_len = 0;
1966 	zend_long foreground_color;
1967 	bool foreground_color_is_null = 1;
1968 	gdImagePtr im;
1969 	int i;
1970 	gdIOCtx *ctx = NULL;
1971 	php_stream *stream;
1972 
1973 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op!|l!", &imgind, gd_image_ce, &file, &file_len, &foreground_color, &foreground_color_is_null) == FAILURE) {
1974 		RETURN_THROWS();
1975 	}
1976 
1977 	im = php_gd_libgdimageptr_from_zval_p(imgind);
1978 
1979 	if (file != NULL) {
1980 		stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
1981 		if (stream == NULL) {
1982 			RETURN_FALSE;
1983 		}
1984 
1985 		ctx = create_stream_context(stream, 1);
1986 	} else {
1987 		ctx = create_output_context();
1988 	}
1989 
1990 	if (foreground_color_is_null) {
1991 		for (i=0; i < gdImageColorsTotal(im); i++) {
1992 			if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
1993 				break;
1994 			}
1995 		}
1996 
1997 		foreground_color = i;
1998 	}
1999 
2000 	gdImageXbmCtx(im, file ? file : "", (int) foreground_color, ctx);
2001 
2002 	ctx->gd_free(ctx);
2003 
2004 	RETURN_TRUE;
2005 }
2006 /* }}} */
2007 
2008 /* {{{ Output GIF image to browser or file */
PHP_FUNCTION(imagegif)2009 PHP_FUNCTION(imagegif)
2010 {
2011 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF");
2012 }
2013 /* }}} */
2014 
2015 #ifdef HAVE_GD_PNG
2016 /* {{{ Output PNG image to browser or file */
PHP_FUNCTION(imagepng)2017 PHP_FUNCTION(imagepng)
2018 {
2019 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG");
2020 }
2021 /* }}} */
2022 #endif /* HAVE_GD_PNG */
2023 
2024 #ifdef HAVE_GD_WEBP
2025 /* {{{ Output WEBP image to browser or file */
PHP_FUNCTION(imagewebp)2026 PHP_FUNCTION(imagewebp)
2027 {
2028 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP");
2029 }
2030 /* }}} */
2031 #endif /* HAVE_GD_WEBP */
2032 
2033 #ifdef HAVE_GD_AVIF
2034 /* {{{ Output AVIF image to browser or file */
PHP_FUNCTION(imageavif)2035 PHP_FUNCTION(imageavif)
2036 {
2037 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF");
2038 }
2039 /* }}} */
2040 #endif /* HAVE_GD_AVIF */
2041 
2042 #ifdef HAVE_GD_JPG
2043 /* {{{ Output JPEG image to browser or file */
PHP_FUNCTION(imagejpeg)2044 PHP_FUNCTION(imagejpeg)
2045 {
2046 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG");
2047 }
2048 /* }}} */
2049 #endif /* HAVE_GD_JPG */
2050 
2051 /* {{{ Output WBMP image to browser or file */
PHP_FUNCTION(imagewbmp)2052 PHP_FUNCTION(imagewbmp)
2053 {
2054 	zval *imgind;
2055 	zend_long foreground_color;
2056 	zend_long foreground_color_is_null = 1;
2057 	gdImagePtr im;
2058 	int i;
2059 	gdIOCtx *ctx = NULL;
2060 	zval *to_zval = NULL;
2061 
2062 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l!", &imgind, gd_image_ce, &to_zval, &foreground_color, &foreground_color_is_null) == FAILURE) {
2063 		RETURN_THROWS();
2064 	}
2065 
2066 	im = php_gd_libgdimageptr_from_zval_p(imgind);
2067 
2068 	if (to_zval != NULL) {
2069 		ctx = create_stream_context_from_zval(to_zval);
2070 		if (!ctx) {
2071 			RETURN_FALSE;
2072 		}
2073 	} else {
2074 		ctx = create_output_context();
2075 	}
2076 
2077 	if (foreground_color_is_null) {
2078 		for (i=0; i < gdImageColorsTotal(im); i++) {
2079 			if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
2080 				break;
2081 			}
2082 		}
2083 
2084 		foreground_color = i;
2085 	}
2086 
2087 	gdImageWBMPCtx(im, foreground_color, ctx);
2088 
2089 	ctx->gd_free(ctx);
2090 
2091 	RETURN_TRUE;
2092 }
2093 /* }}} */
2094 
2095 /* {{{ Output GD image to browser or file */
PHP_FUNCTION(imagegd)2096 PHP_FUNCTION(imagegd)
2097 {
2098 	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD");
2099 }
2100 /* }}} */
2101 
2102 /* {{{ Output GD2 image to browser or file */
PHP_FUNCTION(imagegd2)2103 PHP_FUNCTION(imagegd2)
2104 {
2105 	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2");
2106 }
2107 /* }}} */
2108 
2109 #ifdef HAVE_GD_BMP
2110 /* {{{ Output BMP image to browser or file */
PHP_FUNCTION(imagebmp)2111 PHP_FUNCTION(imagebmp)
2112 {
2113 	zval *imgind;
2114 	bool compressed = 1;
2115 	gdImagePtr im;
2116 	gdIOCtx *ctx = NULL;
2117 	zval *to_zval = NULL;
2118 
2119 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
2120 		RETURN_THROWS();
2121 	}
2122 
2123 	im = php_gd_libgdimageptr_from_zval_p(imgind);
2124 
2125 	if (to_zval != NULL) {
2126 		ctx = create_stream_context_from_zval(to_zval);
2127 		if (!ctx) {
2128 			RETURN_FALSE;
2129 		}
2130 	} else {
2131 		ctx = create_output_context();
2132 	}
2133 
2134 	gdImageBmpCtx(im, ctx, (int) compressed);
2135 
2136 	ctx->gd_free(ctx);
2137 
2138 	RETURN_TRUE;
2139 }
2140 /* }}} */
2141 #endif
2142 
2143 /* {{{ Destroy an image - No effect as of PHP 8.0 */
PHP_FUNCTION(imagedestroy)2144 PHP_FUNCTION(imagedestroy)
2145 {
2146 	/* This function used to free the resource, as resources are no longer used, it does nothing */
2147 	zval *IM;
2148 
2149 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
2150 		RETURN_THROWS();
2151 	}
2152 
2153 	RETURN_TRUE;
2154 }
2155 /* }}} */
2156 
2157 /* {{{ Allocate a color for an image */
PHP_FUNCTION(imagecolorallocate)2158 PHP_FUNCTION(imagecolorallocate)
2159 {
2160 	zval *IM;
2161 	zend_long red, green, blue;
2162 	gdImagePtr im;
2163 	int ct = (-1);
2164 
2165 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2166 		RETURN_THROWS();
2167 	}
2168 
2169 	im = php_gd_libgdimageptr_from_zval_p(IM);
2170 
2171 	CHECK_RGBA_RANGE(red, Red, 2);
2172 	CHECK_RGBA_RANGE(green, Green, 3);
2173 	CHECK_RGBA_RANGE(blue, Blue, 4);
2174 
2175 	ct = gdImageColorAllocate(im, red, green, blue);
2176 	if (ct < 0) {
2177 		RETURN_FALSE;
2178 	}
2179 	RETURN_LONG(ct);
2180 }
2181 /* }}} */
2182 
2183 /* {{{ Copy the palette from the src image onto the dst image */
PHP_FUNCTION(imagepalettecopy)2184 PHP_FUNCTION(imagepalettecopy)
2185 {
2186 	zval *dstim, *srcim;
2187 	gdImagePtr dst, src;
2188 
2189 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &dstim, gd_image_ce, &srcim, gd_image_ce) == FAILURE) {
2190 		RETURN_THROWS();
2191 	}
2192 
2193 	src = php_gd_libgdimageptr_from_zval_p(srcim);
2194 	dst = php_gd_libgdimageptr_from_zval_p(dstim);
2195 
2196 	gdImagePaletteCopy(dst, src);
2197 }
2198 /* }}} */
2199 
2200 /* {{{ Get the index of the color of a pixel */
PHP_FUNCTION(imagecolorat)2201 PHP_FUNCTION(imagecolorat)
2202 {
2203 	zval *IM;
2204 	zend_long x, y;
2205 	gdImagePtr im;
2206 
2207 	ZEND_PARSE_PARAMETERS_START(3, 3)
2208 		Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
2209 		Z_PARAM_LONG(x)
2210 		Z_PARAM_LONG(y)
2211 	ZEND_PARSE_PARAMETERS_END();
2212 
2213 	im = php_gd_libgdimageptr_from_zval_p(IM);
2214 
2215 	if (gdImageTrueColor(im)) {
2216 		if (im->tpixels && gdImageBoundsSafe(im, x, y)) {
2217 			RETURN_LONG(gdImageTrueColorPixel(im, x, y));
2218 		} else {
2219 			php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
2220 			RETURN_FALSE;
2221 		}
2222 	} else {
2223 		if (im->pixels && gdImageBoundsSafe(im, x, y)) {
2224 			RETURN_LONG(im->pixels[y][x]);
2225 		} else {
2226 			php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
2227 			RETURN_FALSE;
2228 		}
2229 	}
2230 }
2231 /* }}} */
2232 
2233 /* {{{ Get the index of the closest color to the specified color */
PHP_FUNCTION(imagecolorclosest)2234 PHP_FUNCTION(imagecolorclosest)
2235 {
2236 	zval *IM;
2237 	zend_long red, green, blue;
2238 	gdImagePtr im;
2239 
2240 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2241 		RETURN_THROWS();
2242 	}
2243 
2244 	im = php_gd_libgdimageptr_from_zval_p(IM);
2245 
2246 	CHECK_RGBA_RANGE(red, Red, 2);
2247 	CHECK_RGBA_RANGE(green, Green, 3);
2248 	CHECK_RGBA_RANGE(blue, Blue, 4);
2249 
2250 	RETURN_LONG(gdImageColorClosest(im, red, green, blue));
2251 }
2252 /* }}} */
2253 
2254 /* {{{ Get the index of the color which has the hue, white and blackness nearest to the given color */
PHP_FUNCTION(imagecolorclosesthwb)2255 PHP_FUNCTION(imagecolorclosesthwb)
2256 {
2257 	zval *IM;
2258 	zend_long red, green, blue;
2259 	gdImagePtr im;
2260 
2261 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2262 		RETURN_THROWS();
2263 	}
2264 
2265 	im = php_gd_libgdimageptr_from_zval_p(IM);
2266 
2267 	CHECK_RGBA_RANGE(red, Red, 2);
2268 	CHECK_RGBA_RANGE(green, Green, 3);
2269 	CHECK_RGBA_RANGE(blue, Blue, 4);
2270 
2271 	RETURN_LONG(gdImageColorClosestHWB(im, red, green, blue));
2272 }
2273 /* }}} */
2274 
2275 /* {{{ De-allocate a color for an image */
PHP_FUNCTION(imagecolordeallocate)2276 PHP_FUNCTION(imagecolordeallocate)
2277 {
2278 	zval *IM;
2279 	zend_long index;
2280 	int col;
2281 	gdImagePtr im;
2282 
2283 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &index) == FAILURE) {
2284 		RETURN_THROWS();
2285 	}
2286 
2287 	im = php_gd_libgdimageptr_from_zval_p(IM);
2288 
2289 	/* We can return right away for a truecolor image as deallocating colours is meaningless here */
2290 	if (gdImageTrueColor(im)) {
2291 		RETURN_TRUE;
2292 	}
2293 
2294 	col = index;
2295 
2296 	if (col >= 0 && col < gdImageColorsTotal(im)) {
2297 		gdImageColorDeallocate(im, col);
2298 		RETURN_TRUE;
2299 	} else {
2300 		zend_argument_value_error(2, "must be between 0 and %d", gdImageColorsTotal(im));
2301 		RETURN_THROWS();
2302 	}
2303 }
2304 /* }}} */
2305 
2306 /* {{{ Get the index of the specified color or its closest possible alternative */
PHP_FUNCTION(imagecolorresolve)2307 PHP_FUNCTION(imagecolorresolve)
2308 {
2309 	zval *IM;
2310 	zend_long red, green, blue;
2311 	gdImagePtr im;
2312 
2313 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2314 		RETURN_THROWS();
2315 	}
2316 
2317 	im = php_gd_libgdimageptr_from_zval_p(IM);
2318 
2319 	CHECK_RGBA_RANGE(red, Red, 2);
2320 	CHECK_RGBA_RANGE(green, Green, 3);
2321 	CHECK_RGBA_RANGE(blue, Blue, 4);
2322 
2323 	RETURN_LONG(gdImageColorResolve(im, red, green, blue));
2324 }
2325 /* }}} */
2326 
2327 /* {{{ Get the index of the specified color */
PHP_FUNCTION(imagecolorexact)2328 PHP_FUNCTION(imagecolorexact)
2329 {
2330 	zval *IM;
2331 	zend_long red, green, blue;
2332 	gdImagePtr im;
2333 
2334 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2335 		RETURN_THROWS();
2336 	}
2337 
2338 	im = php_gd_libgdimageptr_from_zval_p(IM);
2339 
2340 	CHECK_RGBA_RANGE(red, Red, 2);
2341 	CHECK_RGBA_RANGE(green, Green, 3);
2342 	CHECK_RGBA_RANGE(blue, Blue, 4);
2343 
2344 	RETURN_LONG(gdImageColorExact(im, red, green, blue));
2345 }
2346 /* }}} */
2347 
2348 /* {{{ Set the color for the specified palette index */
PHP_FUNCTION(imagecolorset)2349 PHP_FUNCTION(imagecolorset)
2350 {
2351 	zval *IM;
2352 	zend_long color, red, green, blue, alpha = 0;
2353 	int col;
2354 	gdImagePtr im;
2355 
2356 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll|l", &IM, gd_image_ce, &color, &red, &green, &blue, &alpha) == FAILURE) {
2357 		RETURN_THROWS();
2358 	}
2359 
2360 	im = php_gd_libgdimageptr_from_zval_p(IM);
2361 
2362 	CHECK_RGBA_RANGE(red, Red, 2);
2363 	CHECK_RGBA_RANGE(green, Green, 3);
2364 	CHECK_RGBA_RANGE(blue, Blue, 4);
2365 
2366 	col = color;
2367 
2368 	if (col >= 0 && col < gdImageColorsTotal(im)) {
2369 		im->red[col]   = red;
2370 		im->green[col] = green;
2371 		im->blue[col]  = blue;
2372 		im->alpha[col]  = alpha;
2373 	} else {
2374 		RETURN_FALSE;
2375 	}
2376 }
2377 /* }}} */
2378 
2379 /* {{{ Get the colors for an index */
PHP_FUNCTION(imagecolorsforindex)2380 PHP_FUNCTION(imagecolorsforindex)
2381 {
2382 	zval *IM;
2383 	zend_long index;
2384 	int col;
2385 	gdImagePtr im;
2386 
2387 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &index) == FAILURE) {
2388 		RETURN_THROWS();
2389 	}
2390 
2391 	im = php_gd_libgdimageptr_from_zval_p(IM);
2392 
2393 	col = index;
2394 
2395 	if ((col >= 0 && gdImageTrueColor(im)) || (!gdImageTrueColor(im) && col >= 0 && col < gdImageColorsTotal(im))) {
2396 		array_init(return_value);
2397 
2398 		add_assoc_long(return_value,"red",  gdImageRed(im,col));
2399 		add_assoc_long(return_value,"green", gdImageGreen(im,col));
2400 		add_assoc_long(return_value,"blue", gdImageBlue(im,col));
2401 		add_assoc_long(return_value,"alpha", gdImageAlpha(im,col));
2402 	} else {
2403 		zend_argument_value_error(2, "is out of range");
2404 		RETURN_THROWS();
2405 	}
2406 }
2407 /* }}} */
2408 
2409 /* {{{ Apply a gamma correction to a GD image */
PHP_FUNCTION(imagegammacorrect)2410 PHP_FUNCTION(imagegammacorrect)
2411 {
2412 	zval *IM;
2413 	gdImagePtr im;
2414 	int i;
2415 	double input, output, gamma;
2416 
2417 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odd", &IM, gd_image_ce, &input, &output) == FAILURE) {
2418 		RETURN_THROWS();
2419 	}
2420 
2421 	if (input <= 0.0) {
2422 		zend_argument_value_error(2, "must be greater than 0");
2423 		RETURN_THROWS();
2424 	}
2425 
2426 	if (output <= 0.0) {
2427 		zend_argument_value_error(3, "must be greater than 0");
2428 		RETURN_THROWS();
2429 	}
2430 
2431 	gamma = input / output;
2432 
2433 	im = php_gd_libgdimageptr_from_zval_p(IM);
2434 
2435 	if (gdImageTrueColor(im))	{
2436 		int x, y, c;
2437 
2438 		for (y = 0; y < gdImageSY(im); y++)	{
2439 			for (x = 0; x < gdImageSX(im); x++)	{
2440 				c = gdImageGetPixel(im, x, y);
2441 				gdImageSetPixel(im, x, y,
2442 					gdTrueColorAlpha(
2443 						(int) ((pow((gdTrueColorGetRed(c)   / 255.0), gamma) * 255) + .5),
2444 						(int) ((pow((gdTrueColorGetGreen(c) / 255.0), gamma) * 255) + .5),
2445 						(int) ((pow((gdTrueColorGetBlue(c)  / 255.0), gamma) * 255) + .5),
2446 						gdTrueColorGetAlpha(c)
2447 					)
2448 				);
2449 			}
2450 		}
2451 		RETURN_TRUE;
2452 	}
2453 
2454 	for (i = 0; i < gdImageColorsTotal(im); i++) {
2455 		im->red[i]   = (int)((pow((im->red[i]   / 255.0), gamma) * 255) + .5);
2456 		im->green[i] = (int)((pow((im->green[i] / 255.0), gamma) * 255) + .5);
2457 		im->blue[i]  = (int)((pow((im->blue[i]  / 255.0), gamma) * 255) + .5);
2458 	}
2459 
2460 	RETURN_TRUE;
2461 }
2462 /* }}} */
2463 
2464 /* {{{ Set a single pixel */
PHP_FUNCTION(imagesetpixel)2465 PHP_FUNCTION(imagesetpixel)
2466 {
2467 	zval *IM;
2468 	zend_long x, y, col;
2469 	gdImagePtr im;
2470 
2471 	ZEND_PARSE_PARAMETERS_START(4, 4)
2472 		Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
2473 		Z_PARAM_LONG(x)
2474 		Z_PARAM_LONG(y)
2475 		Z_PARAM_LONG(col)
2476 	ZEND_PARSE_PARAMETERS_END();
2477 
2478 	im = php_gd_libgdimageptr_from_zval_p(IM);
2479 
2480 	gdImageSetPixel(im, x, y, col);
2481 	RETURN_TRUE;
2482 }
2483 /* }}} */
2484 
2485 /* {{{ Draw a line */
PHP_FUNCTION(imageline)2486 PHP_FUNCTION(imageline)
2487 {
2488 	zval *IM;
2489 	zend_long x1, y1, x2, y2, col;
2490 	gdImagePtr im;
2491 
2492 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce,  &x1, &y1, &x2, &y2, &col) == FAILURE) {
2493 		RETURN_THROWS();
2494 	}
2495 
2496 	im = php_gd_libgdimageptr_from_zval_p(IM);
2497 
2498 	if (im->AA) {
2499 		gdImageSetAntiAliased(im, col);
2500 		col = gdAntiAliased;
2501 	}
2502 	gdImageLine(im, x1, y1, x2, y2, col);
2503 	RETURN_TRUE;
2504 }
2505 /* }}} */
2506 
2507 /* {{{ Draw a dashed line */
PHP_FUNCTION(imagedashedline)2508 PHP_FUNCTION(imagedashedline)
2509 {
2510 	zval *IM;
2511 	zend_long x1, y1, x2, y2, col;
2512 	gdImagePtr im;
2513 
2514 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2515 		RETURN_THROWS();
2516 	}
2517 
2518 	im = php_gd_libgdimageptr_from_zval_p(IM);
2519 
2520 	gdImageDashedLine(im, x1, y1, x2, y2, col);
2521 	RETURN_TRUE;
2522 }
2523 /* }}} */
2524 
2525 /* {{{ Draw a rectangle */
PHP_FUNCTION(imagerectangle)2526 PHP_FUNCTION(imagerectangle)
2527 {
2528 	zval *IM;
2529 	zend_long x1, y1, x2, y2, col;
2530 	gdImagePtr im;
2531 
2532 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2533 		RETURN_THROWS();
2534 	}
2535 
2536 	im = php_gd_libgdimageptr_from_zval_p(IM);
2537 
2538 	gdImageRectangle(im, x1, y1, x2, y2, col);
2539 	RETURN_TRUE;
2540 }
2541 /* }}} */
2542 
2543 /* {{{ Draw a filled rectangle */
PHP_FUNCTION(imagefilledrectangle)2544 PHP_FUNCTION(imagefilledrectangle)
2545 {
2546 	zval *IM;
2547 	zend_long x1, y1, x2, y2, col;
2548 	gdImagePtr im;
2549 
2550 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2551 		RETURN_THROWS();
2552 	}
2553 
2554 	im = php_gd_libgdimageptr_from_zval_p(IM);
2555 	gdImageFilledRectangle(im, x1, y1, x2, y2, col);
2556 	RETURN_TRUE;
2557 }
2558 /* }}} */
2559 
2560 /* {{{ Draw a partial ellipse */
PHP_FUNCTION(imagearc)2561 PHP_FUNCTION(imagearc)
2562 {
2563 	zval *IM;
2564 	zend_long cx, cy, w, h, ST, E, col;
2565 	gdImagePtr im;
2566 	int e, st;
2567 
2568 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col) == FAILURE) {
2569 		RETURN_THROWS();
2570 	}
2571 
2572 	im = php_gd_libgdimageptr_from_zval_p(IM);
2573 
2574 	e = E;
2575 	if (e < 0) {
2576 		e %= 360;
2577 	}
2578 
2579 	st = ST;
2580 	if (st < 0) {
2581 		st %= 360;
2582 	}
2583 
2584 	gdImageArc(im, cx, cy, w, h, st, e, col);
2585 	RETURN_TRUE;
2586 }
2587 /* }}} */
2588 
2589 /* {{{ Draw an ellipse */
PHP_FUNCTION(imageellipse)2590 PHP_FUNCTION(imageellipse)
2591 {
2592 	zval *IM;
2593 	zend_long cx, cy, w, h, color;
2594 	gdImagePtr im;
2595 
2596 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce,  &cx, &cy, &w, &h, &color) == FAILURE) {
2597 		RETURN_THROWS();
2598 	}
2599 
2600 	im = php_gd_libgdimageptr_from_zval_p(IM);
2601 
2602 	gdImageEllipse(im, cx, cy, w, h, color);
2603 	RETURN_TRUE;
2604 }
2605 /* }}} */
2606 
2607 /* {{{ Flood fill to specific color */
PHP_FUNCTION(imagefilltoborder)2608 PHP_FUNCTION(imagefilltoborder)
2609 {
2610 	zval *IM;
2611 	zend_long x, y, border, col;
2612 	gdImagePtr im;
2613 
2614 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce,  &x, &y, &border, &col) == FAILURE) {
2615 		RETURN_THROWS();
2616 	}
2617 
2618 	im = php_gd_libgdimageptr_from_zval_p(IM);
2619 
2620 	gdImageFillToBorder(im, x, y, border, col);
2621 	RETURN_TRUE;
2622 }
2623 /* }}} */
2624 
2625 /* {{{ Flood fill */
PHP_FUNCTION(imagefill)2626 PHP_FUNCTION(imagefill)
2627 {
2628 	zval *IM;
2629 	zend_long x, y, col;
2630 	gdImagePtr im;
2631 
2632 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &x, &y, &col) == FAILURE) {
2633 		RETURN_THROWS();
2634 	}
2635 
2636 	im = php_gd_libgdimageptr_from_zval_p(IM);
2637 
2638 	gdImageFill(im, x, y, col);
2639 	RETURN_TRUE;
2640 }
2641 /* }}} */
2642 
2643 /* {{{ Find out the number of colors in an image's palette */
PHP_FUNCTION(imagecolorstotal)2644 PHP_FUNCTION(imagecolorstotal)
2645 {
2646 	zval *IM;
2647 	gdImagePtr im;
2648 
2649 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
2650 		RETURN_THROWS();
2651 	}
2652 
2653 	im = php_gd_libgdimageptr_from_zval_p(IM);
2654 
2655 	RETURN_LONG(gdImageColorsTotal(im));
2656 }
2657 /* }}} */
2658 
2659 /* {{{ Define a color as transparent */
PHP_FUNCTION(imagecolortransparent)2660 PHP_FUNCTION(imagecolortransparent)
2661 {
2662 	zval *IM;
2663 	zend_long COL = 0;
2664 	bool COL_IS_NULL = 1;
2665 	gdImagePtr im;
2666 
2667 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) {
2668 		RETURN_THROWS();
2669 	}
2670 
2671 	im = php_gd_libgdimageptr_from_zval_p(IM);
2672 
2673 	if (!COL_IS_NULL) {
2674 		gdImageColorTransparent(im, COL);
2675 	}
2676 
2677 	RETURN_LONG(gdImageGetTransparent(im));
2678 }
2679 /* }}} */
2680 
2681 /* {{{ Enable or disable interlace */
PHP_FUNCTION(imageinterlace)2682 PHP_FUNCTION(imageinterlace)
2683 {
2684 	zval *IM;
2685 	bool INT = 0;
2686 	bool INT_IS_NULL = 1;
2687 	gdImagePtr im;
2688 
2689 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) {
2690 		RETURN_THROWS();
2691 	}
2692 
2693 	im = php_gd_libgdimageptr_from_zval_p(IM);
2694 
2695 	if (!INT_IS_NULL) {
2696 		gdImageInterlace(im, INT);
2697 	}
2698 
2699 	RETURN_BOOL(gdImageGetInterlaced(im));
2700 }
2701 /* }}} */
2702 
2703 /* {{{ php_imagepolygon
2704    arg = -1 open polygon
2705    arg = 0  normal polygon
2706    arg = 1  filled polygon */
2707 /* im, points, num_points, col */
php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS,int filled)2708 static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
2709 {
2710 	zval *IM, *POINTS;
2711 	zend_long NPOINTS, COL;
2712 	bool COL_IS_NULL = 1;
2713 	zval *var = NULL;
2714 	gdImagePtr im;
2715 	gdPointPtr points;
2716 	int npoints, col, nelem, i;
2717 
2718 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l!", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL, &COL_IS_NULL) == FAILURE) {
2719 		RETURN_THROWS();
2720 	}
2721 	if (COL_IS_NULL) {
2722 		COL = NPOINTS;
2723 		NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
2724 		if (NPOINTS % 2 != 0) {
2725 			zend_argument_value_error(2, "must have an even number of elements");
2726 			RETURN_THROWS();
2727 		}
2728 		NPOINTS /= 2;
2729 	} else {
2730 		php_error_docref(NULL, E_DEPRECATED, "Using the $num_points parameter is deprecated");
2731 	}
2732 
2733 	im = php_gd_libgdimageptr_from_zval_p(IM);
2734 
2735 	npoints = NPOINTS;
2736 	col = COL;
2737 
2738 	nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
2739 	if (npoints < 3) {
2740 		zend_argument_value_error(3, "must be greater than or equal to 3");
2741 		RETURN_THROWS();
2742 	}
2743 
2744 	if (nelem < npoints * 2) {
2745 		zend_value_error("Trying to use %d points in array with only %d points", npoints, nelem/2);
2746 		RETURN_THROWS();
2747 	}
2748 
2749 	points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
2750 
2751 	for (i = 0; i < npoints; i++) {
2752 		if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2))) != NULL) {
2753 			points[i].x = zval_get_long(var);
2754 		}
2755 		if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2) + 1)) != NULL) {
2756 			points[i].y = zval_get_long(var);
2757 		}
2758 	}
2759 
2760 	if (im->AA) {
2761 		gdImageSetAntiAliased(im, col);
2762 		col = gdAntiAliased;
2763 	}
2764 	switch (filled) {
2765 		case -1:
2766 			gdImageOpenPolygon(im, points, npoints, col);
2767 			break;
2768 		case 0:
2769 			gdImagePolygon(im, points, npoints, col);
2770 			break;
2771 		case 1:
2772 			gdImageFilledPolygon(im, points, npoints, col);
2773 			break;
2774 	}
2775 
2776 	efree(points);
2777 	RETURN_TRUE;
2778 }
2779 /* }}} */
2780 
2781 /* {{{ Draw a polygon */
PHP_FUNCTION(imagepolygon)2782 PHP_FUNCTION(imagepolygon)
2783 {
2784 	php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2785 }
2786 /* }}} */
2787 
2788 /* {{{ Draw a polygon */
PHP_FUNCTION(imageopenpolygon)2789 PHP_FUNCTION(imageopenpolygon)
2790 {
2791 	php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1);
2792 }
2793 /* }}} */
2794 
2795 /* {{{ Draw a filled polygon */
PHP_FUNCTION(imagefilledpolygon)2796 PHP_FUNCTION(imagefilledpolygon)
2797 {
2798 	php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2799 }
2800 /* }}} */
2801 
2802 /* {{{ php_find_gd_font */
php_find_gd_font(zend_object * font_obj,zend_long font_int)2803 static gdFontPtr php_find_gd_font(zend_object *font_obj, zend_long font_int)
2804 {
2805 	if (font_obj) {
2806 		return php_gd_font_object_from_zend_object(font_obj)->font;
2807 	}
2808 
2809 	switch (font_int) {
2810 		case 1: return gdFontTiny;
2811 		case 2: return gdFontSmall;
2812 		case 3: return gdFontMediumBold;
2813 		case 4: return gdFontLarge;
2814 		case 5: return gdFontGiant;
2815 	}
2816 
2817 	return font_int < 1 ? gdFontTiny : gdFontGiant;
2818 }
2819 /* }}} */
2820 
2821 /* {{{ php_imagefontsize
2822  * arg = 0  ImageFontWidth
2823  * arg = 1  ImageFontHeight
2824  */
php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS,int arg)2825 static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg)
2826 {
2827 	zend_object *font_obj;
2828 	zend_long font_int;
2829 	gdFontPtr font;
2830 
2831 	ZEND_PARSE_PARAMETERS_START(1, 1)
2832 		Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int)
2833 	ZEND_PARSE_PARAMETERS_END();
2834 
2835 	font = php_find_gd_font(font_obj, font_int);
2836 	RETURN_LONG(arg ? font->h : font->w);
2837 }
2838 /* }}} */
2839 
2840 /* {{{ Get font width */
PHP_FUNCTION(imagefontwidth)2841 PHP_FUNCTION(imagefontwidth)
2842 {
2843 	php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2844 }
2845 /* }}} */
2846 
2847 /* {{{ Get font height */
PHP_FUNCTION(imagefontheight)2848 PHP_FUNCTION(imagefontheight)
2849 {
2850 	php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2851 }
2852 /* }}} */
2853 
2854 /* {{{ php_gdimagecharup
2855  * workaround for a bug in gd 1.2 */
php_gdimagecharup(gdImagePtr im,gdFontPtr f,int x,int y,int c,int color)2856 static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color)
2857 {
2858 	int cx, cy, px, py, fline;
2859 	cx = 0;
2860 	cy = 0;
2861 
2862 	if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
2863 		return;
2864 	}
2865 
2866 	fline = (c - f->offset) * f->h * f->w;
2867 	for (py = y; (py > (y - f->w)); py--) {
2868 		for (px = x; (px < (x + f->h)); px++) {
2869 			if (f->data[fline + cy * f->w + cx]) {
2870 				gdImageSetPixel(im, px, py, color);
2871 			}
2872 			cy++;
2873 		}
2874 		cy = 0;
2875 		cx++;
2876 	}
2877 }
2878 /* }}} */
2879 
2880 /* {{{ php_imagechar
2881  * arg = 0  ImageChar
2882  * arg = 1  ImageCharUp
2883  * arg = 2  ImageString
2884  * arg = 3  ImageStringUp
2885  */
php_imagechar(INTERNAL_FUNCTION_PARAMETERS,int mode)2886 static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode)
2887 {
2888 	zval *IM;
2889 	zend_long X, Y, COL;
2890 	char *C;
2891 	size_t C_len;
2892 	gdImagePtr im;
2893 	int ch = 0, col, x, y, i, l = 0;
2894 	unsigned char *str = NULL;
2895 	zend_object *font_obj;
2896 	zend_long font_int;
2897 	gdFontPtr font;
2898 
2899 	ZEND_PARSE_PARAMETERS_START(6, 6)
2900 		Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
2901 		Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int)
2902 		Z_PARAM_LONG(X)
2903 		Z_PARAM_LONG(Y)
2904 		Z_PARAM_STRING(C, C_len)
2905 		Z_PARAM_LONG(COL)
2906 	ZEND_PARSE_PARAMETERS_END();
2907 
2908 	im = php_gd_libgdimageptr_from_zval_p(IM);
2909 
2910 	col = COL;
2911 
2912 	if (mode < 2) {
2913 		ch = (int)((unsigned char)*C);
2914 	} else {
2915 		str = (unsigned char *) estrndup(C, C_len);
2916 		l = strlen((char *)str);
2917 	}
2918 
2919 	y = Y;
2920 	x = X;
2921 
2922 	font = php_find_gd_font(font_obj, font_int);
2923 
2924 	switch (mode) {
2925 		case 0:
2926 			gdImageChar(im, font, x, y, ch, col);
2927 			break;
2928 		case 1:
2929 			php_gdimagecharup(im, font, x, y, ch, col);
2930 			break;
2931 		case 2:
2932 			for (i = 0; (i < l); i++) {
2933 				gdImageChar(im, font, x, y, (int) ((unsigned char) str[i]), col);
2934 				x += font->w;
2935 			}
2936 			break;
2937 		case 3: {
2938 			for (i = 0; (i < l); i++) {
2939 				/* php_gdimagecharup(im, font, x, y, (int) str[i], col); */
2940 				gdImageCharUp(im, font, x, y, (int) str[i], col);
2941 				y -= font->w;
2942 			}
2943 			break;
2944 		}
2945 	}
2946 	if (str) {
2947 		efree(str);
2948 	}
2949 	RETURN_TRUE;
2950 }
2951 /* }}} */
2952 
2953 /* {{{ Draw a character */
PHP_FUNCTION(imagechar)2954 PHP_FUNCTION(imagechar)
2955 {
2956 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2957 }
2958 /* }}} */
2959 
2960 /* {{{ Draw a character rotated 90 degrees counter-clockwise */
PHP_FUNCTION(imagecharup)2961 PHP_FUNCTION(imagecharup)
2962 {
2963 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2964 }
2965 /* }}} */
2966 
2967 /* {{{ Draw a string horizontally */
PHP_FUNCTION(imagestring)2968 PHP_FUNCTION(imagestring)
2969 {
2970 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
2971 }
2972 /* }}} */
2973 
2974 /* {{{ Draw a string vertically - rotated 90 degrees counter-clockwise */
PHP_FUNCTION(imagestringup)2975 PHP_FUNCTION(imagestringup)
2976 {
2977 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
2978 }
2979 /* }}} */
2980 
2981 /* {{{ Copy part of an image */
PHP_FUNCTION(imagecopy)2982 PHP_FUNCTION(imagecopy)
2983 {
2984 	zval *SIM, *DIM;
2985 	zend_long SX, SY, SW, SH, DX, DY;
2986 	gdImagePtr im_dst, im_src;
2987 	int srcH, srcW, srcY, srcX, dstY, dstX;
2988 
2989 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH) == FAILURE) {
2990 		RETURN_THROWS();
2991 	}
2992 
2993 	im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
2994 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
2995 
2996 	srcX = SX;
2997 	srcY = SY;
2998 	srcH = SH;
2999 	srcW = SW;
3000 	dstX = DX;
3001 	dstY = DY;
3002 
3003 	gdImageCopy(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH);
3004 	RETURN_TRUE;
3005 }
3006 /* }}} */
3007 
3008 /* {{{ Merge one part of an image with another */
PHP_FUNCTION(imagecopymerge)3009 PHP_FUNCTION(imagecopymerge)
3010 {
3011 	zval *SIM, *DIM;
3012 	zend_long SX, SY, SW, SH, DX, DY, PCT;
3013 	gdImagePtr im_dst, im_src;
3014 	int srcH, srcW, srcY, srcX, dstY, dstX, pct;
3015 
3016 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOlllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
3017 		RETURN_THROWS();
3018 	}
3019 
3020 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3021 	im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3022 
3023 	srcX = SX;
3024 	srcY = SY;
3025 	srcH = SH;
3026 	srcW = SW;
3027 	dstX = DX;
3028 	dstY = DY;
3029 	pct  = PCT;
3030 
3031 	gdImageCopyMerge(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
3032 	RETURN_TRUE;
3033 }
3034 /* }}} */
3035 
3036 /* {{{ Merge one part of an image with another */
PHP_FUNCTION(imagecopymergegray)3037 PHP_FUNCTION(imagecopymergegray)
3038 {
3039 	zval *SIM, *DIM;
3040 	zend_long SX, SY, SW, SH, DX, DY, PCT;
3041 	gdImagePtr im_dst, im_src;
3042 	int srcH, srcW, srcY, srcX, dstY, dstX, pct;
3043 
3044 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOlllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
3045 		RETURN_THROWS();
3046 	}
3047 
3048 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3049 	im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3050 
3051 	srcX = SX;
3052 	srcY = SY;
3053 	srcH = SH;
3054 	srcW = SW;
3055 	dstX = DX;
3056 	dstY = DY;
3057 	pct  = PCT;
3058 
3059 	gdImageCopyMergeGray(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
3060 	RETURN_TRUE;
3061 }
3062 /* }}} */
3063 
3064 /* {{{ Copy and resize part of an image */
PHP_FUNCTION(imagecopyresized)3065 PHP_FUNCTION(imagecopyresized)
3066 {
3067 	zval *SIM, *DIM;
3068 	zend_long SX, SY, SW, SH, DX, DY, DW, DH;
3069 	gdImagePtr im_dst, im_src;
3070 	int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
3071 
3072 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
3073 		RETURN_THROWS();
3074 	}
3075 
3076 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3077 	im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3078 
3079 	srcX = SX;
3080 	srcY = SY;
3081 	srcH = SH;
3082 	srcW = SW;
3083 	dstX = DX;
3084 	dstY = DY;
3085 	dstH = DH;
3086 	dstW = DW;
3087 
3088 	if (dstW <= 0) {
3089 		zend_argument_value_error(3, "must be greater than 0");
3090 		RETURN_THROWS();
3091 	}
3092 
3093 	if (dstH <= 0) {
3094 		zend_argument_value_error(4, "must be greater than 0");
3095 		RETURN_THROWS();
3096 	}
3097 
3098 	if (srcW <= 0) {
3099 		zend_argument_value_error(5, "must be greater than 0");
3100 		RETURN_THROWS();
3101 	}
3102 
3103 	if (srcH <= 0) {
3104 		zend_argument_value_error(6, "must be greater than 0");
3105 		RETURN_THROWS();
3106 	}
3107 
3108 	gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
3109 	RETURN_TRUE;
3110 }
3111 /* }}} */
3112 
3113 /* {{{ Get image width */
PHP_FUNCTION(imagesx)3114 PHP_FUNCTION(imagesx)
3115 {
3116 	zval *IM;
3117 	gdImagePtr im;
3118 
3119 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
3120 		RETURN_THROWS();
3121 	}
3122 
3123 	im = php_gd_libgdimageptr_from_zval_p(IM);
3124 
3125 	RETURN_LONG(gdImageSX(im));
3126 }
3127 /* }}} */
3128 
3129 /* {{{ Get image height */
PHP_FUNCTION(imagesy)3130 PHP_FUNCTION(imagesy)
3131 {
3132 	zval *IM;
3133 	gdImagePtr im;
3134 
3135 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
3136 		RETURN_THROWS();
3137 	}
3138 
3139 	im = php_gd_libgdimageptr_from_zval_p(IM);
3140 
3141 	RETURN_LONG(gdImageSY(im));
3142 }
3143 /* }}} */
3144 
3145 /* {{{ Set the clipping rectangle. */
PHP_FUNCTION(imagesetclip)3146 PHP_FUNCTION(imagesetclip)
3147 {
3148 	zval *im_zval;
3149 	gdImagePtr im;
3150 	zend_long x1, y1, x2, y2;
3151 
3152 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &im_zval, gd_image_ce, &x1, &y1, &x2, &y2) == FAILURE) {
3153 		RETURN_THROWS();
3154 	}
3155 
3156 	im = php_gd_libgdimageptr_from_zval_p(im_zval);
3157 
3158 	gdImageSetClip(im, x1, y1, x2, y2);
3159 	RETURN_TRUE;
3160 }
3161 /* }}} */
3162 
3163 /* {{{ Get the clipping rectangle. */
PHP_FUNCTION(imagegetclip)3164 PHP_FUNCTION(imagegetclip)
3165 {
3166 	zval *im_zval;
3167 	gdImagePtr im;
3168 	int x1, y1, x2, y2;
3169 
3170 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &im_zval, gd_image_ce) == FAILURE) {
3171 		RETURN_THROWS();
3172 	}
3173 
3174 	im = php_gd_libgdimageptr_from_zval_p(im_zval);
3175 
3176 	gdImageGetClip(im, &x1, &y1, &x2, &y2);
3177 
3178 	array_init(return_value);
3179 	add_next_index_long(return_value, x1);
3180 	add_next_index_long(return_value, y1);
3181 	add_next_index_long(return_value, x2);
3182 	add_next_index_long(return_value, y2);
3183 }
3184 /* }}} */
3185 
3186 #define TTFTEXT_DRAW 0
3187 #define TTFTEXT_BBOX 1
3188 
3189 #ifdef HAVE_GD_FREETYPE
3190 /* {{{ Give the bounding box of a text using fonts via freetype2 */
PHP_FUNCTION(imageftbbox)3191 PHP_FUNCTION(imageftbbox)
3192 {
3193 	php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX);
3194 }
3195 /* }}} */
3196 
3197 /* {{{ Write text to the image using fonts via freetype2 */
PHP_FUNCTION(imagefttext)3198 PHP_FUNCTION(imagefttext)
3199 {
3200 	php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW);
3201 }
3202 /* }}} */
3203 
3204 /* {{{ php_imagettftext_common */
php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS,int mode)3205 static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int mode)
3206 {
3207 	zval *IM, *EXT = NULL;
3208 	gdImagePtr im=NULL;
3209 	zend_long col = -1, x = 0, y = 0;
3210 	size_t str_len, fontname_len;
3211 	int i, brect[8];
3212 	double ptsize, angle;
3213 	char *str = NULL, *fontname = NULL;
3214 	char *error = NULL;
3215 	gdFTStringExtra strex = {0};
3216 
3217 	if (mode == TTFTEXT_BBOX) {
3218 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "ddss|a", &ptsize, &angle, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
3219 			RETURN_THROWS();
3220 		}
3221 	} else {
3222 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oddlllss|a", &IM, gd_image_ce, &ptsize, &angle, &x, &y, &col, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
3223 			RETURN_THROWS();
3224 		}
3225 		im = php_gd_libgdimageptr_from_zval_p(IM);
3226 	}
3227 
3228 	/* convert angle to radians */
3229 	angle = angle * (M_PI/180);
3230 
3231 	if (EXT) {	/* parse extended info */
3232 		zval *item;
3233 		zend_string *key;
3234 
3235 		/* walk the assoc array */
3236 		ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(EXT), key, item) {
3237 			if (key == NULL) {
3238 				continue;
3239 			}
3240 			if (zend_string_equals_literal(key, "linespacing")) {
3241 				strex.flags |= gdFTEX_LINESPACE;
3242 				strex.linespacing = zval_get_double(item);
3243 			}
3244 		} ZEND_HASH_FOREACH_END();
3245 	}
3246 
3247 #ifdef VIRTUAL_DIR
3248 	{
3249 		char tmp_font_path[MAXPATHLEN];
3250 
3251 		if (!VCWD_REALPATH(fontname, tmp_font_path)) {
3252 			fontname = NULL;
3253 		}
3254 	}
3255 #endif /* VIRTUAL_DIR */
3256 
3257 	PHP_GD_CHECK_OPEN_BASEDIR(fontname, "Invalid font filename");
3258 
3259 	if (EXT) {
3260 		error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
3261 	} else {
3262 		error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
3263 	}
3264 
3265 	if (error) {
3266 		php_error_docref(NULL, E_WARNING, "%s", error);
3267 		RETURN_FALSE;
3268 	}
3269 
3270 	array_init(return_value);
3271 
3272 	/* return array with the text's bounding box */
3273 	for (i = 0; i < 8; i++) {
3274 		add_next_index_long(return_value, brect[i]);
3275 	}
3276 }
3277 /* }}} */
3278 #endif /* HAVE_GD_FREETYPE */
3279 
3280 /* Section Filters */
3281 #define PHP_GD_SINGLE_RES	\
3282 	zval *SIM;	\
3283 	gdImagePtr im_src;	\
3284 	if (zend_parse_parameters(1, "O", &SIM, gd_image_ce) == FAILURE) {	\
3285 		RETURN_THROWS();	\
3286 	}	\
3287 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3288 
php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)3289 static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
3290 {
3291 	PHP_GD_SINGLE_RES
3292 
3293 	if (gdImageNegate(im_src) == 1) {
3294 		RETURN_TRUE;
3295 	}
3296 
3297 	RETURN_FALSE;
3298 }
3299 
php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)3300 static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
3301 {
3302 	PHP_GD_SINGLE_RES
3303 
3304 	if (gdImageGrayScale(im_src) == 1) {
3305 		RETURN_TRUE;
3306 	}
3307 
3308 	RETURN_FALSE;
3309 }
3310 
php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)3311 static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
3312 {
3313 	zval *SIM;
3314 	gdImagePtr im_src;
3315 	zend_long brightness, tmp;
3316 
3317 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &SIM, gd_image_ce, &tmp, &brightness) == FAILURE) {
3318 		RETURN_THROWS();
3319 	}
3320 
3321 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3322 
3323 	if (gdImageBrightness(im_src, (int)brightness) == 1) {
3324 		RETURN_TRUE;
3325 	}
3326 
3327 	RETURN_FALSE;
3328 }
3329 
php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)3330 static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
3331 {
3332 	zval *SIM;
3333 	gdImagePtr im_src;
3334 	zend_long contrast, tmp;
3335 
3336 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &SIM, gd_image_ce, &tmp, &contrast) == FAILURE) {
3337 		RETURN_THROWS();
3338 	}
3339 
3340 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3341 
3342 	if (gdImageContrast(im_src, (int)contrast) == 1) {
3343 		RETURN_TRUE;
3344 	}
3345 
3346 	RETURN_FALSE;
3347 }
3348 
php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)3349 static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
3350 {
3351 	zval *SIM;
3352 	gdImagePtr im_src;
3353 	zend_long r,g,b,tmp;
3354 	zend_long a = 0;
3355 
3356 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll|l", &SIM, gd_image_ce, &tmp, &r, &g, &b, &a) == FAILURE) {
3357 		RETURN_THROWS();
3358 	}
3359 
3360 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3361 
3362 	if (gdImageColor(im_src, (int) r, (int) g, (int) b, (int) a) == 1) {
3363 		RETURN_TRUE;
3364 	}
3365 
3366 	RETURN_FALSE;
3367 }
3368 
php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)3369 static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
3370 {
3371 	PHP_GD_SINGLE_RES
3372 
3373 	if (gdImageEdgeDetectQuick(im_src) == 1) {
3374 		RETURN_TRUE;
3375 	}
3376 
3377 	RETURN_FALSE;
3378 }
3379 
php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)3380 static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
3381 {
3382 	PHP_GD_SINGLE_RES
3383 
3384 	if (gdImageEmboss(im_src) == 1) {
3385 		RETURN_TRUE;
3386 	}
3387 
3388 	RETURN_FALSE;
3389 }
3390 
php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)3391 static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
3392 {
3393 	PHP_GD_SINGLE_RES
3394 
3395 	if (gdImageGaussianBlur(im_src) == 1) {
3396 		RETURN_TRUE;
3397 	}
3398 
3399 	RETURN_FALSE;
3400 }
3401 
php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)3402 static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
3403 {
3404 	PHP_GD_SINGLE_RES
3405 
3406 	if (gdImageSelectiveBlur(im_src) == 1) {
3407 		RETURN_TRUE;
3408 	}
3409 
3410 	RETURN_FALSE;
3411 }
3412 
php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)3413 static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
3414 {
3415 	PHP_GD_SINGLE_RES
3416 
3417 	if (gdImageMeanRemoval(im_src) == 1) {
3418 		RETURN_TRUE;
3419 	}
3420 
3421 	RETURN_FALSE;
3422 }
3423 
php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)3424 static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
3425 {
3426 	zval *SIM;
3427 	zend_long tmp;
3428 	gdImagePtr im_src;
3429 	double weight;
3430 
3431 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Old", &SIM, gd_image_ce, &tmp, &weight) == FAILURE) {
3432 		RETURN_THROWS();
3433 	}
3434 
3435 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3436 
3437 	if (gdImageSmooth(im_src, (float)weight)==1) {
3438 		RETURN_TRUE;
3439 	}
3440 
3441 	RETURN_FALSE;
3442 }
3443 
php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)3444 static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)
3445 {
3446 	zval *IM;
3447 	gdImagePtr im;
3448 	zend_long tmp, blocksize;
3449 	bool mode = 0;
3450 
3451 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll|b", &IM, gd_image_ce, &tmp, &blocksize, &mode) == FAILURE) {
3452 		RETURN_THROWS();
3453 	}
3454 
3455 	im = php_gd_libgdimageptr_from_zval_p(IM);
3456 
3457 	if (gdImagePixelate(im, (int) blocksize, (const unsigned int) mode)) {
3458 		RETURN_TRUE;
3459 	}
3460 
3461 	RETURN_FALSE;
3462 }
3463 
php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)3464 static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
3465 {
3466 	zval *IM;
3467 	zval *hash_colors = NULL;
3468 	gdImagePtr im;
3469 	zend_long tmp;
3470 	zend_long scatter_sub, scatter_plus;
3471 
3472 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll|a", &IM, gd_image_ce, &tmp, &scatter_sub, &scatter_plus, &hash_colors) == FAILURE) {
3473 		RETURN_THROWS();
3474 	}
3475 
3476 	im = php_gd_libgdimageptr_from_zval_p(IM);
3477 
3478 	if (hash_colors) {
3479 		uint32_t i = 0;
3480 		uint32_t num_colors = zend_hash_num_elements(Z_ARRVAL_P(hash_colors));
3481 		zval *color;
3482 		int *colors;
3483 
3484 		if (num_colors == 0) {
3485 			RETURN_BOOL(gdImageScatter(im, (int)scatter_sub, (int)scatter_plus));
3486 		}
3487 
3488 		colors = emalloc(num_colors * sizeof(int));
3489 
3490 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hash_colors), color) {
3491 			*(colors + i++) = (int) zval_get_long(color);
3492 		} ZEND_HASH_FOREACH_END();
3493 
3494 		RETVAL_BOOL(gdImageScatterColor(im, (int)scatter_sub, (int)scatter_plus, colors, num_colors));
3495 
3496 		efree(colors);
3497 	} else {
3498 		RETURN_BOOL(gdImageScatter(im, (int) scatter_sub, (int) scatter_plus));
3499 	}
3500 }
3501 
3502 /* {{{ Applies Filter an image using a custom angle */
PHP_FUNCTION(imagefilter)3503 PHP_FUNCTION(imagefilter)
3504 {
3505 	zval *tmp;
3506 
3507 	typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
3508 	zend_long filtertype;
3509 	image_filter filters[] =
3510 	{
3511 		php_image_filter_negate ,
3512 		php_image_filter_grayscale,
3513 		php_image_filter_brightness,
3514 		php_image_filter_contrast,
3515 		php_image_filter_colorize,
3516 		php_image_filter_edgedetect,
3517 		php_image_filter_emboss,
3518 		php_image_filter_gaussian_blur,
3519 		php_image_filter_selective_blur,
3520 		php_image_filter_mean_removal,
3521 		php_image_filter_smooth,
3522 		php_image_filter_pixelate,
3523 		php_image_filter_scatter
3524 	};
3525 
3526 	if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > IMAGE_FILTER_MAX_ARGS) {
3527 		WRONG_PARAM_COUNT;
3528 	} else if (zend_parse_parameters(2, "Ol", &tmp, gd_image_ce, &filtertype) == FAILURE) {
3529 		RETURN_THROWS();
3530 	}
3531 
3532 	if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
3533 		filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
3534 	}
3535 }
3536 /* }}} */
3537 
3538 /* {{{ Apply a 3x3 convolution matrix, using coefficient div and offset */
PHP_FUNCTION(imageconvolution)3539 PHP_FUNCTION(imageconvolution)
3540 {
3541 	zval *SIM, *hash_matrix;
3542 	zval *var = NULL, *var2 = NULL;
3543 	gdImagePtr im_src = NULL;
3544 	double div, offset;
3545 	int nelem, i, j, res;
3546 	float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
3547 
3548 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oadd", &SIM, gd_image_ce, &hash_matrix, &div, &offset) == FAILURE) {
3549 		RETURN_THROWS();
3550 	}
3551 
3552 	im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3553 
3554 	nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
3555 	if (nelem != 3) {
3556 		zend_argument_value_error(2, "must be a 3x3 array");
3557 		RETURN_THROWS();
3558 	}
3559 
3560 	for (i=0; i<3; i++) {
3561 		if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) {
3562 			if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) {
3563 				zend_argument_value_error(2, "must be a 3x3 array, matrix[%d] only has %d elements", i, zend_hash_num_elements(Z_ARRVAL_P(var)));
3564 				RETURN_THROWS();
3565 			}
3566 
3567 			for (j=0; j<3; j++) {
3568 				if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) {
3569 					matrix[i][j] = (float) zval_get_double(var2);
3570 				} else {
3571 					zend_argument_value_error(2, "must be a 3x3 array, matrix[%d][%d] cannot be found (missing integer key)", i, j);
3572 					RETURN_THROWS();
3573 				}
3574 			}
3575 		}
3576 	}
3577 	res = gdImageConvolution(im_src, matrix, (float)div, (float)offset);
3578 
3579 	if (res) {
3580 		RETURN_TRUE;
3581 	} else {
3582 		RETURN_FALSE;
3583 	}
3584 }
3585 /* }}} */
3586 /* End section: Filters */
3587 
3588 /* {{{ Flip an image (in place) horizontally, vertically or both directions. */
PHP_FUNCTION(imageflip)3589 PHP_FUNCTION(imageflip)
3590 {
3591 	zval *IM;
3592 	zend_long mode;
3593 	gdImagePtr im;
3594 
3595 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &mode) == FAILURE)  {
3596 		RETURN_THROWS();
3597 	}
3598 
3599 	im = php_gd_libgdimageptr_from_zval_p(IM);
3600 
3601 	switch (mode) {
3602 		case PHP_GD_FLIP_VERTICAL:
3603 			gdImageFlipVertical(im);
3604 			break;
3605 
3606 		case PHP_GD_FLIP_HORIZONTAL:
3607 			gdImageFlipHorizontal(im);
3608 			break;
3609 
3610 		case PHP_GD_FLIP_BOTH:
3611 			gdImageFlipBoth(im);
3612 			break;
3613 
3614 		default:
3615 			zend_argument_value_error(2, "must be one of IMG_FLIP_VERTICAL, IMG_FLIP_HORIZONTAL, or IMG_FLIP_BOTH");
3616 			RETURN_THROWS();
3617 	}
3618 
3619 	RETURN_TRUE;
3620 }
3621 /* }}} */
3622 
3623 /* {{{ Should antialiased functions used or not*/
PHP_FUNCTION(imageantialias)3624 PHP_FUNCTION(imageantialias)
3625 {
3626 	zval *IM;
3627 	bool alias;
3628 	gdImagePtr im;
3629 
3630 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &alias) == FAILURE) {
3631 		RETURN_THROWS();
3632 	}
3633 
3634 	im = php_gd_libgdimageptr_from_zval_p(IM);
3635 	if (im->trueColor) {
3636 		im->AA = alias;
3637 	}
3638 
3639 	RETURN_TRUE;
3640 }
3641 /* }}} */
3642 
3643 /* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
PHP_FUNCTION(imagecrop)3644 PHP_FUNCTION(imagecrop)
3645 {
3646 	zval *IM;
3647 	gdImagePtr im;
3648 	gdImagePtr im_crop;
3649 	gdRect rect;
3650 	zval *z_rect;
3651 	zval *tmp;
3652 
3653 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &z_rect) == FAILURE)  {
3654 		RETURN_THROWS();
3655 	}
3656 
3657 	im = php_gd_libgdimageptr_from_zval_p(IM);
3658 
3659 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) {
3660 		rect.x = zval_get_long(tmp);
3661 	} else {
3662 		zend_argument_value_error(2, "must have an \"x\" key");
3663 		RETURN_THROWS();
3664 	}
3665 
3666 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
3667 		rect.y = zval_get_long(tmp);
3668 	} else {
3669 		zend_argument_value_error(2, "must have a \"y\" key");
3670 		RETURN_THROWS();
3671 	}
3672 
3673 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
3674 		rect.width = zval_get_long(tmp);
3675 	} else {
3676 		zend_argument_value_error(2, "must have a \"width\" key");
3677 		RETURN_THROWS();
3678 	}
3679 
3680 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
3681 		rect.height = zval_get_long(tmp);
3682 	} else {
3683 		zend_argument_value_error(2, "must have a \"height\" key");
3684 		RETURN_THROWS();
3685 	}
3686 
3687 	im_crop = gdImageCrop(im, &rect);
3688 
3689 	if (im_crop == NULL) {
3690 		RETURN_FALSE;
3691 	}
3692 
3693 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_crop);
3694 }
3695 /* }}} */
3696 
3697 /* {{{ Crop an image automatically using one of the available modes. */
PHP_FUNCTION(imagecropauto)3698 PHP_FUNCTION(imagecropauto)
3699 {
3700 	zval *IM;
3701 	zend_long mode = GD_CROP_DEFAULT;
3702 	zend_long color = -1;
3703 	double threshold = 0.5f;
3704 	gdImagePtr im;
3705 	gdImagePtr im_crop;
3706 
3707 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ldl", &IM, gd_image_ce, &mode, &threshold, &color) == FAILURE)  {
3708 		RETURN_THROWS();
3709 	}
3710 
3711 	im = php_gd_libgdimageptr_from_zval_p(IM);
3712 
3713 	switch (mode) {
3714 		case GD_CROP_DEFAULT:
3715 		case GD_CROP_TRANSPARENT:
3716 		case GD_CROP_BLACK:
3717 		case GD_CROP_WHITE:
3718 		case GD_CROP_SIDES:
3719 			im_crop = gdImageCropAuto(im, mode);
3720 			break;
3721 
3722 		case GD_CROP_THRESHOLD:
3723 			if (color < 0 || (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im))) {
3724 				zend_argument_value_error(4, "must be greater than or equal to 0 when using the threshold mode");
3725 				RETURN_THROWS();
3726 			}
3727 			im_crop = gdImageCropThreshold(im, color, (float) threshold);
3728 			break;
3729 
3730 		default:
3731 			zend_argument_value_error(2, "must be a valid mode");
3732 			RETURN_THROWS();
3733 	}
3734 
3735 	if (im_crop == NULL) {
3736 		RETURN_FALSE;
3737 	}
3738 
3739 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_crop);
3740 }
3741 /* }}} */
3742 
3743 /* {{{ Scale an image using the given new width and height. */
PHP_FUNCTION(imagescale)3744 PHP_FUNCTION(imagescale)
3745 {
3746 	zval *IM;
3747 	gdImagePtr im;
3748 	gdImagePtr im_scaled = NULL;
3749 	int new_width, new_height;
3750 	zend_long tmp_w, tmp_h=-1, tmp_m = GD_BILINEAR_FIXED;
3751 	gdInterpolationMethod method, old_method;
3752 
3753 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|ll", &IM, gd_image_ce, &tmp_w, &tmp_h, &tmp_m) == FAILURE)  {
3754 		RETURN_THROWS();
3755 	}
3756 	method = tmp_m;
3757 
3758 	im = php_gd_libgdimageptr_from_zval_p(IM);
3759 
3760 	if (tmp_h < 0 || tmp_w < 0) {
3761 		/* preserve ratio */
3762 		long src_x, src_y;
3763 
3764 		src_x = gdImageSX(im);
3765 		src_y = gdImageSY(im);
3766 
3767 		if (src_x && tmp_h < 0) {
3768 			tmp_h = tmp_w * src_y / src_x;
3769 		}
3770 		if (src_y && tmp_w < 0) {
3771 			tmp_w = tmp_h * src_x / src_y;
3772 		}
3773 	}
3774 
3775 	if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) {
3776 		RETURN_FALSE;
3777 	}
3778 
3779 	new_width = tmp_w;
3780 	new_height = tmp_h;
3781 
3782 	/* gdImageGetInterpolationMethod() is only available as of GD 2.1.1 */
3783 	old_method = im->interpolation_id;
3784 	if (gdImageSetInterpolationMethod(im, method)) {
3785 		im_scaled = gdImageScale(im, new_width, new_height);
3786 	}
3787 	gdImageSetInterpolationMethod(im, old_method);
3788 
3789 	if (im_scaled == NULL) {
3790 		RETURN_FALSE;
3791 	}
3792 
3793 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_scaled);
3794 }
3795 /* }}} */
3796 
3797 /* {{{ Return an image containing the affine tramsformed src image, using an optional clipping area */
PHP_FUNCTION(imageaffine)3798 PHP_FUNCTION(imageaffine)
3799 {
3800 	zval *IM;
3801 	gdImagePtr src;
3802 	gdImagePtr dst;
3803 	gdRect rect;
3804 	gdRectPtr pRect = NULL;
3805 	zval *z_rect = NULL;
3806 	zval *z_affine;
3807 	zval *tmp;
3808 	double affine[6];
3809 	int i, nelems;
3810 	zval *zval_affine_elem = NULL;
3811 
3812 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a!", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE)  {
3813 		RETURN_THROWS();
3814 	}
3815 
3816 	src = php_gd_libgdimageptr_from_zval_p(IM);
3817 
3818 	if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
3819 		zend_argument_value_error(2, "must have 6 elements");
3820 		RETURN_THROWS();
3821 	}
3822 
3823 	for (i = 0; i < nelems; i++) {
3824 		if ((zval_affine_elem = zend_hash_index_find(Z_ARRVAL_P(z_affine), i)) != NULL) {
3825 			switch (Z_TYPE_P(zval_affine_elem)) {
3826 				case IS_LONG:
3827 					affine[i]  = Z_LVAL_P(zval_affine_elem);
3828 					break;
3829 				case IS_DOUBLE:
3830 					affine[i] = Z_DVAL_P(zval_affine_elem);
3831 					break;
3832 				case IS_STRING:
3833 					affine[i] = zval_get_double(zval_affine_elem);
3834 					break;
3835 				default:
3836 					zend_argument_type_error(3, "contains invalid type for element %i", i);
3837 					RETURN_THROWS();
3838 			}
3839 		}
3840 	}
3841 
3842 	if (z_rect != NULL) {
3843 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") - 1)) != NULL) {
3844 			rect.x = zval_get_long(tmp);
3845 		} else {
3846 			zend_argument_value_error(3, "must have an \"x\" key");
3847 			RETURN_THROWS();
3848 		}
3849 
3850 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
3851 			rect.y = zval_get_long(tmp);
3852 		} else {
3853 			zend_argument_value_error(3, "must have a \"y\" key");
3854 			RETURN_THROWS();
3855 		}
3856 
3857 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
3858 			rect.width = zval_get_long(tmp);
3859 		} else {
3860 			zend_argument_value_error(3, "must have a \"width\" key");
3861 			RETURN_THROWS();
3862 		}
3863 
3864 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
3865 			rect.height = zval_get_long(tmp);
3866 		} else {
3867 			zend_argument_value_error(3, "must have a \"height\" key");
3868 			RETURN_THROWS();
3869 		}
3870 		pRect = &rect;
3871 	}
3872 
3873 	if (gdTransformAffineGetImage(&dst, src, pRect, affine) != GD_TRUE) {
3874 		RETURN_FALSE;
3875 	}
3876 
3877 	if (dst == NULL) {
3878 		RETURN_FALSE;
3879 	}
3880 
3881 	php_gd_assign_libgdimageptr_as_extgdimage(return_value, dst);
3882 }
3883 /* }}} */
3884 
3885 /* {{{ Return an image containing the affine tramsformed src image, using an optional clipping area */
PHP_FUNCTION(imageaffinematrixget)3886 PHP_FUNCTION(imageaffinematrixget)
3887 {
3888 	double affine[6];
3889 	zend_long type;
3890 	zval *options = NULL;
3891 	zval *tmp;
3892 	int res = GD_FALSE, i;
3893 
3894 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &type, &options) == FAILURE)  {
3895 		RETURN_THROWS();
3896 	}
3897 
3898 	switch((gdAffineStandardMatrix)type) {
3899 		case GD_AFFINE_TRANSLATE:
3900 		case GD_AFFINE_SCALE: {
3901 			double x, y;
3902 			if (Z_TYPE_P(options) != IS_ARRAY) {
3903 				zend_argument_type_error(1, "must be of type array when using translate or scale");
3904 				RETURN_THROWS();
3905 			}
3906 
3907 			if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) {
3908 				x = zval_get_double(tmp);
3909 			} else {
3910 				zend_argument_value_error(2, "must have an \"x\" key");
3911 				RETURN_THROWS();
3912 			}
3913 
3914 			if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) {
3915 				y = zval_get_double(tmp);
3916 			} else {
3917 				zend_argument_value_error(2, "must have a \"y\" key");
3918 				RETURN_THROWS();
3919 			}
3920 
3921 			if (type == GD_AFFINE_TRANSLATE) {
3922 				res = gdAffineTranslate(affine, x, y);
3923 			} else {
3924 				res = gdAffineScale(affine, x, y);
3925 			}
3926 			break;
3927 		}
3928 
3929 		case GD_AFFINE_ROTATE:
3930 		case GD_AFFINE_SHEAR_HORIZONTAL:
3931 		case GD_AFFINE_SHEAR_VERTICAL: {
3932 			double angle;
3933 
3934 			angle = zval_get_double(options);
3935 
3936 			if (type == GD_AFFINE_SHEAR_HORIZONTAL) {
3937 				res = gdAffineShearHorizontal(affine, angle);
3938 			} else if (type == GD_AFFINE_SHEAR_VERTICAL) {
3939 				res = gdAffineShearVertical(affine, angle);
3940 			} else {
3941 				res = gdAffineRotate(affine, angle);
3942 			}
3943 			break;
3944 		}
3945 
3946 		default:
3947 			zend_argument_value_error(1, "must be a valid element type");
3948 			RETURN_THROWS();
3949 	}
3950 
3951 	if (res == GD_FALSE) {
3952 		RETURN_FALSE;
3953 	} else {
3954 		array_init(return_value);
3955 		for (i = 0; i < 6; i++) {
3956 			add_index_double(return_value, i, affine[i]);
3957 		}
3958 	}
3959 } /* }}} */
3960 
3961 /* {{{ Concat two matrices (as in doing many ops in one go) */
PHP_FUNCTION(imageaffinematrixconcat)3962 PHP_FUNCTION(imageaffinematrixconcat)
3963 {
3964 	double m1[6];
3965 	double m2[6];
3966 	double mr[6];
3967 
3968 	zval *tmp;
3969 	zval *z_m1;
3970 	zval *z_m2;
3971 	int i;
3972 
3973 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &z_m1, &z_m2) == FAILURE)  {
3974 		RETURN_THROWS();
3975 	}
3976 
3977 	if (zend_hash_num_elements(Z_ARRVAL_P(z_m1)) != 6) {
3978 		zend_argument_value_error(1, "must have 6 elements");
3979 		RETURN_THROWS();
3980 	}
3981 
3982 	if (zend_hash_num_elements(Z_ARRVAL_P(z_m2)) != 6) {
3983 		zend_argument_value_error(1, "must have 6 elements");
3984 		RETURN_THROWS();
3985 	}
3986 
3987 	for (i = 0; i < 6; i++) {
3988 		if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m1), i)) != NULL) {
3989 			switch (Z_TYPE_P(tmp)) {
3990 				case IS_LONG:
3991 					m1[i]  = Z_LVAL_P(tmp);
3992 					break;
3993 				case IS_DOUBLE:
3994 					m1[i] = Z_DVAL_P(tmp);
3995 					break;
3996 				case IS_STRING:
3997 					m1[i] = zval_get_double(tmp);
3998 					break;
3999 				default:
4000 					zend_argument_type_error(1, "contains invalid type for element %i", i);
4001 					RETURN_THROWS();
4002 			}
4003 		}
4004 
4005 		if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m2), i)) != NULL) {
4006 			switch (Z_TYPE_P(tmp)) {
4007 				case IS_LONG:
4008 					m2[i]  = Z_LVAL_P(tmp);
4009 					break;
4010 				case IS_DOUBLE:
4011 					m2[i] = Z_DVAL_P(tmp);
4012 					break;
4013 				case IS_STRING:
4014 					m2[i] = zval_get_double(tmp);
4015 					break;
4016 				default:
4017 					zend_argument_type_error(2, "contains invalid type for element %i", i);
4018 					RETURN_THROWS();
4019 			}
4020 		}
4021 	}
4022 
4023 	if (gdAffineConcat (mr, m1, m2) != GD_TRUE) {
4024 		RETURN_FALSE;
4025 	}
4026 
4027 	array_init(return_value);
4028 	for (i = 0; i < 6; i++) {
4029 		add_index_double(return_value, i, mr[i]);
4030 	}
4031 } /* }}} */
4032 
4033 /* {{{ Get the default interpolation method. */
PHP_FUNCTION(imagegetinterpolation)4034 PHP_FUNCTION(imagegetinterpolation)
4035 {
4036 	zval *IM;
4037 	gdImagePtr im;
4038 
4039 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE)  {
4040 		RETURN_THROWS();
4041 	}
4042 	im = php_gd_libgdimageptr_from_zval_p(IM);
4043 
4044 #ifdef HAVE_GD_GET_INTERPOLATION
4045 	RETURN_LONG(gdImageGetInterpolationMethod(im));
4046 #else
4047 	RETURN_LONG(im->interpolation_id);
4048 #endif
4049 }
4050 /* }}} */
4051 
4052 /* {{{ Set the default interpolation method, passing -1 or 0 sets it to the libgd default (bilinear). */
PHP_FUNCTION(imagesetinterpolation)4053 PHP_FUNCTION(imagesetinterpolation)
4054 {
4055 	zval *IM;
4056 	gdImagePtr im;
4057 	zend_long method = GD_BILINEAR_FIXED;
4058 
4059 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &IM, gd_image_ce, &method) == FAILURE)  {
4060 		RETURN_THROWS();
4061 	}
4062 
4063 	im = php_gd_libgdimageptr_from_zval_p(IM);
4064 
4065 	if (method == -1) {
4066 		 method = GD_BILINEAR_FIXED;
4067 	}
4068 	RETURN_BOOL(gdImageSetInterpolationMethod(im, (gdInterpolationMethod) method));
4069 }
4070 /* }}} */
4071 
4072 /* {{{ Get or set the resolution of the image in DPI. */
PHP_FUNCTION(imageresolution)4073 PHP_FUNCTION(imageresolution)
4074 {
4075 	zval *IM;
4076 	gdImagePtr im;
4077 	zend_long res_x, res_y;
4078 	bool res_x_is_null = 1, res_y_is_null = 1;
4079 
4080 	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)  {
4081 		RETURN_THROWS();
4082 	}
4083 
4084 	im = php_gd_libgdimageptr_from_zval_p(IM);
4085 
4086 	if (!res_x_is_null && !res_y_is_null) {
4087 		gdImageSetResolution(im, res_x, res_y);
4088 		RETURN_TRUE;
4089 	} else if (!res_x_is_null && res_y_is_null) {
4090 		gdImageSetResolution(im, res_x, res_x);
4091 		RETURN_TRUE;
4092 	} else if (res_x_is_null && !res_y_is_null) {
4093 		gdImageSetResolution(im, res_y, res_y);
4094 		RETURN_TRUE;
4095 	}
4096 
4097 	array_init(return_value);
4098 	add_next_index_long(return_value, gdImageResolutionX(im));
4099 	add_next_index_long(return_value, gdImageResolutionY(im));
4100 }
4101 /* }}} */
4102 
4103 
4104 /*********************************************************
4105  *
4106  * Stream Handling
4107  * Formerly contained within ext/gd/gd_ctx.c and included
4108  * at the the top of this file
4109  *
4110  ********************************************************/
4111 
4112 #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
4113 
_php_image_output_putc(struct gdIOCtx * ctx,int c)4114 static void _php_image_output_putc(struct gdIOCtx *ctx, int c) /* {{{ */
4115 {
4116 	/* without the following downcast, the write will fail
4117 	 * (i.e., will write a zero byte) for all
4118 	 * big endian architectures:
4119 	 */
4120 	unsigned char ch = (unsigned char) c;
4121 	php_write(&ch, 1);
4122 } /* }}} */
4123 
_php_image_output_putbuf(struct gdIOCtx * ctx,const void * buf,int l)4124 static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
4125 {
4126 	return php_write((void *)buf, l);
4127 } /* }}} */
4128 
_php_image_output_ctxfree(struct gdIOCtx * ctx)4129 static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
4130 {
4131 	if(ctx) {
4132 		efree(ctx);
4133 	}
4134 } /* }}} */
4135 
_php_image_stream_putc(struct gdIOCtx * ctx,int c)4136 static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
4137 	char ch = (char) c;
4138 	php_stream * stream = (php_stream *)ctx->data;
4139 	php_stream_write(stream, &ch, 1);
4140 } /* }}} */
4141 
_php_image_stream_putbuf(struct gdIOCtx * ctx,const void * buf,int l)4142 static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
4143 {
4144 	php_stream * stream = (php_stream *)ctx->data;
4145 	return php_stream_write(stream, (void *)buf, l);
4146 } /* }}} */
4147 
_php_image_stream_ctxfree(struct gdIOCtx * ctx)4148 static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
4149 {
4150 	if(ctx->data) {
4151 		ctx->data = NULL;
4152 	}
4153 	if(ctx) {
4154 		efree(ctx);
4155 	}
4156 } /* }}} */
4157 
_php_image_stream_ctxfreeandclose(struct gdIOCtx * ctx)4158 static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
4159 {
4160 
4161 	if(ctx->data) {
4162 		php_stream_close((php_stream *) ctx->data);
4163 		ctx->data = NULL;
4164 	}
4165 	if(ctx) {
4166 		efree(ctx);
4167 	}
4168 } /* }}} */
4169 
create_stream_context_from_zval(zval * to_zval)4170 static gdIOCtx *create_stream_context_from_zval(zval *to_zval) {
4171 	php_stream *stream;
4172 	int close_stream = 1;
4173 
4174 	if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
4175 		php_stream_from_zval_no_verify(stream, to_zval);
4176 		if (stream == NULL) {
4177 			return NULL;
4178 		}
4179 		close_stream = 0;
4180 	} else if (Z_TYPE_P(to_zval) == IS_STRING) {
4181 		if (CHECK_ZVAL_NULL_PATH(to_zval)) {
4182 			zend_argument_type_error(2, "must not contain null bytes");
4183 			return NULL;
4184 		}
4185 
4186 		stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
4187 		if (stream == NULL) {
4188 			return NULL;
4189 		}
4190 	} else {
4191 		zend_argument_type_error(2, "must be a file name or a stream resource, %s given", zend_zval_type_name(to_zval));
4192 		return NULL;
4193 	}
4194 
4195 	return create_stream_context(stream, close_stream);
4196 }
4197 
create_stream_context(php_stream * stream,int close_stream)4198 static gdIOCtx *create_stream_context(php_stream *stream, int close_stream) {
4199 	gdIOCtx *ctx = ecalloc(1, sizeof(gdIOCtx));
4200 
4201 	ctx->putC = _php_image_stream_putc;
4202 	ctx->putBuf = _php_image_stream_putbuf;
4203 	if (close_stream) {
4204 		ctx->gd_free = _php_image_stream_ctxfreeandclose;
4205 	} else {
4206 		ctx->gd_free = _php_image_stream_ctxfree;
4207 	}
4208 	ctx->data = (void *)stream;
4209 
4210 	return ctx;
4211 }
4212 
create_output_context()4213 static gdIOCtx *create_output_context() {
4214 	gdIOCtx *ctx = ecalloc(1, sizeof(gdIOCtx));
4215 
4216 	ctx->putC = _php_image_output_putc;
4217 	ctx->putBuf = _php_image_output_putbuf;
4218 	ctx->gd_free = _php_image_output_ctxfree;
4219 
4220 	return ctx;
4221 }
4222 
_php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn)4223 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn)
4224 {
4225 	zval *imgind;
4226 	zend_long quality = -1, basefilter = -1, speed = -1;
4227 	gdImagePtr im;
4228 	gdIOCtx *ctx = NULL;
4229 	zval *to_zval = NULL;
4230 
4231 	if (image_type == PHP_GDIMG_TYPE_GIF) {
4232 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!", &imgind, gd_image_ce, &to_zval) == FAILURE) {
4233 			RETURN_THROWS();
4234 		}
4235 	} else if (image_type == PHP_GDIMG_TYPE_PNG) {
4236 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
4237 			RETURN_THROWS();
4238 		}
4239 	} else if (image_type == PHP_GDIMG_TYPE_AVIF) {
4240 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &speed) == FAILURE) {
4241 			RETURN_THROWS();
4242 		}
4243 	} else {
4244 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
4245 			RETURN_THROWS();
4246 		}
4247 	}
4248 
4249 	im = php_gd_libgdimageptr_from_zval_p(imgind);
4250 
4251 	if (to_zval != NULL) {
4252 		ctx = create_stream_context_from_zval(to_zval);
4253 		if (!ctx) {
4254 			RETURN_FALSE;
4255 		}
4256 	} else {
4257 		ctx = create_output_context();
4258 	}
4259 
4260 	switch (image_type) {
4261 #ifdef HAVE_GD_JPG
4262 		case PHP_GDIMG_TYPE_JPG:
4263 			gdImageJpegCtx(im, ctx, (int) quality);
4264 			break;
4265 #endif
4266 #ifdef HAVE_GD_WEBP
4267 		case PHP_GDIMG_TYPE_WEBP:
4268 			if (quality == -1) {
4269 				quality = 80;
4270 			}
4271 			gdImageWebpCtx(im, ctx, (int) quality);
4272 			break;
4273 #endif
4274 #ifdef HAVE_GD_AVIF
4275 		case PHP_GDIMG_TYPE_AVIF:
4276 			if (speed == -1) {
4277 				speed = 6;
4278 			}
4279 			gdImageAvifCtx(im, ctx, (int) quality, (int) speed);
4280 			break;
4281 #endif
4282 #ifdef HAVE_GD_PNG
4283 		case PHP_GDIMG_TYPE_PNG:
4284 #ifdef HAVE_GD_BUNDLED
4285 			gdImagePngCtxEx(im, ctx, (int) quality, (int) basefilter);
4286 #else
4287 			gdImagePngCtxEx(im, ctx, (int) quality);
4288 #endif
4289 			break;
4290 #endif
4291 		case PHP_GDIMG_TYPE_GIF:
4292 			gdImageGifCtx(im, ctx);
4293 			break;
4294 		 EMPTY_SWITCH_DEFAULT_CASE()
4295 	}
4296 
4297 	ctx->gd_free(ctx);
4298 
4299 	RETURN_TRUE;
4300 }
4301 
4302 /* }}} */
4303