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_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 if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
747 php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
748 efree(font);
749 php_stream_close(stream);
750 RETURN_FALSE;
751 }
752 body_size = font->w * font->h * font->nchars;
753 }
754
755 if (body_size != body_size_check) {
756 php_error_docref(NULL, E_WARNING, "Error reading font");
757 efree(font);
758 php_stream_close(stream);
759 RETURN_FALSE;
760 }
761
762 ZEND_ASSERT(body_size > 0);
763 font->data = emalloc(body_size);
764 b = 0;
765 while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b)) > 0) {
766 b += n;
767 }
768
769 if (n <= 0) {
770 efree(font->data);
771 efree(font);
772 if (php_stream_eof(stream)) {
773 php_error_docref(NULL, E_WARNING, "End of file while reading body");
774 } else {
775 php_error_docref(NULL, E_WARNING, "Error while reading body");
776 }
777 php_stream_close(stream);
778 RETURN_FALSE;
779 }
780 php_stream_close(stream);
781
782 object_init_ex(return_value, gd_font_ce);
783 php_gd_font_object_from_zend_object(Z_OBJ_P(return_value))->font = font;
784 }
785 /* }}} */
786
787 /* {{{ Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
PHP_FUNCTION(imagesetstyle)788 PHP_FUNCTION(imagesetstyle)
789 {
790 zval *IM, *styles, *item;
791 gdImagePtr im;
792 int *stylearr;
793 int index = 0;
794 uint32_t num_styles;
795
796 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &styles) == FAILURE) {
797 RETURN_THROWS();
798 }
799
800 im = php_gd_libgdimageptr_from_zval_p(IM);
801
802 num_styles = zend_hash_num_elements(Z_ARRVAL_P(styles));
803 if (num_styles == 0) {
804 zend_argument_value_error(2, "cannot be empty");
805 RETURN_THROWS();
806 }
807
808 /* copy the style values in the stylearr */
809 stylearr = safe_emalloc(sizeof(int), num_styles, 0);
810
811 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(styles), item) {
812 stylearr[index++] = zval_get_long(item);
813 } ZEND_HASH_FOREACH_END();
814
815 gdImageSetStyle(im, stylearr, index);
816
817 efree(stylearr);
818
819 RETURN_TRUE;
820 }
821 /* }}} */
822
823 /* {{{ Create a new true color image */
PHP_FUNCTION(imagecreatetruecolor)824 PHP_FUNCTION(imagecreatetruecolor)
825 {
826 zend_long x_size, y_size;
827 gdImagePtr im;
828
829 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
830 RETURN_THROWS();
831 }
832
833 if (x_size <= 0 || x_size >= INT_MAX) {
834 zend_argument_value_error(1, "must be greater than 0");
835 RETURN_THROWS();
836 }
837
838 if (y_size <= 0 || y_size >= INT_MAX) {
839 zend_argument_value_error(2, "must be greater than 0");
840 RETURN_THROWS();
841 }
842
843 im = gdImageCreateTrueColor(x_size, y_size);
844
845 if (!im) {
846 RETURN_FALSE;
847 }
848
849 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
850 }
851 /* }}} */
852
853 /* {{{ return true if the image uses truecolor */
PHP_FUNCTION(imageistruecolor)854 PHP_FUNCTION(imageistruecolor)
855 {
856 zval *IM;
857 gdImagePtr im;
858
859 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
860 RETURN_THROWS();
861 }
862
863 im = php_gd_libgdimageptr_from_zval_p(IM);
864
865 RETURN_BOOL(im->trueColor);
866 }
867 /* }}} */
868
869 /* {{{ Convert a true color image to a palette based image with a number of colors, optionally using dithering. */
PHP_FUNCTION(imagetruecolortopalette)870 PHP_FUNCTION(imagetruecolortopalette)
871 {
872 zval *IM;
873 bool dither;
874 zend_long ncolors;
875 gdImagePtr im;
876
877 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Obl", &IM, gd_image_ce, &dither, &ncolors) == FAILURE) {
878 RETURN_THROWS();
879 }
880
881 im = php_gd_libgdimageptr_from_zval_p(IM);
882
883 if (ncolors <= 0 || ZEND_LONG_INT_OVFL(ncolors)) {
884 zend_argument_value_error(3, "must be greater than 0 and less than %d", INT_MAX);
885 RETURN_THROWS();
886 }
887
888 if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) {
889 RETURN_TRUE;
890 } else {
891 php_error_docref(NULL, E_WARNING, "Couldn't convert to palette");
892 RETURN_FALSE;
893 }
894 }
895 /* }}} */
896
897 /* {{{ Convert a palette based image to a true color image. */
PHP_FUNCTION(imagepalettetotruecolor)898 PHP_FUNCTION(imagepalettetotruecolor)
899 {
900 zval *IM;
901 gdImagePtr im;
902
903 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
904 RETURN_THROWS();
905 }
906
907 im = php_gd_libgdimageptr_from_zval_p(IM);
908
909 if (gdImagePaletteToTrueColor(im) == 0) {
910 RETURN_FALSE;
911 }
912
913 RETURN_TRUE;
914 }
915 /* }}} */
916
917 /* {{{ Makes the colors of the palette version of an image more closely match the true color version */
PHP_FUNCTION(imagecolormatch)918 PHP_FUNCTION(imagecolormatch)
919 {
920 zval *IM1, *IM2;
921 gdImagePtr im1, im2;
922 int result;
923
924 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM1, gd_image_ce, &IM2, gd_image_ce) == FAILURE) {
925 RETURN_THROWS();
926 }
927
928 im1 = php_gd_libgdimageptr_from_zval_p(IM1);
929 im2 = php_gd_libgdimageptr_from_zval_p(IM2);
930
931 result = gdImageColorMatch(im1, im2);
932 switch (result) {
933 case -1:
934 zend_argument_value_error(1, "must be TrueColor");
935 RETURN_THROWS();
936 break;
937 case -2:
938 zend_argument_value_error(2, "must be Palette");
939 RETURN_THROWS();
940 break;
941 case -3:
942 zend_argument_value_error(2, "must be the same size as argument #1 ($im1)");
943 RETURN_THROWS();
944 break;
945 case -4:
946 zend_argument_value_error(2, "must have at least one color");
947 RETURN_THROWS();
948 break;
949 }
950
951 RETURN_TRUE;
952 }
953 /* }}} */
954
955 /* {{{ Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
PHP_FUNCTION(imagesetthickness)956 PHP_FUNCTION(imagesetthickness)
957 {
958 zval *IM;
959 zend_long thick;
960 gdImagePtr im;
961
962 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &thick) == FAILURE) {
963 RETURN_THROWS();
964 }
965
966 im = php_gd_libgdimageptr_from_zval_p(IM);
967
968 gdImageSetThickness(im, thick);
969
970 RETURN_TRUE;
971 }
972 /* }}} */
973
974 /* {{{ Draw an ellipse */
PHP_FUNCTION(imagefilledellipse)975 PHP_FUNCTION(imagefilledellipse)
976 {
977 zval *IM;
978 zend_long cx, cy, w, h, color;
979 gdImagePtr im;
980
981 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &color) == FAILURE) {
982 RETURN_THROWS();
983 }
984
985 im = php_gd_libgdimageptr_from_zval_p(IM);
986
987 gdImageFilledEllipse(im, cx, cy, w, h, color);
988 RETURN_TRUE;
989 }
990 /* }}} */
991
992 /* {{{ Draw a filled partial ellipse */
PHP_FUNCTION(imagefilledarc)993 PHP_FUNCTION(imagefilledarc)
994 {
995 zval *IM;
996 zend_long cx, cy, w, h, ST, E, col, style;
997 gdImagePtr im;
998 int e, st;
999
1000 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
1001 RETURN_THROWS();
1002 }
1003
1004 im = php_gd_libgdimageptr_from_zval_p(IM);
1005
1006 e = E;
1007 if (e < 0) {
1008 e %= 360;
1009 }
1010
1011 st = ST;
1012 if (st < 0) {
1013 st %= 360;
1014 }
1015
1016 gdImageFilledArc(im, cx, cy, w, h, st, e, col, style);
1017
1018 RETURN_TRUE;
1019 }
1020 /* }}} */
1021
1022 /* {{{ Turn alpha blending mode on or off for the given image */
PHP_FUNCTION(imagealphablending)1023 PHP_FUNCTION(imagealphablending)
1024 {
1025 zval *IM;
1026 bool blend;
1027 gdImagePtr im;
1028
1029 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &blend) == FAILURE) {
1030 RETURN_THROWS();
1031 }
1032
1033 im = php_gd_libgdimageptr_from_zval_p(IM);
1034
1035 gdImageAlphaBlending(im, blend);
1036
1037 RETURN_TRUE;
1038 }
1039 /* }}} */
1040
1041 /* {{{ Include alpha channel to a saved image */
PHP_FUNCTION(imagesavealpha)1042 PHP_FUNCTION(imagesavealpha)
1043 {
1044 zval *IM;
1045 bool save;
1046 gdImagePtr im;
1047
1048 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &save) == FAILURE) {
1049 RETURN_THROWS();
1050 }
1051
1052 im = php_gd_libgdimageptr_from_zval_p(IM);
1053
1054 gdImageSaveAlpha(im, save);
1055
1056 RETURN_TRUE;
1057 }
1058 /* }}} */
1059
1060 /* {{{ Set the alpha blending flag to use the bundled libgd layering effects */
PHP_FUNCTION(imagelayereffect)1061 PHP_FUNCTION(imagelayereffect)
1062 {
1063 zval *IM;
1064 zend_long effect;
1065 gdImagePtr im;
1066
1067 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &effect) == FAILURE) {
1068 RETURN_THROWS();
1069 }
1070
1071 im = php_gd_libgdimageptr_from_zval_p(IM);
1072
1073 gdImageAlphaBlending(im, effect);
1074
1075 RETURN_TRUE;
1076 }
1077 /* }}} */
1078
1079 #define CHECK_RGBA_RANGE(component, name, argument_number) \
1080 if (component < 0 || component > gd##name##Max) { \
1081 zend_argument_value_error(argument_number, "must be between 0 and %d (inclusive)", gd##name##Max); \
1082 RETURN_THROWS(); \
1083 }
1084
1085 /* {{{ Allocate a color with an alpha level. Works for true color and palette based images */
PHP_FUNCTION(imagecolorallocatealpha)1086 PHP_FUNCTION(imagecolorallocatealpha)
1087 {
1088 zval *IM;
1089 zend_long red, green, blue, alpha;
1090 gdImagePtr im;
1091 int ct = (-1);
1092
1093 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1094 RETURN_THROWS();
1095 }
1096
1097 im = php_gd_libgdimageptr_from_zval_p(IM);
1098
1099 CHECK_RGBA_RANGE(red, Red, 2);
1100 CHECK_RGBA_RANGE(green, Green, 3);
1101 CHECK_RGBA_RANGE(blue, Blue, 4);
1102 CHECK_RGBA_RANGE(alpha, Alpha, 5);
1103
1104 ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
1105 if (ct < 0) {
1106 RETURN_FALSE;
1107 }
1108 RETURN_LONG((zend_long)ct);
1109 }
1110 /* }}} */
1111
1112 /* {{{ Resolve/Allocate a colour with an alpha level. Works for true colour and palette based images */
PHP_FUNCTION(imagecolorresolvealpha)1113 PHP_FUNCTION(imagecolorresolvealpha)
1114 {
1115 zval *IM;
1116 zend_long red, green, blue, alpha;
1117 gdImagePtr im;
1118
1119 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1120 RETURN_THROWS();
1121 }
1122
1123 im = php_gd_libgdimageptr_from_zval_p(IM);
1124
1125 CHECK_RGBA_RANGE(red, Red, 2);
1126 CHECK_RGBA_RANGE(green, Green, 3);
1127 CHECK_RGBA_RANGE(blue, Blue, 4);
1128 CHECK_RGBA_RANGE(alpha, Alpha, 5);
1129
1130 RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha));
1131 }
1132 /* }}} */
1133
1134 /* {{{ Find the closest matching colour with alpha transparency */
PHP_FUNCTION(imagecolorclosestalpha)1135 PHP_FUNCTION(imagecolorclosestalpha)
1136 {
1137 zval *IM;
1138 zend_long red, green, blue, alpha;
1139 gdImagePtr im;
1140
1141 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1142 RETURN_THROWS();
1143 }
1144
1145 im = php_gd_libgdimageptr_from_zval_p(IM);
1146
1147 CHECK_RGBA_RANGE(red, Red, 2);
1148 CHECK_RGBA_RANGE(green, Green, 3);
1149 CHECK_RGBA_RANGE(blue, Blue, 4);
1150 CHECK_RGBA_RANGE(alpha, Alpha, 5);
1151
1152 RETURN_LONG(gdImageColorClosestAlpha(im, red, green, blue, alpha));
1153 }
1154 /* }}} */
1155
1156 /* {{{ Find exact match for colour with transparency */
PHP_FUNCTION(imagecolorexactalpha)1157 PHP_FUNCTION(imagecolorexactalpha)
1158 {
1159 zval *IM;
1160 zend_long red, green, blue, alpha;
1161 gdImagePtr im;
1162
1163 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
1164 RETURN_THROWS();
1165 }
1166
1167 im = php_gd_libgdimageptr_from_zval_p(IM);
1168
1169 CHECK_RGBA_RANGE(red, Red, 2);
1170 CHECK_RGBA_RANGE(green, Green, 3);
1171 CHECK_RGBA_RANGE(blue, Blue, 4);
1172 CHECK_RGBA_RANGE(alpha, Alpha, 5);
1173
1174 RETURN_LONG(gdImageColorExactAlpha(im, red, green, blue, alpha));
1175 }
1176 /* }}} */
1177
1178 /* {{{ Copy and resize part of an image using resampling to help ensure clarity */
PHP_FUNCTION(imagecopyresampled)1179 PHP_FUNCTION(imagecopyresampled)
1180 {
1181 zval *SIM, *DIM;
1182 zend_long SX, SY, SW, SH, DX, DY, DW, DH;
1183 gdImagePtr im_dst, im_src;
1184 int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
1185
1186 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) {
1187 RETURN_THROWS();
1188 }
1189
1190 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
1191 im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
1192
1193 srcX = SX;
1194 srcY = SY;
1195 srcH = SH;
1196 srcW = SW;
1197 dstX = DX;
1198 dstY = DY;
1199 dstH = DH;
1200 dstW = DW;
1201
1202 gdImageCopyResampled(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
1203
1204 RETURN_TRUE;
1205 }
1206 /* }}} */
1207
1208 #ifdef PHP_WIN32
1209 /* {{{ Grab a window or its client area using a windows handle (HWND property in COM instance) */
PHP_FUNCTION(imagegrabwindow)1210 PHP_FUNCTION(imagegrabwindow)
1211 {
1212 HWND window;
1213 bool client_area = 0;
1214 RECT rc = {0};
1215 int Width, Height;
1216 HDC hdc;
1217 HDC memDC;
1218 HBITMAP memBM;
1219 HBITMAP hOld;
1220 zend_long lwindow_handle;
1221 gdImagePtr im = NULL;
1222
1223 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &lwindow_handle, &client_area) == FAILURE) {
1224 RETURN_THROWS();
1225 }
1226
1227 window = (HWND) lwindow_handle;
1228
1229 if (!IsWindow(window)) {
1230 php_error_docref(NULL, E_NOTICE, "Invalid window handle");
1231 RETURN_FALSE;
1232 }
1233
1234 hdc = GetDC(0);
1235
1236 if (client_area) {
1237 GetClientRect(window, &rc);
1238 Width = rc.right;
1239 Height = rc.bottom;
1240 } else {
1241 GetWindowRect(window, &rc);
1242 Width = rc.right - rc.left;
1243 Height = rc.bottom - rc.top;
1244 }
1245
1246 Width = (Width/4)*4;
1247
1248 memDC = CreateCompatibleDC(hdc);
1249 memBM = CreateCompatibleBitmap(hdc, Width, Height);
1250 hOld = (HBITMAP) SelectObject (memDC, memBM);
1251
1252 PrintWindow(window, memDC, (UINT) client_area);
1253
1254 im = gdImageCreateTrueColor(Width, Height);
1255 if (im) {
1256 int x,y;
1257 for (y=0; y <= Height; y++) {
1258 for (x=0; x <= Width; x++) {
1259 int c = GetPixel(memDC, x,y);
1260 gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
1261 }
1262 }
1263 }
1264
1265 SelectObject(memDC,hOld);
1266 DeleteObject(memBM);
1267 DeleteDC(memDC);
1268 ReleaseDC( 0, hdc );
1269
1270 if (!im) {
1271 RETURN_FALSE;
1272 }
1273
1274 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1275 }
1276 /* }}} */
1277
1278 /* {{{ Grab a screenshot */
PHP_FUNCTION(imagegrabscreen)1279 PHP_FUNCTION(imagegrabscreen)
1280 {
1281 HWND window = GetDesktopWindow();
1282 RECT rc = {0};
1283 int Width, Height;
1284 HDC hdc;
1285 HDC memDC;
1286 HBITMAP memBM;
1287 HBITMAP hOld;
1288 gdImagePtr im;
1289 hdc = GetDC(0);
1290
1291 if (zend_parse_parameters_none() == FAILURE) {
1292 RETURN_THROWS();
1293 }
1294
1295 if (!hdc) {
1296 RETURN_FALSE;
1297 }
1298
1299 GetWindowRect(window, &rc);
1300 Width = rc.right - rc.left;
1301 Height = rc.bottom - rc.top;
1302
1303 Width = (Width/4)*4;
1304
1305 memDC = CreateCompatibleDC(hdc);
1306 memBM = CreateCompatibleBitmap(hdc, Width, Height);
1307 hOld = (HBITMAP) SelectObject (memDC, memBM);
1308 BitBlt( memDC, 0, 0, Width, Height , hdc, rc.left, rc.top , SRCCOPY );
1309
1310 im = gdImageCreateTrueColor(Width, Height);
1311 if (im) {
1312 int x,y;
1313 for (y=0; y <= Height; y++) {
1314 for (x=0; x <= Width; x++) {
1315 int c = GetPixel(memDC, x,y);
1316 gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
1317 }
1318 }
1319 }
1320
1321 SelectObject(memDC,hOld);
1322 DeleteObject(memBM);
1323 DeleteDC(memDC);
1324 ReleaseDC( 0, hdc );
1325
1326 if (!im) {
1327 RETURN_FALSE;
1328 }
1329
1330 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1331 }
1332 /* }}} */
1333 #endif /* PHP_WIN32 */
1334
1335 /* {{{ Rotate an image using a custom angle */
PHP_FUNCTION(imagerotate)1336 PHP_FUNCTION(imagerotate)
1337 {
1338 zval *SIM;
1339 gdImagePtr im_dst, im_src;
1340 double degrees;
1341 zend_long color;
1342 bool ignoretransparent = 0;
1343
1344 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odl|b", &SIM, gd_image_ce, °rees, &color, &ignoretransparent) == FAILURE) {
1345 RETURN_THROWS();
1346 }
1347
1348 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
1349 im_dst = gdImageRotateInterpolated(im_src, (const float)degrees, color);
1350
1351 if (im_dst == NULL) {
1352 RETURN_FALSE;
1353 }
1354
1355 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_dst);
1356 }
1357 /* }}} */
1358
1359 /* {{{ Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color */
PHP_FUNCTION(imagesettile)1360 PHP_FUNCTION(imagesettile)
1361 {
1362 zval *IM, *TILE;
1363 gdImagePtr im, tile;
1364
1365 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM, gd_image_ce, &TILE, gd_image_ce) == FAILURE) {
1366 RETURN_THROWS();
1367 }
1368
1369 im = php_gd_libgdimageptr_from_zval_p(IM);
1370 tile = php_gd_libgdimageptr_from_zval_p(TILE);
1371
1372 gdImageSetTile(im, tile);
1373
1374 RETURN_TRUE;
1375 }
1376 /* }}} */
1377
1378 /* {{{ Set the brush image to $brush when filling $image with the "IMG_COLOR_BRUSHED" color */
PHP_FUNCTION(imagesetbrush)1379 PHP_FUNCTION(imagesetbrush)
1380 {
1381 zval *IM, *TILE;
1382 gdImagePtr im, tile;
1383
1384 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM, gd_image_ce, &TILE, gd_image_ce) == FAILURE) {
1385 RETURN_THROWS();
1386 }
1387
1388 im = php_gd_libgdimageptr_from_zval_p(IM);
1389 tile = php_gd_libgdimageptr_from_zval_p(TILE);
1390
1391 gdImageSetBrush(im, tile);
1392
1393 RETURN_TRUE;
1394 }
1395 /* }}} */
1396
1397 /* {{{ Create a new image */
PHP_FUNCTION(imagecreate)1398 PHP_FUNCTION(imagecreate)
1399 {
1400 zend_long x_size, y_size;
1401 gdImagePtr im;
1402
1403 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
1404 RETURN_THROWS();
1405 }
1406
1407 if (x_size <= 0 || x_size >= INT_MAX) {
1408 zend_argument_value_error(1, "must be greater than 0");
1409 RETURN_THROWS();
1410 }
1411
1412 if (y_size <= 0 || y_size >= INT_MAX) {
1413 zend_argument_value_error(2, "must be greater than 0");
1414 RETURN_THROWS();
1415 }
1416
1417 im = gdImageCreate(x_size, y_size);
1418
1419 if (!im) {
1420 RETURN_FALSE;
1421 }
1422
1423 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1424 }
1425 /* }}} */
1426
1427 /* {{{ Return the types of images supported in a bitfield - 1=GIF, 2=JPEG, 4=PNG, 8=WBMP, 16=XPM, etc */
PHP_FUNCTION(imagetypes)1428 PHP_FUNCTION(imagetypes)
1429 {
1430 int ret = 0;
1431 ret = PHP_IMG_GIF;
1432 #ifdef HAVE_GD_JPG
1433 ret |= PHP_IMG_JPG;
1434 #endif
1435 #ifdef HAVE_GD_PNG
1436 ret |= PHP_IMG_PNG;
1437 #endif
1438 ret |= PHP_IMG_WBMP;
1439 #ifdef HAVE_GD_XPM
1440 ret |= PHP_IMG_XPM;
1441 #endif
1442 #ifdef HAVE_GD_WEBP
1443 ret |= PHP_IMG_WEBP;
1444 #endif
1445 #ifdef HAVE_GD_BMP
1446 ret |= PHP_IMG_BMP;
1447 #endif
1448 #ifdef HAVE_GD_TGA
1449 ret |= PHP_IMG_TGA;
1450 #endif
1451 #ifdef HAVE_GD_AVIF
1452 ret |= PHP_IMG_AVIF;
1453 #endif
1454
1455 if (zend_parse_parameters_none() == FAILURE) {
1456 RETURN_THROWS();
1457 }
1458
1459 RETURN_LONG(ret);
1460 }
1461 /* }}} */
1462
1463 /* {{{ _php_ctx_getmbi */
1464
_php_ctx_getmbi(gdIOCtx * ctx)1465 static int _php_ctx_getmbi(gdIOCtx *ctx)
1466 {
1467 int i, mbi = 0;
1468
1469 do {
1470 i = (ctx->getC)(ctx);
1471 if (i < 0) {
1472 return -1;
1473 }
1474 mbi = (mbi << 7) | (i & 0x7f);
1475 } while (i & 0x80);
1476
1477 return mbi;
1478 }
1479 /* }}} */
1480
1481 /* {{{ _php_image_type
1482 * Based on ext/standard/image.c
1483 */
1484 static const char php_sig_gd2[3] = {'g', 'd', '2'};
1485
_php_image_type(zend_string * data)1486 static int _php_image_type(zend_string *data)
1487 {
1488 if (ZSTR_LEN(data) < 12) {
1489 /* Handle this the same way as an unknown image type. */
1490 return -1;
1491 }
1492
1493 if (!memcmp(ZSTR_VAL(data), php_sig_gd2, sizeof(php_sig_gd2))) {
1494 return PHP_GDIMG_TYPE_GD2;
1495 } else if (!memcmp(ZSTR_VAL(data), php_sig_jpg, sizeof(php_sig_jpg))) {
1496 return PHP_GDIMG_TYPE_JPG;
1497 } else if (!memcmp(ZSTR_VAL(data), php_sig_png, sizeof(php_sig_png))) {
1498 return PHP_GDIMG_TYPE_PNG;
1499 } else if (!memcmp(ZSTR_VAL(data), php_sig_gif, sizeof(php_sig_gif))) {
1500 return PHP_GDIMG_TYPE_GIF;
1501 } else if (!memcmp(ZSTR_VAL(data), php_sig_bmp, sizeof(php_sig_bmp))) {
1502 return PHP_GDIMG_TYPE_BMP;
1503 } 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))) {
1504 return PHP_GDIMG_TYPE_WEBP;
1505 }
1506
1507 php_stream *image_stream = php_stream_memory_open(TEMP_STREAM_READONLY, data);
1508
1509 if (image_stream != NULL) {
1510 bool is_avif = php_is_image_avif(image_stream);
1511 php_stream_close(image_stream);
1512
1513 if (is_avif) {
1514 return PHP_GDIMG_TYPE_AVIF;
1515 }
1516 }
1517
1518 gdIOCtx *io_ctx;
1519 io_ctx = gdNewDynamicCtxEx(8, ZSTR_VAL(data), 0);
1520 if (io_ctx) {
1521 if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
1522 io_ctx->gd_free(io_ctx);
1523 return PHP_GDIMG_TYPE_WBM;
1524 } else {
1525 io_ctx->gd_free(io_ctx);
1526 }
1527 }
1528
1529 return -1;
1530 }
1531 /* }}} */
1532
1533 /* {{{ _php_image_create_from_string */
_php_image_create_from_string(zend_string * data,char * tn,gdImagePtr (* ioctx_func_p)(gdIOCtxPtr))1534 gdImagePtr _php_image_create_from_string(zend_string *data, char *tn, gdImagePtr (*ioctx_func_p)(gdIOCtxPtr))
1535 {
1536 gdImagePtr im;
1537 gdIOCtx *io_ctx;
1538
1539 io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(data), ZSTR_VAL(data), 0);
1540
1541 if (!io_ctx) {
1542 return NULL;
1543 }
1544
1545 im = (*ioctx_func_p)(io_ctx);
1546 if (!im) {
1547 php_error_docref(NULL, E_WARNING, "Passed data is not in \"%s\" format", tn);
1548 io_ctx->gd_free(io_ctx);
1549 return NULL;
1550 }
1551
1552 io_ctx->gd_free(io_ctx);
1553
1554 return im;
1555 }
1556 /* }}} */
1557
1558 /* {{{ Create a new image from the image stream in the string */
PHP_FUNCTION(imagecreatefromstring)1559 PHP_FUNCTION(imagecreatefromstring)
1560 {
1561 zend_string *data;
1562 gdImagePtr im;
1563 int imtype;
1564
1565 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &data) == FAILURE) {
1566 RETURN_THROWS();
1567 }
1568
1569 imtype = _php_image_type(data);
1570
1571 switch (imtype) {
1572 case PHP_GDIMG_TYPE_JPG:
1573 #ifdef HAVE_GD_JPG
1574 im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx);
1575 #else
1576 php_error_docref(NULL, E_WARNING, "No JPEG support in this PHP build");
1577 RETURN_FALSE;
1578 #endif
1579 break;
1580
1581 case PHP_GDIMG_TYPE_PNG:
1582 #ifdef HAVE_GD_PNG
1583 im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx);
1584 #else
1585 php_error_docref(NULL, E_WARNING, "No PNG support in this PHP build");
1586 RETURN_FALSE;
1587 #endif
1588 break;
1589
1590 case PHP_GDIMG_TYPE_GIF:
1591 im = _php_image_create_from_string(data, "GIF", gdImageCreateFromGifCtx);
1592 break;
1593
1594 case PHP_GDIMG_TYPE_WBM:
1595 im = _php_image_create_from_string(data, "WBMP", gdImageCreateFromWBMPCtx);
1596 break;
1597
1598 case PHP_GDIMG_TYPE_GD2:
1599 im = _php_image_create_from_string(data, "GD2", gdImageCreateFromGd2Ctx);
1600 break;
1601
1602 case PHP_GDIMG_TYPE_BMP:
1603 im = _php_image_create_from_string(data, "BMP", gdImageCreateFromBmpCtx);
1604 break;
1605
1606 case PHP_GDIMG_TYPE_WEBP:
1607 #ifdef HAVE_GD_WEBP
1608 im = _php_image_create_from_string(data, "WEBP", gdImageCreateFromWebpCtx);
1609 break;
1610 #else
1611 php_error_docref(NULL, E_WARNING, "No WEBP support in this PHP build");
1612 RETURN_FALSE;
1613 #endif
1614
1615 case PHP_GDIMG_TYPE_AVIF:
1616 #ifdef HAVE_GD_AVIF
1617 im = _php_image_create_from_string(data, "AVIF", gdImageCreateFromAvifCtx);
1618 break;
1619 #else
1620 php_error_docref(NULL, E_WARNING, "No AVIF support in this PHP build");
1621 RETURN_FALSE;
1622 #endif
1623
1624 default:
1625 php_error_docref(NULL, E_WARNING, "Data is not in a recognized format");
1626 RETURN_FALSE;
1627 }
1628
1629 if (!im) {
1630 php_error_docref(NULL, E_WARNING, "Couldn't create GD Image Stream out of Data");
1631 RETURN_FALSE;
1632 }
1633
1634 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1635 }
1636 /* }}} */
1637
1638 /* {{{ _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))1639 static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(FILE *), gdImagePtr (*ioctx_func_p)(gdIOCtxPtr))
1640 {
1641 char *file;
1642 size_t file_len;
1643 zend_long srcx, srcy, width, height;
1644 gdImagePtr im = NULL;
1645 php_stream *stream;
1646 FILE * fp = NULL;
1647 #ifdef HAVE_GD_JPG
1648 long ignore_warning;
1649 #endif
1650
1651 if (image_type == PHP_GDIMG_TYPE_GD2PART) {
1652 if (zend_parse_parameters(ZEND_NUM_ARGS(), "pllll", &file, &file_len, &srcx, &srcy, &width, &height) == FAILURE) {
1653 RETURN_THROWS();
1654 }
1655
1656 if (width < 1) {
1657 zend_argument_value_error(4, "must be greater than or equal to 1");
1658 RETURN_THROWS();
1659 }
1660
1661 if (height < 1) {
1662 zend_argument_value_error(5, "must be greater than or equal to 1");
1663 RETURN_THROWS();
1664 }
1665
1666 } else {
1667 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &file, &file_len) == FAILURE) {
1668 RETURN_THROWS();
1669 }
1670 }
1671
1672
1673 stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH, NULL);
1674 if (stream == NULL) {
1675 RETURN_FALSE;
1676 }
1677
1678 /* try and avoid allocating a FILE* if the stream is not naturally a FILE* */
1679 if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
1680 if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
1681 goto out_err;
1682 }
1683 } else if (ioctx_func_p || image_type == PHP_GDIMG_TYPE_GD2PART) {
1684 /* we can create an io context */
1685 gdIOCtx* io_ctx;
1686 zend_string *buff;
1687 char *pstr;
1688
1689 buff = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
1690
1691 if (!buff) {
1692 php_error_docref(NULL, E_WARNING,"Cannot read image data");
1693 goto out_err;
1694 }
1695
1696 /* needs to be malloc (persistent) - GD will free() it later */
1697 pstr = pestrndup(ZSTR_VAL(buff), ZSTR_LEN(buff), 1);
1698 io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(buff), pstr, 0);
1699 if (!io_ctx) {
1700 pefree(pstr, 1);
1701 zend_string_release_ex(buff, 0);
1702 php_error_docref(NULL, E_WARNING,"Cannot allocate GD IO context");
1703 goto out_err;
1704 }
1705
1706 if (image_type == PHP_GDIMG_TYPE_GD2PART) {
1707 im = gdImageCreateFromGd2PartCtx(io_ctx, srcx, srcy, width, height);
1708 } else {
1709 im = (*ioctx_func_p)(io_ctx);
1710 }
1711 io_ctx->gd_free(io_ctx);
1712 pefree(pstr, 1);
1713 zend_string_release_ex(buff, 0);
1714 }
1715 else if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO)) {
1716 /* try and force the stream to be FILE* */
1717 if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {
1718 goto out_err;
1719 }
1720 }
1721
1722 if (!im && fp) {
1723 switch (image_type) {
1724 case PHP_GDIMG_TYPE_GD2PART:
1725 im = gdImageCreateFromGd2Part(fp, srcx, srcy, width, height);
1726 break;
1727 #ifdef HAVE_GD_XPM
1728 case PHP_GDIMG_TYPE_XPM:
1729 im = gdImageCreateFromXpm(file);
1730 break;
1731 #endif
1732
1733 #ifdef HAVE_GD_JPG
1734 case PHP_GDIMG_TYPE_JPG:
1735 ignore_warning = INI_INT("gd.jpeg_ignore_warning");
1736 im = gdImageCreateFromJpegEx(fp, ignore_warning);
1737 break;
1738 #endif
1739
1740 default:
1741 im = (*func_p)(fp);
1742 break;
1743 }
1744
1745 fflush(fp);
1746 }
1747
1748 /* register_im: */
1749 if (im) {
1750 php_stream_close(stream);
1751 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
1752 return;
1753 }
1754
1755 php_error_docref(NULL, E_WARNING, "\"%s\" is not a valid %s file", file, tn);
1756 out_err:
1757 php_stream_close(stream);
1758 RETURN_FALSE;
1759
1760 }
1761 /* }}} */
1762
1763 /* {{{ Create a new image from GIF file or URL */
PHP_FUNCTION(imagecreatefromgif)1764 PHP_FUNCTION(imagecreatefromgif)
1765 {
1766 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
1767 }
1768 /* }}} */
1769
1770 #ifdef HAVE_GD_JPG
1771 /* {{{ Create a new image from JPEG file or URL */
PHP_FUNCTION(imagecreatefromjpeg)1772 PHP_FUNCTION(imagecreatefromjpeg)
1773 {
1774 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
1775 }
1776 /* }}} */
1777 #endif /* HAVE_GD_JPG */
1778
1779 #ifdef HAVE_GD_PNG
1780 /* {{{ Create a new image from PNG file or URL */
PHP_FUNCTION(imagecreatefrompng)1781 PHP_FUNCTION(imagecreatefrompng)
1782 {
1783 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
1784 }
1785 /* }}} */
1786 #endif /* HAVE_GD_PNG */
1787
1788 #ifdef HAVE_GD_WEBP
1789 /* {{{ Create a new image from WEBP file or URL */
PHP_FUNCTION(imagecreatefromwebp)1790 PHP_FUNCTION(imagecreatefromwebp)
1791 {
1792 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageCreateFromWebp, gdImageCreateFromWebpCtx);
1793 }
1794 /* }}} */
1795 #endif /* HAVE_GD_WEBP */
1796
1797 /* {{{ Create a new image from XBM file or URL */
PHP_FUNCTION(imagecreatefromxbm)1798 PHP_FUNCTION(imagecreatefromxbm)
1799 {
1800 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);
1801 }
1802 /* }}} */
1803
1804 #ifdef HAVE_GD_AVIF
1805 /* {{{ Create a new image from AVIF file or URL */
PHP_FUNCTION(imagecreatefromavif)1806 PHP_FUNCTION(imagecreatefromavif)
1807 {
1808 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF", gdImageCreateFromAvif, gdImageCreateFromAvifCtx);
1809 }
1810 /* }}} */
1811 #endif /* HAVE_GD_AVIF */
1812
1813 #ifdef HAVE_GD_XPM
1814 /* {{{ Create a new image from XPM file or URL */
PHP_FUNCTION(imagecreatefromxpm)1815 PHP_FUNCTION(imagecreatefromxpm)
1816 {
1817 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", NULL, NULL);
1818 }
1819 /* }}} */
1820 #endif
1821
1822 /* {{{ Create a new image from WBMP file or URL */
PHP_FUNCTION(imagecreatefromwbmp)1823 PHP_FUNCTION(imagecreatefromwbmp)
1824 {
1825 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);
1826 }
1827 /* }}} */
1828
1829 /* {{{ Create a new image from GD file or URL */
PHP_FUNCTION(imagecreatefromgd)1830 PHP_FUNCTION(imagecreatefromgd)
1831 {
1832 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);
1833 }
1834 /* }}} */
1835
1836 /* {{{ Create a new image from GD2 file or URL */
PHP_FUNCTION(imagecreatefromgd2)1837 PHP_FUNCTION(imagecreatefromgd2)
1838 {
1839 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);
1840 }
1841 /* }}} */
1842
1843 /* {{{ Create a new image from a given part of GD2 file or URL */
PHP_FUNCTION(imagecreatefromgd2part)1844 PHP_FUNCTION(imagecreatefromgd2part)
1845 {
1846 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", NULL, NULL);
1847 }
1848 /* }}} */
1849
1850 #ifdef HAVE_GD_BMP
1851 /* {{{ Create a new image from BMP file or URL */
PHP_FUNCTION(imagecreatefrombmp)1852 PHP_FUNCTION(imagecreatefrombmp)
1853 {
1854 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_BMP, "BMP", gdImageCreateFromBmp, gdImageCreateFromBmpCtx);
1855 }
1856 /* }}} */
1857 #endif
1858
1859 #ifdef HAVE_GD_TGA
1860 /* {{{ Create a new image from TGA file or URL */
PHP_FUNCTION(imagecreatefromtga)1861 PHP_FUNCTION(imagecreatefromtga)
1862 {
1863 _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_TGA, "TGA", gdImageCreateFromTga, gdImageCreateFromTgaCtx);
1864 }
1865 /* }}} */
1866 #endif
1867
1868 /* {{{ _php_image_output */
_php_image_output(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn)1869 static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn)
1870 {
1871 zval *imgind;
1872 char *file = NULL;
1873 zend_long quality = 0, type = 0;
1874 gdImagePtr im;
1875 FILE *fp;
1876 size_t file_len = 0;
1877 int argc = ZEND_NUM_ARGS();
1878 int q = -1, t = 1;
1879
1880 /* The quality parameter for gd2 stands for chunk size */
1881
1882 switch (image_type) {
1883 case PHP_GDIMG_TYPE_GD:
1884 if (zend_parse_parameters(argc, "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
1885 RETURN_THROWS();
1886 }
1887 break;
1888 case PHP_GDIMG_TYPE_GD2:
1889 if (zend_parse_parameters(argc, "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
1890 RETURN_THROWS();
1891 }
1892 break;
1893 EMPTY_SWITCH_DEFAULT_CASE()
1894 }
1895
1896 im = php_gd_libgdimageptr_from_zval_p(imgind);
1897
1898 if (argc >= 3) {
1899 q = quality;
1900 if (argc == 4) {
1901 t = type;
1902 }
1903 }
1904
1905 if (file_len) {
1906 PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
1907
1908 fp = VCWD_FOPEN(file, "wb");
1909 if (!fp) {
1910 php_error_docref(NULL, E_WARNING, "Unable to open \"%s\" for writing", file);
1911 RETURN_FALSE;
1912 }
1913
1914 switch (image_type) {
1915 case PHP_GDIMG_TYPE_GD:
1916 gdImageGd(im, fp);
1917 break;
1918 case PHP_GDIMG_TYPE_GD2:
1919 if (q == -1) {
1920 q = 128;
1921 }
1922 gdImageGd2(im, fp, q, t);
1923 break;
1924 EMPTY_SWITCH_DEFAULT_CASE()
1925 }
1926 fflush(fp);
1927 fclose(fp);
1928 } else {
1929 int b;
1930 FILE *tmp;
1931 char buf[4096];
1932 zend_string *path;
1933
1934 tmp = php_open_temporary_file(NULL, NULL, &path);
1935 if (tmp == NULL) {
1936 php_error_docref(NULL, E_WARNING, "Unable to open temporary file");
1937 RETURN_FALSE;
1938 }
1939
1940 switch (image_type) {
1941 case PHP_GDIMG_TYPE_GD:
1942 gdImageGd(im, tmp);
1943 break;
1944 case PHP_GDIMG_TYPE_GD2:
1945 if (q == -1) {
1946 q = 128;
1947 }
1948 gdImageGd2(im, tmp, q, t);
1949 break;
1950 EMPTY_SWITCH_DEFAULT_CASE()
1951 }
1952
1953 fseek(tmp, 0, SEEK_SET);
1954
1955 while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
1956 php_write(buf, b);
1957 }
1958
1959 fclose(tmp);
1960 VCWD_UNLINK((const char *)ZSTR_VAL(path)); /* make sure that the temporary file is removed */
1961 zend_string_release_ex(path, 0);
1962 }
1963 RETURN_TRUE;
1964 }
1965 /* }}} */
1966
1967 /* {{{ Output XBM image to browser or file */
PHP_FUNCTION(imagexbm)1968 PHP_FUNCTION(imagexbm)
1969 {
1970 zval *imgind;
1971 char *file = NULL;
1972 size_t file_len = 0;
1973 zend_long foreground_color;
1974 bool foreground_color_is_null = 1;
1975 gdImagePtr im;
1976 int i;
1977 gdIOCtx *ctx = NULL;
1978 php_stream *stream;
1979
1980 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op!|l!", &imgind, gd_image_ce, &file, &file_len, &foreground_color, &foreground_color_is_null) == FAILURE) {
1981 RETURN_THROWS();
1982 }
1983
1984 im = php_gd_libgdimageptr_from_zval_p(imgind);
1985
1986 if (file != NULL) {
1987 stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
1988 if (stream == NULL) {
1989 RETURN_FALSE;
1990 }
1991
1992 ctx = create_stream_context(stream, 1);
1993 } else {
1994 ctx = create_output_context();
1995 }
1996
1997 if (foreground_color_is_null) {
1998 for (i=0; i < gdImageColorsTotal(im); i++) {
1999 if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
2000 break;
2001 }
2002 }
2003
2004 foreground_color = i;
2005 }
2006
2007 gdImageXbmCtx(im, file ? file : "", (int) foreground_color, ctx);
2008
2009 ctx->gd_free(ctx);
2010
2011 RETURN_TRUE;
2012 }
2013 /* }}} */
2014
2015 /* {{{ Output GIF image to browser or file */
PHP_FUNCTION(imagegif)2016 PHP_FUNCTION(imagegif)
2017 {
2018 _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF");
2019 }
2020 /* }}} */
2021
2022 #ifdef HAVE_GD_PNG
2023 /* {{{ Output PNG image to browser or file */
PHP_FUNCTION(imagepng)2024 PHP_FUNCTION(imagepng)
2025 {
2026 _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG");
2027 }
2028 /* }}} */
2029 #endif /* HAVE_GD_PNG */
2030
2031 #ifdef HAVE_GD_WEBP
2032 /* {{{ Output WEBP image to browser or file */
PHP_FUNCTION(imagewebp)2033 PHP_FUNCTION(imagewebp)
2034 {
2035 _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP");
2036 }
2037 /* }}} */
2038 #endif /* HAVE_GD_WEBP */
2039
2040 #ifdef HAVE_GD_AVIF
2041 /* {{{ Output AVIF image to browser or file */
PHP_FUNCTION(imageavif)2042 PHP_FUNCTION(imageavif)
2043 {
2044 _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF");
2045 }
2046 /* }}} */
2047 #endif /* HAVE_GD_AVIF */
2048
2049 #ifdef HAVE_GD_JPG
2050 /* {{{ Output JPEG image to browser or file */
PHP_FUNCTION(imagejpeg)2051 PHP_FUNCTION(imagejpeg)
2052 {
2053 _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG");
2054 }
2055 /* }}} */
2056 #endif /* HAVE_GD_JPG */
2057
2058 /* {{{ Output WBMP image to browser or file */
PHP_FUNCTION(imagewbmp)2059 PHP_FUNCTION(imagewbmp)
2060 {
2061 zval *imgind;
2062 zend_long foreground_color;
2063 zend_long foreground_color_is_null = 1;
2064 gdImagePtr im;
2065 int i;
2066 gdIOCtx *ctx = NULL;
2067 zval *to_zval = NULL;
2068
2069 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l!", &imgind, gd_image_ce, &to_zval, &foreground_color, &foreground_color_is_null) == FAILURE) {
2070 RETURN_THROWS();
2071 }
2072
2073 im = php_gd_libgdimageptr_from_zval_p(imgind);
2074
2075 if (to_zval != NULL) {
2076 ctx = create_stream_context_from_zval(to_zval);
2077 if (!ctx) {
2078 RETURN_FALSE;
2079 }
2080 } else {
2081 ctx = create_output_context();
2082 }
2083
2084 if (foreground_color_is_null) {
2085 for (i=0; i < gdImageColorsTotal(im); i++) {
2086 if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
2087 break;
2088 }
2089 }
2090
2091 foreground_color = i;
2092 }
2093
2094 gdImageWBMPCtx(im, foreground_color, ctx);
2095
2096 ctx->gd_free(ctx);
2097
2098 RETURN_TRUE;
2099 }
2100 /* }}} */
2101
2102 /* {{{ Output GD image to browser or file */
PHP_FUNCTION(imagegd)2103 PHP_FUNCTION(imagegd)
2104 {
2105 _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD");
2106 }
2107 /* }}} */
2108
2109 /* {{{ Output GD2 image to browser or file */
PHP_FUNCTION(imagegd2)2110 PHP_FUNCTION(imagegd2)
2111 {
2112 _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2");
2113 }
2114 /* }}} */
2115
2116 #ifdef HAVE_GD_BMP
2117 /* {{{ Output BMP image to browser or file */
PHP_FUNCTION(imagebmp)2118 PHP_FUNCTION(imagebmp)
2119 {
2120 zval *imgind;
2121 bool compressed = 1;
2122 gdImagePtr im;
2123 gdIOCtx *ctx = NULL;
2124 zval *to_zval = NULL;
2125
2126 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
2127 RETURN_THROWS();
2128 }
2129
2130 im = php_gd_libgdimageptr_from_zval_p(imgind);
2131
2132 if (to_zval != NULL) {
2133 ctx = create_stream_context_from_zval(to_zval);
2134 if (!ctx) {
2135 RETURN_FALSE;
2136 }
2137 } else {
2138 ctx = create_output_context();
2139 }
2140
2141 gdImageBmpCtx(im, ctx, (int) compressed);
2142
2143 ctx->gd_free(ctx);
2144
2145 RETURN_TRUE;
2146 }
2147 /* }}} */
2148 #endif
2149
2150 /* {{{ Destroy an image - No effect as of PHP 8.0 */
PHP_FUNCTION(imagedestroy)2151 PHP_FUNCTION(imagedestroy)
2152 {
2153 /* This function used to free the resource, as resources are no longer used, it does nothing */
2154 zval *IM;
2155
2156 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
2157 RETURN_THROWS();
2158 }
2159
2160 RETURN_TRUE;
2161 }
2162 /* }}} */
2163
2164 /* {{{ Allocate a color for an image */
PHP_FUNCTION(imagecolorallocate)2165 PHP_FUNCTION(imagecolorallocate)
2166 {
2167 zval *IM;
2168 zend_long red, green, blue;
2169 gdImagePtr im;
2170 int ct = (-1);
2171
2172 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2173 RETURN_THROWS();
2174 }
2175
2176 im = php_gd_libgdimageptr_from_zval_p(IM);
2177
2178 CHECK_RGBA_RANGE(red, Red, 2);
2179 CHECK_RGBA_RANGE(green, Green, 3);
2180 CHECK_RGBA_RANGE(blue, Blue, 4);
2181
2182 ct = gdImageColorAllocate(im, red, green, blue);
2183 if (ct < 0) {
2184 RETURN_FALSE;
2185 }
2186 RETURN_LONG(ct);
2187 }
2188 /* }}} */
2189
2190 /* {{{ Copy the palette from the src image onto the dst image */
PHP_FUNCTION(imagepalettecopy)2191 PHP_FUNCTION(imagepalettecopy)
2192 {
2193 zval *dstim, *srcim;
2194 gdImagePtr dst, src;
2195
2196 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &dstim, gd_image_ce, &srcim, gd_image_ce) == FAILURE) {
2197 RETURN_THROWS();
2198 }
2199
2200 src = php_gd_libgdimageptr_from_zval_p(srcim);
2201 dst = php_gd_libgdimageptr_from_zval_p(dstim);
2202
2203 gdImagePaletteCopy(dst, src);
2204 }
2205 /* }}} */
2206
2207 /* {{{ Get the index of the color of a pixel */
PHP_FUNCTION(imagecolorat)2208 PHP_FUNCTION(imagecolorat)
2209 {
2210 zval *IM;
2211 zend_long x, y;
2212 gdImagePtr im;
2213
2214 ZEND_PARSE_PARAMETERS_START(3, 3)
2215 Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
2216 Z_PARAM_LONG(x)
2217 Z_PARAM_LONG(y)
2218 ZEND_PARSE_PARAMETERS_END();
2219
2220 im = php_gd_libgdimageptr_from_zval_p(IM);
2221
2222 if (gdImageTrueColor(im)) {
2223 if (im->tpixels && gdImageBoundsSafe(im, x, y)) {
2224 RETURN_LONG(gdImageTrueColorPixel(im, x, y));
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 } else {
2230 if (im->pixels && gdImageBoundsSafe(im, x, y)) {
2231 RETURN_LONG(im->pixels[y][x]);
2232 } else {
2233 php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
2234 RETURN_FALSE;
2235 }
2236 }
2237 }
2238 /* }}} */
2239
2240 /* {{{ Get the index of the closest color to the specified color */
PHP_FUNCTION(imagecolorclosest)2241 PHP_FUNCTION(imagecolorclosest)
2242 {
2243 zval *IM;
2244 zend_long red, green, blue;
2245 gdImagePtr im;
2246
2247 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2248 RETURN_THROWS();
2249 }
2250
2251 im = php_gd_libgdimageptr_from_zval_p(IM);
2252
2253 CHECK_RGBA_RANGE(red, Red, 2);
2254 CHECK_RGBA_RANGE(green, Green, 3);
2255 CHECK_RGBA_RANGE(blue, Blue, 4);
2256
2257 RETURN_LONG(gdImageColorClosest(im, red, green, blue));
2258 }
2259 /* }}} */
2260
2261 /* {{{ Get the index of the color which has the hue, white and blackness nearest to the given color */
PHP_FUNCTION(imagecolorclosesthwb)2262 PHP_FUNCTION(imagecolorclosesthwb)
2263 {
2264 zval *IM;
2265 zend_long red, green, blue;
2266 gdImagePtr im;
2267
2268 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2269 RETURN_THROWS();
2270 }
2271
2272 im = php_gd_libgdimageptr_from_zval_p(IM);
2273
2274 CHECK_RGBA_RANGE(red, Red, 2);
2275 CHECK_RGBA_RANGE(green, Green, 3);
2276 CHECK_RGBA_RANGE(blue, Blue, 4);
2277
2278 RETURN_LONG(gdImageColorClosestHWB(im, red, green, blue));
2279 }
2280 /* }}} */
2281
2282 /* {{{ De-allocate a color for an image */
PHP_FUNCTION(imagecolordeallocate)2283 PHP_FUNCTION(imagecolordeallocate)
2284 {
2285 zval *IM;
2286 zend_long index;
2287 int col;
2288 gdImagePtr im;
2289
2290 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &index) == FAILURE) {
2291 RETURN_THROWS();
2292 }
2293
2294 im = php_gd_libgdimageptr_from_zval_p(IM);
2295
2296 /* We can return right away for a truecolor image as deallocating colours is meaningless here */
2297 if (gdImageTrueColor(im)) {
2298 RETURN_TRUE;
2299 }
2300
2301 col = index;
2302
2303 if (col >= 0 && col < gdImageColorsTotal(im)) {
2304 gdImageColorDeallocate(im, col);
2305 RETURN_TRUE;
2306 } else {
2307 zend_argument_value_error(2, "must be between 0 and %d", gdImageColorsTotal(im));
2308 RETURN_THROWS();
2309 }
2310 }
2311 /* }}} */
2312
2313 /* {{{ Get the index of the specified color or its closest possible alternative */
PHP_FUNCTION(imagecolorresolve)2314 PHP_FUNCTION(imagecolorresolve)
2315 {
2316 zval *IM;
2317 zend_long red, green, blue;
2318 gdImagePtr im;
2319
2320 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2321 RETURN_THROWS();
2322 }
2323
2324 im = php_gd_libgdimageptr_from_zval_p(IM);
2325
2326 CHECK_RGBA_RANGE(red, Red, 2);
2327 CHECK_RGBA_RANGE(green, Green, 3);
2328 CHECK_RGBA_RANGE(blue, Blue, 4);
2329
2330 RETURN_LONG(gdImageColorResolve(im, red, green, blue));
2331 }
2332 /* }}} */
2333
2334 /* {{{ Get the index of the specified color */
PHP_FUNCTION(imagecolorexact)2335 PHP_FUNCTION(imagecolorexact)
2336 {
2337 zval *IM;
2338 zend_long red, green, blue;
2339 gdImagePtr im;
2340
2341 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
2342 RETURN_THROWS();
2343 }
2344
2345 im = php_gd_libgdimageptr_from_zval_p(IM);
2346
2347 CHECK_RGBA_RANGE(red, Red, 2);
2348 CHECK_RGBA_RANGE(green, Green, 3);
2349 CHECK_RGBA_RANGE(blue, Blue, 4);
2350
2351 RETURN_LONG(gdImageColorExact(im, red, green, blue));
2352 }
2353 /* }}} */
2354
2355 /* {{{ Set the color for the specified palette index */
PHP_FUNCTION(imagecolorset)2356 PHP_FUNCTION(imagecolorset)
2357 {
2358 zval *IM;
2359 zend_long color, red, green, blue, alpha = 0;
2360 int col;
2361 gdImagePtr im;
2362
2363 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll|l", &IM, gd_image_ce, &color, &red, &green, &blue, &alpha) == FAILURE) {
2364 RETURN_THROWS();
2365 }
2366
2367 im = php_gd_libgdimageptr_from_zval_p(IM);
2368
2369 CHECK_RGBA_RANGE(red, Red, 2);
2370 CHECK_RGBA_RANGE(green, Green, 3);
2371 CHECK_RGBA_RANGE(blue, Blue, 4);
2372
2373 col = color;
2374
2375 if (col >= 0 && col < gdImageColorsTotal(im)) {
2376 im->red[col] = red;
2377 im->green[col] = green;
2378 im->blue[col] = blue;
2379 im->alpha[col] = alpha;
2380 } else {
2381 RETURN_FALSE;
2382 }
2383 }
2384 /* }}} */
2385
2386 /* {{{ Get the colors for an index */
PHP_FUNCTION(imagecolorsforindex)2387 PHP_FUNCTION(imagecolorsforindex)
2388 {
2389 zval *IM;
2390 zend_long index;
2391 int col;
2392 gdImagePtr im;
2393
2394 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &index) == FAILURE) {
2395 RETURN_THROWS();
2396 }
2397
2398 im = php_gd_libgdimageptr_from_zval_p(IM);
2399
2400 col = index;
2401
2402 if ((col >= 0 && gdImageTrueColor(im)) || (!gdImageTrueColor(im) && col >= 0 && col < gdImageColorsTotal(im))) {
2403 array_init(return_value);
2404
2405 add_assoc_long(return_value,"red", gdImageRed(im,col));
2406 add_assoc_long(return_value,"green", gdImageGreen(im,col));
2407 add_assoc_long(return_value,"blue", gdImageBlue(im,col));
2408 add_assoc_long(return_value,"alpha", gdImageAlpha(im,col));
2409 } else {
2410 zend_argument_value_error(2, "is out of range");
2411 RETURN_THROWS();
2412 }
2413 }
2414 /* }}} */
2415
2416 /* {{{ Apply a gamma correction to a GD image */
PHP_FUNCTION(imagegammacorrect)2417 PHP_FUNCTION(imagegammacorrect)
2418 {
2419 zval *IM;
2420 gdImagePtr im;
2421 int i;
2422 double input, output, gamma;
2423
2424 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odd", &IM, gd_image_ce, &input, &output) == FAILURE) {
2425 RETURN_THROWS();
2426 }
2427
2428 if (input <= 0.0) {
2429 zend_argument_value_error(2, "must be greater than 0");
2430 RETURN_THROWS();
2431 }
2432
2433 if (output <= 0.0) {
2434 zend_argument_value_error(3, "must be greater than 0");
2435 RETURN_THROWS();
2436 }
2437
2438 gamma = input / output;
2439
2440 im = php_gd_libgdimageptr_from_zval_p(IM);
2441
2442 if (gdImageTrueColor(im)) {
2443 int x, y, c;
2444
2445 for (y = 0; y < gdImageSY(im); y++) {
2446 for (x = 0; x < gdImageSX(im); x++) {
2447 c = gdImageGetPixel(im, x, y);
2448 gdImageSetPixel(im, x, y,
2449 gdTrueColorAlpha(
2450 (int) ((pow((gdTrueColorGetRed(c) / 255.0), gamma) * 255) + .5),
2451 (int) ((pow((gdTrueColorGetGreen(c) / 255.0), gamma) * 255) + .5),
2452 (int) ((pow((gdTrueColorGetBlue(c) / 255.0), gamma) * 255) + .5),
2453 gdTrueColorGetAlpha(c)
2454 )
2455 );
2456 }
2457 }
2458 RETURN_TRUE;
2459 }
2460
2461 for (i = 0; i < gdImageColorsTotal(im); i++) {
2462 im->red[i] = (int)((pow((im->red[i] / 255.0), gamma) * 255) + .5);
2463 im->green[i] = (int)((pow((im->green[i] / 255.0), gamma) * 255) + .5);
2464 im->blue[i] = (int)((pow((im->blue[i] / 255.0), gamma) * 255) + .5);
2465 }
2466
2467 RETURN_TRUE;
2468 }
2469 /* }}} */
2470
2471 /* {{{ Set a single pixel */
PHP_FUNCTION(imagesetpixel)2472 PHP_FUNCTION(imagesetpixel)
2473 {
2474 zval *IM;
2475 zend_long x, y, col;
2476 gdImagePtr im;
2477
2478 ZEND_PARSE_PARAMETERS_START(4, 4)
2479 Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
2480 Z_PARAM_LONG(x)
2481 Z_PARAM_LONG(y)
2482 Z_PARAM_LONG(col)
2483 ZEND_PARSE_PARAMETERS_END();
2484
2485 im = php_gd_libgdimageptr_from_zval_p(IM);
2486
2487 gdImageSetPixel(im, x, y, col);
2488 RETURN_TRUE;
2489 }
2490 /* }}} */
2491
2492 /* {{{ Draw a line */
PHP_FUNCTION(imageline)2493 PHP_FUNCTION(imageline)
2494 {
2495 zval *IM;
2496 zend_long x1, y1, x2, y2, col;
2497 gdImagePtr im;
2498
2499 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2500 RETURN_THROWS();
2501 }
2502
2503 im = php_gd_libgdimageptr_from_zval_p(IM);
2504
2505 if (im->AA) {
2506 gdImageSetAntiAliased(im, col);
2507 col = gdAntiAliased;
2508 }
2509 gdImageLine(im, x1, y1, x2, y2, col);
2510 RETURN_TRUE;
2511 }
2512 /* }}} */
2513
2514 /* {{{ Draw a dashed line */
PHP_FUNCTION(imagedashedline)2515 PHP_FUNCTION(imagedashedline)
2516 {
2517 zval *IM;
2518 zend_long x1, y1, x2, y2, col;
2519 gdImagePtr im;
2520
2521 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2522 RETURN_THROWS();
2523 }
2524
2525 im = php_gd_libgdimageptr_from_zval_p(IM);
2526
2527 gdImageDashedLine(im, x1, y1, x2, y2, col);
2528 RETURN_TRUE;
2529 }
2530 /* }}} */
2531
2532 /* {{{ Draw a rectangle */
PHP_FUNCTION(imagerectangle)2533 PHP_FUNCTION(imagerectangle)
2534 {
2535 zval *IM;
2536 zend_long x1, y1, x2, y2, col;
2537 gdImagePtr im;
2538
2539 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2540 RETURN_THROWS();
2541 }
2542
2543 im = php_gd_libgdimageptr_from_zval_p(IM);
2544
2545 gdImageRectangle(im, x1, y1, x2, y2, col);
2546 RETURN_TRUE;
2547 }
2548 /* }}} */
2549
2550 /* {{{ Draw a filled rectangle */
PHP_FUNCTION(imagefilledrectangle)2551 PHP_FUNCTION(imagefilledrectangle)
2552 {
2553 zval *IM;
2554 zend_long x1, y1, x2, y2, col;
2555 gdImagePtr im;
2556
2557 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2558 RETURN_THROWS();
2559 }
2560
2561 im = php_gd_libgdimageptr_from_zval_p(IM);
2562 gdImageFilledRectangle(im, x1, y1, x2, y2, col);
2563 RETURN_TRUE;
2564 }
2565 /* }}} */
2566
2567 /* {{{ Draw a partial ellipse */
PHP_FUNCTION(imagearc)2568 PHP_FUNCTION(imagearc)
2569 {
2570 zval *IM;
2571 zend_long cx, cy, w, h, ST, E, col;
2572 gdImagePtr im;
2573 int e, st;
2574
2575 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col) == FAILURE) {
2576 RETURN_THROWS();
2577 }
2578
2579 im = php_gd_libgdimageptr_from_zval_p(IM);
2580
2581 e = E;
2582 if (e < 0) {
2583 e %= 360;
2584 }
2585
2586 st = ST;
2587 if (st < 0) {
2588 st %= 360;
2589 }
2590
2591 gdImageArc(im, cx, cy, w, h, st, e, col);
2592 RETURN_TRUE;
2593 }
2594 /* }}} */
2595
2596 /* {{{ Draw an ellipse */
PHP_FUNCTION(imageellipse)2597 PHP_FUNCTION(imageellipse)
2598 {
2599 zval *IM;
2600 zend_long cx, cy, w, h, color;
2601 gdImagePtr im;
2602
2603 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &color) == FAILURE) {
2604 RETURN_THROWS();
2605 }
2606
2607 im = php_gd_libgdimageptr_from_zval_p(IM);
2608
2609 gdImageEllipse(im, cx, cy, w, h, color);
2610 RETURN_TRUE;
2611 }
2612 /* }}} */
2613
2614 /* {{{ Flood fill to specific color */
PHP_FUNCTION(imagefilltoborder)2615 PHP_FUNCTION(imagefilltoborder)
2616 {
2617 zval *IM;
2618 zend_long x, y, border, col;
2619 gdImagePtr im;
2620
2621 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &x, &y, &border, &col) == FAILURE) {
2622 RETURN_THROWS();
2623 }
2624
2625 im = php_gd_libgdimageptr_from_zval_p(IM);
2626
2627 gdImageFillToBorder(im, x, y, border, col);
2628 RETURN_TRUE;
2629 }
2630 /* }}} */
2631
2632 /* {{{ Flood fill */
PHP_FUNCTION(imagefill)2633 PHP_FUNCTION(imagefill)
2634 {
2635 zval *IM;
2636 zend_long x, y, col;
2637 gdImagePtr im;
2638
2639 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &x, &y, &col) == FAILURE) {
2640 RETURN_THROWS();
2641 }
2642
2643 im = php_gd_libgdimageptr_from_zval_p(IM);
2644
2645 gdImageFill(im, x, y, col);
2646 RETURN_TRUE;
2647 }
2648 /* }}} */
2649
2650 /* {{{ Find out the number of colors in an image's palette */
PHP_FUNCTION(imagecolorstotal)2651 PHP_FUNCTION(imagecolorstotal)
2652 {
2653 zval *IM;
2654 gdImagePtr im;
2655
2656 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
2657 RETURN_THROWS();
2658 }
2659
2660 im = php_gd_libgdimageptr_from_zval_p(IM);
2661
2662 RETURN_LONG(gdImageColorsTotal(im));
2663 }
2664 /* }}} */
2665
2666 /* {{{ Define a color as transparent */
PHP_FUNCTION(imagecolortransparent)2667 PHP_FUNCTION(imagecolortransparent)
2668 {
2669 zval *IM;
2670 zend_long COL = 0;
2671 bool COL_IS_NULL = 1;
2672 gdImagePtr im;
2673
2674 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) {
2675 RETURN_THROWS();
2676 }
2677
2678 im = php_gd_libgdimageptr_from_zval_p(IM);
2679
2680 if (!COL_IS_NULL) {
2681 gdImageColorTransparent(im, COL);
2682 }
2683
2684 RETURN_LONG(gdImageGetTransparent(im));
2685 }
2686 /* }}} */
2687
2688 /* {{{ Enable or disable interlace */
PHP_FUNCTION(imageinterlace)2689 PHP_FUNCTION(imageinterlace)
2690 {
2691 zval *IM;
2692 bool INT = 0;
2693 bool INT_IS_NULL = 1;
2694 gdImagePtr im;
2695
2696 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) {
2697 RETURN_THROWS();
2698 }
2699
2700 im = php_gd_libgdimageptr_from_zval_p(IM);
2701
2702 if (!INT_IS_NULL) {
2703 gdImageInterlace(im, INT);
2704 }
2705
2706 RETURN_BOOL(gdImageGetInterlaced(im));
2707 }
2708 /* }}} */
2709
2710 /* {{{ php_imagepolygon
2711 arg = -1 open polygon
2712 arg = 0 normal polygon
2713 arg = 1 filled polygon */
2714 /* im, points, num_points, col */
php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS,int filled)2715 static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
2716 {
2717 zval *IM, *POINTS;
2718 zend_long NPOINTS, COL;
2719 bool COL_IS_NULL = 1;
2720 zval *var = NULL;
2721 gdImagePtr im;
2722 gdPointPtr points;
2723 int npoints, col, nelem, i;
2724
2725 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l!", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL, &COL_IS_NULL) == FAILURE) {
2726 RETURN_THROWS();
2727 }
2728 if (COL_IS_NULL) {
2729 COL = NPOINTS;
2730 NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
2731 if (NPOINTS % 2 != 0) {
2732 zend_argument_value_error(2, "must have an even number of elements");
2733 RETURN_THROWS();
2734 }
2735 NPOINTS /= 2;
2736 } else {
2737 php_error_docref(NULL, E_DEPRECATED, "Using the $num_points parameter is deprecated");
2738 }
2739
2740 im = php_gd_libgdimageptr_from_zval_p(IM);
2741
2742 npoints = NPOINTS;
2743 col = COL;
2744
2745 nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
2746 if (npoints < 3) {
2747 zend_argument_value_error(3, "must be greater than or equal to 3");
2748 RETURN_THROWS();
2749 }
2750
2751 if (nelem < npoints * 2) {
2752 zend_value_error("Trying to use %d points in array with only %d points", npoints, nelem/2);
2753 RETURN_THROWS();
2754 }
2755
2756 points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
2757
2758 for (i = 0; i < npoints; i++) {
2759 if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2))) != NULL) {
2760 points[i].x = zval_get_long(var);
2761 }
2762 if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2) + 1)) != NULL) {
2763 points[i].y = zval_get_long(var);
2764 }
2765 }
2766
2767 if (im->AA) {
2768 gdImageSetAntiAliased(im, col);
2769 col = gdAntiAliased;
2770 }
2771 switch (filled) {
2772 case -1:
2773 gdImageOpenPolygon(im, points, npoints, col);
2774 break;
2775 case 0:
2776 gdImagePolygon(im, points, npoints, col);
2777 break;
2778 case 1:
2779 gdImageFilledPolygon(im, points, npoints, col);
2780 break;
2781 }
2782
2783 efree(points);
2784 RETURN_TRUE;
2785 }
2786 /* }}} */
2787
2788 /* {{{ Draw a polygon */
PHP_FUNCTION(imagepolygon)2789 PHP_FUNCTION(imagepolygon)
2790 {
2791 php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2792 }
2793 /* }}} */
2794
2795 /* {{{ Draw a polygon */
PHP_FUNCTION(imageopenpolygon)2796 PHP_FUNCTION(imageopenpolygon)
2797 {
2798 php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1);
2799 }
2800 /* }}} */
2801
2802 /* {{{ Draw a filled polygon */
PHP_FUNCTION(imagefilledpolygon)2803 PHP_FUNCTION(imagefilledpolygon)
2804 {
2805 php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2806 }
2807 /* }}} */
2808
2809 /* {{{ php_find_gd_font */
php_find_gd_font(zend_object * font_obj,zend_long font_int)2810 static gdFontPtr php_find_gd_font(zend_object *font_obj, zend_long font_int)
2811 {
2812 if (font_obj) {
2813 return php_gd_font_object_from_zend_object(font_obj)->font;
2814 }
2815
2816 switch (font_int) {
2817 case 1: return gdFontTiny;
2818 case 2: return gdFontSmall;
2819 case 3: return gdFontMediumBold;
2820 case 4: return gdFontLarge;
2821 case 5: return gdFontGiant;
2822 }
2823
2824 return font_int < 1 ? gdFontTiny : gdFontGiant;
2825 }
2826 /* }}} */
2827
2828 /* {{{ php_imagefontsize
2829 * arg = 0 ImageFontWidth
2830 * arg = 1 ImageFontHeight
2831 */
php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS,int arg)2832 static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg)
2833 {
2834 zend_object *font_obj;
2835 zend_long font_int;
2836 gdFontPtr font;
2837
2838 ZEND_PARSE_PARAMETERS_START(1, 1)
2839 Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int)
2840 ZEND_PARSE_PARAMETERS_END();
2841
2842 font = php_find_gd_font(font_obj, font_int);
2843 RETURN_LONG(arg ? font->h : font->w);
2844 }
2845 /* }}} */
2846
2847 /* {{{ Get font width */
PHP_FUNCTION(imagefontwidth)2848 PHP_FUNCTION(imagefontwidth)
2849 {
2850 php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2851 }
2852 /* }}} */
2853
2854 /* {{{ Get font height */
PHP_FUNCTION(imagefontheight)2855 PHP_FUNCTION(imagefontheight)
2856 {
2857 php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2858 }
2859 /* }}} */
2860
2861 /* {{{ php_gdimagecharup
2862 * workaround for a bug in gd 1.2 */
php_gdimagecharup(gdImagePtr im,gdFontPtr f,int x,int y,int c,int color)2863 static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color)
2864 {
2865 int cx, cy, px, py, fline;
2866 cx = 0;
2867 cy = 0;
2868
2869 if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
2870 return;
2871 }
2872
2873 fline = (c - f->offset) * f->h * f->w;
2874 for (py = y; (py > (y - f->w)); py--) {
2875 for (px = x; (px < (x + f->h)); px++) {
2876 if (f->data[fline + cy * f->w + cx]) {
2877 gdImageSetPixel(im, px, py, color);
2878 }
2879 cy++;
2880 }
2881 cy = 0;
2882 cx++;
2883 }
2884 }
2885 /* }}} */
2886
2887 /* {{{ php_imagechar
2888 * arg = 0 ImageChar
2889 * arg = 1 ImageCharUp
2890 * arg = 2 ImageString
2891 * arg = 3 ImageStringUp
2892 */
php_imagechar(INTERNAL_FUNCTION_PARAMETERS,int mode)2893 static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode)
2894 {
2895 zval *IM;
2896 zend_long X, Y, COL;
2897 char *C;
2898 size_t C_len;
2899 gdImagePtr im;
2900 int ch = 0, col, x, y, i, l = 0;
2901 unsigned char *str = NULL;
2902 zend_object *font_obj;
2903 zend_long font_int;
2904 gdFontPtr font;
2905
2906 ZEND_PARSE_PARAMETERS_START(6, 6)
2907 Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
2908 Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int)
2909 Z_PARAM_LONG(X)
2910 Z_PARAM_LONG(Y)
2911 Z_PARAM_STRING(C, C_len)
2912 Z_PARAM_LONG(COL)
2913 ZEND_PARSE_PARAMETERS_END();
2914
2915 im = php_gd_libgdimageptr_from_zval_p(IM);
2916
2917 col = COL;
2918
2919 if (mode < 2) {
2920 ch = (int)((unsigned char)*C);
2921 } else {
2922 str = (unsigned char *) estrndup(C, C_len);
2923 l = strlen((char *)str);
2924 }
2925
2926 y = Y;
2927 x = X;
2928
2929 font = php_find_gd_font(font_obj, font_int);
2930
2931 switch (mode) {
2932 case 0:
2933 gdImageChar(im, font, x, y, ch, col);
2934 break;
2935 case 1:
2936 php_gdimagecharup(im, font, x, y, ch, col);
2937 break;
2938 case 2:
2939 for (i = 0; (i < l); i++) {
2940 gdImageChar(im, font, x, y, (int) ((unsigned char) str[i]), col);
2941 x += font->w;
2942 }
2943 break;
2944 case 3: {
2945 for (i = 0; (i < l); i++) {
2946 /* php_gdimagecharup(im, font, x, y, (int) str[i], col); */
2947 gdImageCharUp(im, font, x, y, (int) str[i], col);
2948 y -= font->w;
2949 }
2950 break;
2951 }
2952 }
2953 if (str) {
2954 efree(str);
2955 }
2956 RETURN_TRUE;
2957 }
2958 /* }}} */
2959
2960 /* {{{ Draw a character */
PHP_FUNCTION(imagechar)2961 PHP_FUNCTION(imagechar)
2962 {
2963 php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2964 }
2965 /* }}} */
2966
2967 /* {{{ Draw a character rotated 90 degrees counter-clockwise */
PHP_FUNCTION(imagecharup)2968 PHP_FUNCTION(imagecharup)
2969 {
2970 php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2971 }
2972 /* }}} */
2973
2974 /* {{{ Draw a string horizontally */
PHP_FUNCTION(imagestring)2975 PHP_FUNCTION(imagestring)
2976 {
2977 php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
2978 }
2979 /* }}} */
2980
2981 /* {{{ Draw a string vertically - rotated 90 degrees counter-clockwise */
PHP_FUNCTION(imagestringup)2982 PHP_FUNCTION(imagestringup)
2983 {
2984 php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
2985 }
2986 /* }}} */
2987
2988 /* {{{ Copy part of an image */
PHP_FUNCTION(imagecopy)2989 PHP_FUNCTION(imagecopy)
2990 {
2991 zval *SIM, *DIM;
2992 zend_long SX, SY, SW, SH, DX, DY;
2993 gdImagePtr im_dst, im_src;
2994 int srcH, srcW, srcY, srcX, dstY, dstX;
2995
2996 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH) == FAILURE) {
2997 RETURN_THROWS();
2998 }
2999
3000 im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3001 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3002
3003 srcX = SX;
3004 srcY = SY;
3005 srcH = SH;
3006 srcW = SW;
3007 dstX = DX;
3008 dstY = DY;
3009
3010 gdImageCopy(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH);
3011 RETURN_TRUE;
3012 }
3013 /* }}} */
3014
3015 /* {{{ Merge one part of an image with another */
PHP_FUNCTION(imagecopymerge)3016 PHP_FUNCTION(imagecopymerge)
3017 {
3018 zval *SIM, *DIM;
3019 zend_long SX, SY, SW, SH, DX, DY, PCT;
3020 gdImagePtr im_dst, im_src;
3021 int srcH, srcW, srcY, srcX, dstY, dstX, pct;
3022
3023 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOlllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
3024 RETURN_THROWS();
3025 }
3026
3027 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3028 im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3029
3030 srcX = SX;
3031 srcY = SY;
3032 srcH = SH;
3033 srcW = SW;
3034 dstX = DX;
3035 dstY = DY;
3036 pct = PCT;
3037
3038 gdImageCopyMerge(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
3039 RETURN_TRUE;
3040 }
3041 /* }}} */
3042
3043 /* {{{ Merge one part of an image with another */
PHP_FUNCTION(imagecopymergegray)3044 PHP_FUNCTION(imagecopymergegray)
3045 {
3046 zval *SIM, *DIM;
3047 zend_long SX, SY, SW, SH, DX, DY, PCT;
3048 gdImagePtr im_dst, im_src;
3049 int srcH, srcW, srcY, srcX, dstY, dstX, pct;
3050
3051 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOlllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
3052 RETURN_THROWS();
3053 }
3054
3055 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3056 im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3057
3058 srcX = SX;
3059 srcY = SY;
3060 srcH = SH;
3061 srcW = SW;
3062 dstX = DX;
3063 dstY = DY;
3064 pct = PCT;
3065
3066 gdImageCopyMergeGray(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
3067 RETURN_TRUE;
3068 }
3069 /* }}} */
3070
3071 /* {{{ Copy and resize part of an image */
PHP_FUNCTION(imagecopyresized)3072 PHP_FUNCTION(imagecopyresized)
3073 {
3074 zval *SIM, *DIM;
3075 zend_long SX, SY, SW, SH, DX, DY, DW, DH;
3076 gdImagePtr im_dst, im_src;
3077 int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
3078
3079 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) {
3080 RETURN_THROWS();
3081 }
3082
3083 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3084 im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
3085
3086 srcX = SX;
3087 srcY = SY;
3088 srcH = SH;
3089 srcW = SW;
3090 dstX = DX;
3091 dstY = DY;
3092 dstH = DH;
3093 dstW = DW;
3094
3095 if (dstW <= 0) {
3096 zend_argument_value_error(7, "must be greater than 0");
3097 RETURN_THROWS();
3098 }
3099
3100 if (dstH <= 0) {
3101 zend_argument_value_error(8, "must be greater than 0");
3102 RETURN_THROWS();
3103 }
3104
3105 if (srcW <= 0) {
3106 zend_argument_value_error(9, "must be greater than 0");
3107 RETURN_THROWS();
3108 }
3109
3110 if (srcH <= 0) {
3111 zend_argument_value_error(10, "must be greater than 0");
3112 RETURN_THROWS();
3113 }
3114
3115 gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
3116 RETURN_TRUE;
3117 }
3118 /* }}} */
3119
3120 /* {{{ Get image width */
PHP_FUNCTION(imagesx)3121 PHP_FUNCTION(imagesx)
3122 {
3123 zval *IM;
3124 gdImagePtr im;
3125
3126 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
3127 RETURN_THROWS();
3128 }
3129
3130 im = php_gd_libgdimageptr_from_zval_p(IM);
3131
3132 RETURN_LONG(gdImageSX(im));
3133 }
3134 /* }}} */
3135
3136 /* {{{ Get image height */
PHP_FUNCTION(imagesy)3137 PHP_FUNCTION(imagesy)
3138 {
3139 zval *IM;
3140 gdImagePtr im;
3141
3142 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
3143 RETURN_THROWS();
3144 }
3145
3146 im = php_gd_libgdimageptr_from_zval_p(IM);
3147
3148 RETURN_LONG(gdImageSY(im));
3149 }
3150 /* }}} */
3151
3152 /* {{{ Set the clipping rectangle. */
PHP_FUNCTION(imagesetclip)3153 PHP_FUNCTION(imagesetclip)
3154 {
3155 zval *im_zval;
3156 gdImagePtr im;
3157 zend_long x1, y1, x2, y2;
3158
3159 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &im_zval, gd_image_ce, &x1, &y1, &x2, &y2) == FAILURE) {
3160 RETURN_THROWS();
3161 }
3162
3163 im = php_gd_libgdimageptr_from_zval_p(im_zval);
3164
3165 gdImageSetClip(im, x1, y1, x2, y2);
3166 RETURN_TRUE;
3167 }
3168 /* }}} */
3169
3170 /* {{{ Get the clipping rectangle. */
PHP_FUNCTION(imagegetclip)3171 PHP_FUNCTION(imagegetclip)
3172 {
3173 zval *im_zval;
3174 gdImagePtr im;
3175 int x1, y1, x2, y2;
3176
3177 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &im_zval, gd_image_ce) == FAILURE) {
3178 RETURN_THROWS();
3179 }
3180
3181 im = php_gd_libgdimageptr_from_zval_p(im_zval);
3182
3183 gdImageGetClip(im, &x1, &y1, &x2, &y2);
3184
3185 array_init(return_value);
3186 add_next_index_long(return_value, x1);
3187 add_next_index_long(return_value, y1);
3188 add_next_index_long(return_value, x2);
3189 add_next_index_long(return_value, y2);
3190 }
3191 /* }}} */
3192
3193 #define TTFTEXT_DRAW 0
3194 #define TTFTEXT_BBOX 1
3195
3196 #ifdef HAVE_GD_FREETYPE
3197 /* {{{ Give the bounding box of a text using fonts via freetype2 */
PHP_FUNCTION(imageftbbox)3198 PHP_FUNCTION(imageftbbox)
3199 {
3200 php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX);
3201 }
3202 /* }}} */
3203
3204 /* {{{ Write text to the image using fonts via freetype2 */
PHP_FUNCTION(imagefttext)3205 PHP_FUNCTION(imagefttext)
3206 {
3207 php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW);
3208 }
3209 /* }}} */
3210
3211 /* {{{ php_imagettftext_common */
php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS,int mode)3212 static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int mode)
3213 {
3214 zval *IM, *EXT = NULL;
3215 gdImagePtr im=NULL;
3216 zend_long col = -1, x = 0, y = 0;
3217 size_t str_len, fontname_len;
3218 int i, brect[8];
3219 double ptsize, angle;
3220 char *str = NULL, *fontname = NULL;
3221 char *error = NULL;
3222 gdFTStringExtra strex = {0};
3223
3224 if (mode == TTFTEXT_BBOX) {
3225 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ddss|a", &ptsize, &angle, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
3226 RETURN_THROWS();
3227 }
3228 } else {
3229 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) {
3230 RETURN_THROWS();
3231 }
3232 im = php_gd_libgdimageptr_from_zval_p(IM);
3233 }
3234
3235 /* convert angle to radians */
3236 angle = angle * (M_PI/180);
3237
3238 if (EXT) { /* parse extended info */
3239 zval *item;
3240 zend_string *key;
3241
3242 /* walk the assoc array */
3243 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(EXT), key, item) {
3244 if (key == NULL) {
3245 continue;
3246 }
3247 if (zend_string_equals_literal(key, "linespacing")) {
3248 strex.flags |= gdFTEX_LINESPACE;
3249 strex.linespacing = zval_get_double(item);
3250 }
3251 } ZEND_HASH_FOREACH_END();
3252 }
3253
3254 #ifdef VIRTUAL_DIR
3255 {
3256 char tmp_font_path[MAXPATHLEN];
3257
3258 if (!VCWD_REALPATH(fontname, tmp_font_path)) {
3259 fontname = NULL;
3260 }
3261 }
3262 #endif /* VIRTUAL_DIR */
3263
3264 PHP_GD_CHECK_OPEN_BASEDIR(fontname, "Invalid font filename");
3265
3266 if (EXT) {
3267 error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
3268 } else {
3269 error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
3270 }
3271
3272 if (error) {
3273 php_error_docref(NULL, E_WARNING, "%s", error);
3274 RETURN_FALSE;
3275 }
3276
3277 array_init(return_value);
3278
3279 /* return array with the text's bounding box */
3280 for (i = 0; i < 8; i++) {
3281 add_next_index_long(return_value, brect[i]);
3282 }
3283 }
3284 /* }}} */
3285 #endif /* HAVE_GD_FREETYPE */
3286
3287 /* Section Filters */
3288 #define PHP_GD_SINGLE_RES \
3289 zval *SIM; \
3290 gdImagePtr im_src; \
3291 if (zend_parse_parameters(1, "O", &SIM, gd_image_ce) == FAILURE) { \
3292 RETURN_THROWS(); \
3293 } \
3294 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3295
php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)3296 static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
3297 {
3298 PHP_GD_SINGLE_RES
3299
3300 if (gdImageNegate(im_src) == 1) {
3301 RETURN_TRUE;
3302 }
3303
3304 RETURN_FALSE;
3305 }
3306
php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)3307 static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
3308 {
3309 PHP_GD_SINGLE_RES
3310
3311 if (gdImageGrayScale(im_src) == 1) {
3312 RETURN_TRUE;
3313 }
3314
3315 RETURN_FALSE;
3316 }
3317
php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)3318 static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
3319 {
3320 zval *SIM;
3321 gdImagePtr im_src;
3322 zend_long brightness, tmp;
3323
3324 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &SIM, gd_image_ce, &tmp, &brightness) == FAILURE) {
3325 RETURN_THROWS();
3326 }
3327
3328 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3329
3330 if (gdImageBrightness(im_src, (int)brightness) == 1) {
3331 RETURN_TRUE;
3332 }
3333
3334 RETURN_FALSE;
3335 }
3336
php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)3337 static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
3338 {
3339 zval *SIM;
3340 gdImagePtr im_src;
3341 zend_long contrast, tmp;
3342
3343 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &SIM, gd_image_ce, &tmp, &contrast) == FAILURE) {
3344 RETURN_THROWS();
3345 }
3346
3347 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3348
3349 if (gdImageContrast(im_src, (int)contrast) == 1) {
3350 RETURN_TRUE;
3351 }
3352
3353 RETURN_FALSE;
3354 }
3355
php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)3356 static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
3357 {
3358 zval *SIM;
3359 gdImagePtr im_src;
3360 zend_long r,g,b,tmp;
3361 zend_long a = 0;
3362
3363 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll|l", &SIM, gd_image_ce, &tmp, &r, &g, &b, &a) == FAILURE) {
3364 RETURN_THROWS();
3365 }
3366
3367 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3368
3369 if (gdImageColor(im_src, (int) r, (int) g, (int) b, (int) a) == 1) {
3370 RETURN_TRUE;
3371 }
3372
3373 RETURN_FALSE;
3374 }
3375
php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)3376 static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
3377 {
3378 PHP_GD_SINGLE_RES
3379
3380 if (gdImageEdgeDetectQuick(im_src) == 1) {
3381 RETURN_TRUE;
3382 }
3383
3384 RETURN_FALSE;
3385 }
3386
php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)3387 static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
3388 {
3389 PHP_GD_SINGLE_RES
3390
3391 if (gdImageEmboss(im_src) == 1) {
3392 RETURN_TRUE;
3393 }
3394
3395 RETURN_FALSE;
3396 }
3397
php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)3398 static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
3399 {
3400 PHP_GD_SINGLE_RES
3401
3402 if (gdImageGaussianBlur(im_src) == 1) {
3403 RETURN_TRUE;
3404 }
3405
3406 RETURN_FALSE;
3407 }
3408
php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)3409 static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
3410 {
3411 PHP_GD_SINGLE_RES
3412
3413 if (gdImageSelectiveBlur(im_src) == 1) {
3414 RETURN_TRUE;
3415 }
3416
3417 RETURN_FALSE;
3418 }
3419
php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)3420 static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
3421 {
3422 PHP_GD_SINGLE_RES
3423
3424 if (gdImageMeanRemoval(im_src) == 1) {
3425 RETURN_TRUE;
3426 }
3427
3428 RETURN_FALSE;
3429 }
3430
php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)3431 static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
3432 {
3433 zval *SIM;
3434 zend_long tmp;
3435 gdImagePtr im_src;
3436 double weight;
3437
3438 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Old", &SIM, gd_image_ce, &tmp, &weight) == FAILURE) {
3439 RETURN_THROWS();
3440 }
3441
3442 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3443
3444 if (gdImageSmooth(im_src, (float)weight)==1) {
3445 RETURN_TRUE;
3446 }
3447
3448 RETURN_FALSE;
3449 }
3450
php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)3451 static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)
3452 {
3453 zval *IM;
3454 gdImagePtr im;
3455 zend_long tmp, blocksize;
3456 bool mode = 0;
3457
3458 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll|b", &IM, gd_image_ce, &tmp, &blocksize, &mode) == FAILURE) {
3459 RETURN_THROWS();
3460 }
3461
3462 im = php_gd_libgdimageptr_from_zval_p(IM);
3463
3464 if (gdImagePixelate(im, (int) blocksize, (const unsigned int) mode)) {
3465 RETURN_TRUE;
3466 }
3467
3468 RETURN_FALSE;
3469 }
3470
php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)3471 static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
3472 {
3473 zval *IM;
3474 zval *hash_colors = NULL;
3475 gdImagePtr im;
3476 zend_long tmp;
3477 zend_long scatter_sub, scatter_plus;
3478
3479 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll|a", &IM, gd_image_ce, &tmp, &scatter_sub, &scatter_plus, &hash_colors) == FAILURE) {
3480 RETURN_THROWS();
3481 }
3482
3483 im = php_gd_libgdimageptr_from_zval_p(IM);
3484
3485 if (hash_colors) {
3486 uint32_t i = 0;
3487 uint32_t num_colors = zend_hash_num_elements(Z_ARRVAL_P(hash_colors));
3488 zval *color;
3489 int *colors;
3490
3491 if (num_colors == 0) {
3492 RETURN_BOOL(gdImageScatter(im, (int)scatter_sub, (int)scatter_plus));
3493 }
3494
3495 colors = emalloc(num_colors * sizeof(int));
3496
3497 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hash_colors), color) {
3498 *(colors + i++) = (int) zval_get_long(color);
3499 } ZEND_HASH_FOREACH_END();
3500
3501 RETVAL_BOOL(gdImageScatterColor(im, (int)scatter_sub, (int)scatter_plus, colors, num_colors));
3502
3503 efree(colors);
3504 } else {
3505 RETURN_BOOL(gdImageScatter(im, (int) scatter_sub, (int) scatter_plus));
3506 }
3507 }
3508
3509 /* {{{ Applies Filter an image using a custom angle */
PHP_FUNCTION(imagefilter)3510 PHP_FUNCTION(imagefilter)
3511 {
3512 zval *tmp;
3513
3514 typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
3515 zend_long filtertype;
3516 image_filter filters[] =
3517 {
3518 php_image_filter_negate ,
3519 php_image_filter_grayscale,
3520 php_image_filter_brightness,
3521 php_image_filter_contrast,
3522 php_image_filter_colorize,
3523 php_image_filter_edgedetect,
3524 php_image_filter_emboss,
3525 php_image_filter_gaussian_blur,
3526 php_image_filter_selective_blur,
3527 php_image_filter_mean_removal,
3528 php_image_filter_smooth,
3529 php_image_filter_pixelate,
3530 php_image_filter_scatter
3531 };
3532
3533 if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > IMAGE_FILTER_MAX_ARGS) {
3534 WRONG_PARAM_COUNT;
3535 } else if (zend_parse_parameters(2, "Ol", &tmp, gd_image_ce, &filtertype) == FAILURE) {
3536 RETURN_THROWS();
3537 }
3538
3539 if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
3540 filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
3541 }
3542 }
3543 /* }}} */
3544
3545 /* {{{ Apply a 3x3 convolution matrix, using coefficient div and offset */
PHP_FUNCTION(imageconvolution)3546 PHP_FUNCTION(imageconvolution)
3547 {
3548 zval *SIM, *hash_matrix;
3549 zval *var = NULL, *var2 = NULL;
3550 gdImagePtr im_src = NULL;
3551 double div, offset;
3552 int nelem, i, j, res;
3553 float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
3554
3555 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oadd", &SIM, gd_image_ce, &hash_matrix, &div, &offset) == FAILURE) {
3556 RETURN_THROWS();
3557 }
3558
3559 im_src = php_gd_libgdimageptr_from_zval_p(SIM);
3560
3561 nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
3562 if (nelem != 3) {
3563 zend_argument_value_error(2, "must be a 3x3 array");
3564 RETURN_THROWS();
3565 }
3566
3567 for (i=0; i<3; i++) {
3568 if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) {
3569 if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) {
3570 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)));
3571 RETURN_THROWS();
3572 }
3573
3574 for (j=0; j<3; j++) {
3575 if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) {
3576 matrix[i][j] = (float) zval_get_double(var2);
3577 } else {
3578 zend_argument_value_error(2, "must be a 3x3 array, matrix[%d][%d] cannot be found (missing integer key)", i, j);
3579 RETURN_THROWS();
3580 }
3581 }
3582 }
3583 }
3584 res = gdImageConvolution(im_src, matrix, (float)div, (float)offset);
3585
3586 if (res) {
3587 RETURN_TRUE;
3588 } else {
3589 RETURN_FALSE;
3590 }
3591 }
3592 /* }}} */
3593 /* End section: Filters */
3594
3595 /* {{{ Flip an image (in place) horizontally, vertically or both directions. */
PHP_FUNCTION(imageflip)3596 PHP_FUNCTION(imageflip)
3597 {
3598 zval *IM;
3599 zend_long mode;
3600 gdImagePtr im;
3601
3602 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &mode) == FAILURE) {
3603 RETURN_THROWS();
3604 }
3605
3606 im = php_gd_libgdimageptr_from_zval_p(IM);
3607
3608 switch (mode) {
3609 case PHP_GD_FLIP_VERTICAL:
3610 gdImageFlipVertical(im);
3611 break;
3612
3613 case PHP_GD_FLIP_HORIZONTAL:
3614 gdImageFlipHorizontal(im);
3615 break;
3616
3617 case PHP_GD_FLIP_BOTH:
3618 gdImageFlipBoth(im);
3619 break;
3620
3621 default:
3622 zend_argument_value_error(2, "must be one of IMG_FLIP_VERTICAL, IMG_FLIP_HORIZONTAL, or IMG_FLIP_BOTH");
3623 RETURN_THROWS();
3624 }
3625
3626 RETURN_TRUE;
3627 }
3628 /* }}} */
3629
3630 /* {{{ Should antialiased functions used or not*/
PHP_FUNCTION(imageantialias)3631 PHP_FUNCTION(imageantialias)
3632 {
3633 zval *IM;
3634 bool alias;
3635 gdImagePtr im;
3636
3637 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &alias) == FAILURE) {
3638 RETURN_THROWS();
3639 }
3640
3641 im = php_gd_libgdimageptr_from_zval_p(IM);
3642 if (im->trueColor) {
3643 im->AA = alias;
3644 }
3645
3646 RETURN_TRUE;
3647 }
3648 /* }}} */
3649
3650 /* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
PHP_FUNCTION(imagecrop)3651 PHP_FUNCTION(imagecrop)
3652 {
3653 zval *IM;
3654 gdImagePtr im;
3655 gdImagePtr im_crop;
3656 gdRect rect;
3657 zval *z_rect;
3658 zval *tmp;
3659
3660 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &z_rect) == FAILURE) {
3661 RETURN_THROWS();
3662 }
3663
3664 im = php_gd_libgdimageptr_from_zval_p(IM);
3665
3666 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) {
3667 rect.x = zval_get_long(tmp);
3668 } else {
3669 zend_argument_value_error(2, "must have an \"x\" key");
3670 RETURN_THROWS();
3671 }
3672
3673 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
3674 rect.y = zval_get_long(tmp);
3675 } else {
3676 zend_argument_value_error(2, "must have a \"y\" key");
3677 RETURN_THROWS();
3678 }
3679
3680 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
3681 rect.width = zval_get_long(tmp);
3682 } else {
3683 zend_argument_value_error(2, "must have a \"width\" key");
3684 RETURN_THROWS();
3685 }
3686
3687 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
3688 rect.height = zval_get_long(tmp);
3689 } else {
3690 zend_argument_value_error(2, "must have a \"height\" key");
3691 RETURN_THROWS();
3692 }
3693
3694 im_crop = gdImageCrop(im, &rect);
3695
3696 if (im_crop == NULL) {
3697 RETURN_FALSE;
3698 }
3699
3700 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_crop);
3701 }
3702 /* }}} */
3703
3704 /* {{{ Crop an image automatically using one of the available modes. */
PHP_FUNCTION(imagecropauto)3705 PHP_FUNCTION(imagecropauto)
3706 {
3707 zval *IM;
3708 zend_long mode = GD_CROP_DEFAULT;
3709 zend_long color = -1;
3710 double threshold = 0.5f;
3711 gdImagePtr im;
3712 gdImagePtr im_crop;
3713
3714 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ldl", &IM, gd_image_ce, &mode, &threshold, &color) == FAILURE) {
3715 RETURN_THROWS();
3716 }
3717
3718 im = php_gd_libgdimageptr_from_zval_p(IM);
3719
3720 switch (mode) {
3721 case GD_CROP_DEFAULT:
3722 case GD_CROP_TRANSPARENT:
3723 case GD_CROP_BLACK:
3724 case GD_CROP_WHITE:
3725 case GD_CROP_SIDES:
3726 im_crop = gdImageCropAuto(im, mode);
3727 break;
3728
3729 case GD_CROP_THRESHOLD:
3730 if (color < 0 || (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im))) {
3731 zend_argument_value_error(4, "must be greater than or equal to 0 when using the threshold mode");
3732 RETURN_THROWS();
3733 }
3734 im_crop = gdImageCropThreshold(im, color, (float) threshold);
3735 break;
3736
3737 default:
3738 zend_argument_value_error(2, "must be a valid mode");
3739 RETURN_THROWS();
3740 }
3741
3742 if (im_crop == NULL) {
3743 RETURN_FALSE;
3744 }
3745
3746 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_crop);
3747 }
3748 /* }}} */
3749
3750 /* {{{ Scale an image using the given new width and height. */
PHP_FUNCTION(imagescale)3751 PHP_FUNCTION(imagescale)
3752 {
3753 zval *IM;
3754 gdImagePtr im;
3755 gdImagePtr im_scaled = NULL;
3756 int new_width, new_height;
3757 zend_long tmp_w, tmp_h=-1, tmp_m = GD_BILINEAR_FIXED;
3758 gdInterpolationMethod method, old_method;
3759
3760 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|ll", &IM, gd_image_ce, &tmp_w, &tmp_h, &tmp_m) == FAILURE) {
3761 RETURN_THROWS();
3762 }
3763 method = tmp_m;
3764
3765 im = php_gd_libgdimageptr_from_zval_p(IM);
3766
3767 if (tmp_h < 0 || tmp_w < 0) {
3768 /* preserve ratio */
3769 long src_x, src_y;
3770
3771 src_x = gdImageSX(im);
3772 src_y = gdImageSY(im);
3773
3774 if (src_x && tmp_h < 0) {
3775 tmp_h = tmp_w * src_y / src_x;
3776 }
3777 if (src_y && tmp_w < 0) {
3778 tmp_w = tmp_h * src_x / src_y;
3779 }
3780 }
3781
3782 if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) {
3783 RETURN_FALSE;
3784 }
3785
3786 new_width = tmp_w;
3787 new_height = tmp_h;
3788
3789 /* gdImageGetInterpolationMethod() is only available as of GD 2.1.1 */
3790 old_method = im->interpolation_id;
3791 if (gdImageSetInterpolationMethod(im, method)) {
3792 im_scaled = gdImageScale(im, new_width, new_height);
3793 }
3794 gdImageSetInterpolationMethod(im, old_method);
3795
3796 if (im_scaled == NULL) {
3797 RETURN_FALSE;
3798 }
3799
3800 php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_scaled);
3801 }
3802 /* }}} */
3803
3804 /* {{{ Return an image containing the affine tramsformed src image, using an optional clipping area */
PHP_FUNCTION(imageaffine)3805 PHP_FUNCTION(imageaffine)
3806 {
3807 zval *IM;
3808 gdImagePtr src;
3809 gdImagePtr dst;
3810 gdRect rect;
3811 gdRectPtr pRect = NULL;
3812 zval *z_rect = NULL;
3813 zval *z_affine;
3814 zval *tmp;
3815 double affine[6];
3816 int i, nelems;
3817 zval *zval_affine_elem = NULL;
3818
3819 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a!", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
3820 RETURN_THROWS();
3821 }
3822
3823 src = php_gd_libgdimageptr_from_zval_p(IM);
3824
3825 if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
3826 zend_argument_value_error(2, "must have 6 elements");
3827 RETURN_THROWS();
3828 }
3829
3830 for (i = 0; i < nelems; i++) {
3831 if ((zval_affine_elem = zend_hash_index_find(Z_ARRVAL_P(z_affine), i)) != NULL) {
3832 switch (Z_TYPE_P(zval_affine_elem)) {
3833 case IS_LONG:
3834 affine[i] = Z_LVAL_P(zval_affine_elem);
3835 break;
3836 case IS_DOUBLE:
3837 affine[i] = Z_DVAL_P(zval_affine_elem);
3838 break;
3839 case IS_STRING:
3840 affine[i] = zval_get_double(zval_affine_elem);
3841 break;
3842 default:
3843 zend_argument_type_error(3, "contains invalid type for element %i", i);
3844 RETURN_THROWS();
3845 }
3846 }
3847 }
3848
3849 if (z_rect != NULL) {
3850 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") - 1)) != NULL) {
3851 rect.x = zval_get_long(tmp);
3852 } else {
3853 zend_argument_value_error(3, "must have an \"x\" key");
3854 RETURN_THROWS();
3855 }
3856
3857 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
3858 rect.y = zval_get_long(tmp);
3859 } else {
3860 zend_argument_value_error(3, "must have a \"y\" key");
3861 RETURN_THROWS();
3862 }
3863
3864 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
3865 rect.width = zval_get_long(tmp);
3866 } else {
3867 zend_argument_value_error(3, "must have a \"width\" key");
3868 RETURN_THROWS();
3869 }
3870
3871 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
3872 rect.height = zval_get_long(tmp);
3873 } else {
3874 zend_argument_value_error(3, "must have a \"height\" key");
3875 RETURN_THROWS();
3876 }
3877 pRect = ▭
3878 }
3879
3880 if (gdTransformAffineGetImage(&dst, src, pRect, affine) != GD_TRUE) {
3881 RETURN_FALSE;
3882 }
3883
3884 if (dst == NULL) {
3885 RETURN_FALSE;
3886 }
3887
3888 php_gd_assign_libgdimageptr_as_extgdimage(return_value, dst);
3889 }
3890 /* }}} */
3891
3892 /* {{{ Return an image containing the affine tramsformed src image, using an optional clipping area */
PHP_FUNCTION(imageaffinematrixget)3893 PHP_FUNCTION(imageaffinematrixget)
3894 {
3895 double affine[6];
3896 zend_long type;
3897 zval *options = NULL;
3898 zval *tmp;
3899 int res = GD_FALSE, i;
3900
3901 if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &type, &options) == FAILURE) {
3902 RETURN_THROWS();
3903 }
3904
3905 switch((gdAffineStandardMatrix)type) {
3906 case GD_AFFINE_TRANSLATE:
3907 case GD_AFFINE_SCALE: {
3908 double x, y;
3909 if (Z_TYPE_P(options) != IS_ARRAY) {
3910 zend_argument_type_error(1, "must be of type array when using translate or scale");
3911 RETURN_THROWS();
3912 }
3913
3914 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) {
3915 x = zval_get_double(tmp);
3916 } else {
3917 zend_argument_value_error(2, "must have an \"x\" key");
3918 RETURN_THROWS();
3919 }
3920
3921 if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) {
3922 y = zval_get_double(tmp);
3923 } else {
3924 zend_argument_value_error(2, "must have a \"y\" key");
3925 RETURN_THROWS();
3926 }
3927
3928 if (type == GD_AFFINE_TRANSLATE) {
3929 res = gdAffineTranslate(affine, x, y);
3930 } else {
3931 res = gdAffineScale(affine, x, y);
3932 }
3933 break;
3934 }
3935
3936 case GD_AFFINE_ROTATE:
3937 case GD_AFFINE_SHEAR_HORIZONTAL:
3938 case GD_AFFINE_SHEAR_VERTICAL: {
3939 double angle;
3940
3941 angle = zval_get_double(options);
3942
3943 if (type == GD_AFFINE_SHEAR_HORIZONTAL) {
3944 res = gdAffineShearHorizontal(affine, angle);
3945 } else if (type == GD_AFFINE_SHEAR_VERTICAL) {
3946 res = gdAffineShearVertical(affine, angle);
3947 } else {
3948 res = gdAffineRotate(affine, angle);
3949 }
3950 break;
3951 }
3952
3953 default:
3954 zend_argument_value_error(1, "must be a valid element type");
3955 RETURN_THROWS();
3956 }
3957
3958 if (res == GD_FALSE) {
3959 RETURN_FALSE;
3960 } else {
3961 array_init(return_value);
3962 for (i = 0; i < 6; i++) {
3963 add_index_double(return_value, i, affine[i]);
3964 }
3965 }
3966 } /* }}} */
3967
3968 /* {{{ Concat two matrices (as in doing many ops in one go) */
PHP_FUNCTION(imageaffinematrixconcat)3969 PHP_FUNCTION(imageaffinematrixconcat)
3970 {
3971 double m1[6];
3972 double m2[6];
3973 double mr[6];
3974
3975 zval *tmp;
3976 zval *z_m1;
3977 zval *z_m2;
3978 int i;
3979
3980 if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &z_m1, &z_m2) == FAILURE) {
3981 RETURN_THROWS();
3982 }
3983
3984 if (zend_hash_num_elements(Z_ARRVAL_P(z_m1)) != 6) {
3985 zend_argument_value_error(1, "must have 6 elements");
3986 RETURN_THROWS();
3987 }
3988
3989 if (zend_hash_num_elements(Z_ARRVAL_P(z_m2)) != 6) {
3990 zend_argument_value_error(1, "must have 6 elements");
3991 RETURN_THROWS();
3992 }
3993
3994 for (i = 0; i < 6; i++) {
3995 if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m1), i)) != NULL) {
3996 switch (Z_TYPE_P(tmp)) {
3997 case IS_LONG:
3998 m1[i] = Z_LVAL_P(tmp);
3999 break;
4000 case IS_DOUBLE:
4001 m1[i] = Z_DVAL_P(tmp);
4002 break;
4003 case IS_STRING:
4004 m1[i] = zval_get_double(tmp);
4005 break;
4006 default:
4007 zend_argument_type_error(1, "contains invalid type for element %i", i);
4008 RETURN_THROWS();
4009 }
4010 }
4011
4012 if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m2), i)) != NULL) {
4013 switch (Z_TYPE_P(tmp)) {
4014 case IS_LONG:
4015 m2[i] = Z_LVAL_P(tmp);
4016 break;
4017 case IS_DOUBLE:
4018 m2[i] = Z_DVAL_P(tmp);
4019 break;
4020 case IS_STRING:
4021 m2[i] = zval_get_double(tmp);
4022 break;
4023 default:
4024 zend_argument_type_error(2, "contains invalid type for element %i", i);
4025 RETURN_THROWS();
4026 }
4027 }
4028 }
4029
4030 if (gdAffineConcat (mr, m1, m2) != GD_TRUE) {
4031 RETURN_FALSE;
4032 }
4033
4034 array_init(return_value);
4035 for (i = 0; i < 6; i++) {
4036 add_index_double(return_value, i, mr[i]);
4037 }
4038 } /* }}} */
4039
4040 /* {{{ Get the default interpolation method. */
PHP_FUNCTION(imagegetinterpolation)4041 PHP_FUNCTION(imagegetinterpolation)
4042 {
4043 zval *IM;
4044 gdImagePtr im;
4045
4046 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
4047 RETURN_THROWS();
4048 }
4049 im = php_gd_libgdimageptr_from_zval_p(IM);
4050
4051 #ifdef HAVE_GD_GET_INTERPOLATION
4052 RETURN_LONG(gdImageGetInterpolationMethod(im));
4053 #else
4054 RETURN_LONG(im->interpolation_id);
4055 #endif
4056 }
4057 /* }}} */
4058
4059 /* {{{ Set the default interpolation method, passing -1 or 0 sets it to the libgd default (bilinear). */
PHP_FUNCTION(imagesetinterpolation)4060 PHP_FUNCTION(imagesetinterpolation)
4061 {
4062 zval *IM;
4063 gdImagePtr im;
4064 zend_long method = GD_BILINEAR_FIXED;
4065
4066 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &IM, gd_image_ce, &method) == FAILURE) {
4067 RETURN_THROWS();
4068 }
4069
4070 im = php_gd_libgdimageptr_from_zval_p(IM);
4071
4072 if (method == -1) {
4073 method = GD_BILINEAR_FIXED;
4074 }
4075 RETURN_BOOL(gdImageSetInterpolationMethod(im, (gdInterpolationMethod) method));
4076 }
4077 /* }}} */
4078
4079 /* {{{ Get or set the resolution of the image in DPI. */
PHP_FUNCTION(imageresolution)4080 PHP_FUNCTION(imageresolution)
4081 {
4082 zval *IM;
4083 gdImagePtr im;
4084 zend_long res_x, res_y;
4085 bool res_x_is_null = 1, res_y_is_null = 1;
4086
4087 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) {
4088 RETURN_THROWS();
4089 }
4090
4091 im = php_gd_libgdimageptr_from_zval_p(IM);
4092
4093 if (!res_x_is_null && !res_y_is_null) {
4094 gdImageSetResolution(im, res_x, res_y);
4095 RETURN_TRUE;
4096 } else if (!res_x_is_null && res_y_is_null) {
4097 gdImageSetResolution(im, res_x, res_x);
4098 RETURN_TRUE;
4099 } else if (res_x_is_null && !res_y_is_null) {
4100 gdImageSetResolution(im, res_y, res_y);
4101 RETURN_TRUE;
4102 }
4103
4104 array_init(return_value);
4105 add_next_index_long(return_value, gdImageResolutionX(im));
4106 add_next_index_long(return_value, gdImageResolutionY(im));
4107 }
4108 /* }}} */
4109
4110
4111 /*********************************************************
4112 *
4113 * Stream Handling
4114 * Formerly contained within ext/gd/gd_ctx.c and included
4115 * at the the top of this file
4116 *
4117 ********************************************************/
4118
4119 #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
4120
_php_image_output_putc(struct gdIOCtx * ctx,int c)4121 static void _php_image_output_putc(struct gdIOCtx *ctx, int c) /* {{{ */
4122 {
4123 /* without the following downcast, the write will fail
4124 * (i.e., will write a zero byte) for all
4125 * big endian architectures:
4126 */
4127 unsigned char ch = (unsigned char) c;
4128 php_write(&ch, 1);
4129 } /* }}} */
4130
_php_image_output_putbuf(struct gdIOCtx * ctx,const void * buf,int l)4131 static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
4132 {
4133 return php_write((void *)buf, l);
4134 } /* }}} */
4135
_php_image_output_ctxfree(struct gdIOCtx * ctx)4136 static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
4137 {
4138 if(ctx) {
4139 efree(ctx);
4140 }
4141 } /* }}} */
4142
_php_image_stream_putc(struct gdIOCtx * ctx,int c)4143 static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
4144 char ch = (char) c;
4145 php_stream * stream = (php_stream *)ctx->data;
4146 php_stream_write(stream, &ch, 1);
4147 } /* }}} */
4148
_php_image_stream_putbuf(struct gdIOCtx * ctx,const void * buf,int l)4149 static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
4150 {
4151 php_stream * stream = (php_stream *)ctx->data;
4152 return php_stream_write(stream, (void *)buf, l);
4153 } /* }}} */
4154
_php_image_stream_ctxfree(struct gdIOCtx * ctx)4155 static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
4156 {
4157 if(ctx->data) {
4158 ctx->data = NULL;
4159 }
4160 if(ctx) {
4161 efree(ctx);
4162 }
4163 } /* }}} */
4164
_php_image_stream_ctxfreeandclose(struct gdIOCtx * ctx)4165 static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
4166 {
4167
4168 if(ctx->data) {
4169 php_stream_close((php_stream *) ctx->data);
4170 ctx->data = NULL;
4171 }
4172 if(ctx) {
4173 efree(ctx);
4174 }
4175 } /* }}} */
4176
create_stream_context_from_zval(zval * to_zval)4177 static gdIOCtx *create_stream_context_from_zval(zval *to_zval) {
4178 php_stream *stream;
4179 int close_stream = 1;
4180
4181 if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
4182 php_stream_from_zval_no_verify(stream, to_zval);
4183 if (stream == NULL) {
4184 return NULL;
4185 }
4186 close_stream = 0;
4187 } else if (Z_TYPE_P(to_zval) == IS_STRING) {
4188 if (CHECK_ZVAL_NULL_PATH(to_zval)) {
4189 zend_argument_type_error(2, "must not contain null bytes");
4190 return NULL;
4191 }
4192
4193 stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
4194 if (stream == NULL) {
4195 return NULL;
4196 }
4197 } else {
4198 zend_argument_type_error(2, "must be a file name or a stream resource, %s given", zend_zval_type_name(to_zval));
4199 return NULL;
4200 }
4201
4202 return create_stream_context(stream, close_stream);
4203 }
4204
create_stream_context(php_stream * stream,int close_stream)4205 static gdIOCtx *create_stream_context(php_stream *stream, int close_stream) {
4206 gdIOCtx *ctx = ecalloc(1, sizeof(gdIOCtx));
4207
4208 ctx->putC = _php_image_stream_putc;
4209 ctx->putBuf = _php_image_stream_putbuf;
4210 if (close_stream) {
4211 ctx->gd_free = _php_image_stream_ctxfreeandclose;
4212 } else {
4213 ctx->gd_free = _php_image_stream_ctxfree;
4214 }
4215 ctx->data = (void *)stream;
4216
4217 return ctx;
4218 }
4219
create_output_context()4220 static gdIOCtx *create_output_context() {
4221 gdIOCtx *ctx = ecalloc(1, sizeof(gdIOCtx));
4222
4223 ctx->putC = _php_image_output_putc;
4224 ctx->putBuf = _php_image_output_putbuf;
4225 ctx->gd_free = _php_image_output_ctxfree;
4226
4227 return ctx;
4228 }
4229
_php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn)4230 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn)
4231 {
4232 zval *imgind;
4233 zend_long quality = -1, basefilter = -1, speed = -1;
4234 gdImagePtr im;
4235 gdIOCtx *ctx = NULL;
4236 zval *to_zval = NULL;
4237
4238 if (image_type == PHP_GDIMG_TYPE_GIF) {
4239 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!", &imgind, gd_image_ce, &to_zval) == FAILURE) {
4240 RETURN_THROWS();
4241 }
4242 } else if (image_type == PHP_GDIMG_TYPE_PNG) {
4243 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
4244 RETURN_THROWS();
4245 }
4246 } else if (image_type == PHP_GDIMG_TYPE_AVIF) {
4247 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &speed) == FAILURE) {
4248 RETURN_THROWS();
4249 }
4250 } else {
4251 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
4252 RETURN_THROWS();
4253 }
4254 }
4255
4256 im = php_gd_libgdimageptr_from_zval_p(imgind);
4257
4258 if (to_zval != NULL) {
4259 ctx = create_stream_context_from_zval(to_zval);
4260 if (!ctx) {
4261 RETURN_FALSE;
4262 }
4263 } else {
4264 ctx = create_output_context();
4265 }
4266
4267 switch (image_type) {
4268 #ifdef HAVE_GD_JPG
4269 case PHP_GDIMG_TYPE_JPG:
4270 gdImageJpegCtx(im, ctx, (int) quality);
4271 break;
4272 #endif
4273 #ifdef HAVE_GD_WEBP
4274 case PHP_GDIMG_TYPE_WEBP:
4275 if (quality == -1) {
4276 quality = 80;
4277 }
4278 gdImageWebpCtx(im, ctx, (int) quality);
4279 break;
4280 #endif
4281 #ifdef HAVE_GD_AVIF
4282 case PHP_GDIMG_TYPE_AVIF:
4283 if (speed == -1) {
4284 speed = 6;
4285 }
4286 gdImageAvifCtx(im, ctx, (int) quality, (int) speed);
4287 break;
4288 #endif
4289 #ifdef HAVE_GD_PNG
4290 case PHP_GDIMG_TYPE_PNG:
4291 #ifdef HAVE_GD_BUNDLED
4292 gdImagePngCtxEx(im, ctx, (int) quality, (int) basefilter);
4293 #else
4294 gdImagePngCtxEx(im, ctx, (int) quality);
4295 #endif
4296 break;
4297 #endif
4298 case PHP_GDIMG_TYPE_GIF:
4299 gdImageGifCtx(im, ctx);
4300 break;
4301 EMPTY_SWITCH_DEFAULT_CASE()
4302 }
4303
4304 ctx->gd_free(ctx);
4305
4306 RETURN_TRUE;
4307 }
4308
4309 /* }}} */
4310