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