xref: /PHP-7.2/ext/gd/gd.c (revision 18a9ae41)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
16    |          Stig Bakken <ssb@php.net>                                   |
17    |          Jim Winstead <jimw@php.net>                                 |
18    +----------------------------------------------------------------------+
19  */
20 
21 /* $Id$ */
22 
23 /* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center,
24    Cold Spring Harbor Labs. */
25 
26 /* Note that there is no code from the gd package in this file */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "php.h"
33 #include "php_ini.h"
34 #include "ext/standard/head.h"
35 #include <math.h>
36 #include "SAPI.h"
37 #include "php_gd.h"
38 #include "ext/standard/info.h"
39 #include "php_open_temporary_file.h"
40 
41 
42 #if HAVE_SYS_WAIT_H
43 # include <sys/wait.h>
44 #endif
45 #if HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #ifdef PHP_WIN32
49 # include <io.h>
50 # include <fcntl.h>
51 # include <windows.h>
52 # include <Winuser.h>
53 # include <Wingdi.h>
54 #endif
55 
56 #ifdef HAVE_GD_XPM
57 # include <X11/xpm.h>
58 #endif
59 
60 # include "gd_compat.h"
61 
62 
63 static int le_gd, le_gd_font;
64 
65 #include <gd.h>
66 #include <gd_errors.h>
67 #include <gdfontt.h>  /* 1 Tiny font */
68 #include <gdfonts.h>  /* 2 Small font */
69 #include <gdfontmb.h> /* 3 Medium bold font */
70 #include <gdfontl.h>  /* 4 Large font */
71 #include <gdfontg.h>  /* 5 Giant font */
72 
73 #ifdef ENABLE_GD_TTF
74 # ifdef HAVE_LIBFREETYPE
75 #  include <ft2build.h>
76 #  include FT_FREETYPE_H
77 # endif
78 #endif
79 
80 #if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
81 # include "X11/xpm.h"
82 #endif
83 
84 #ifndef M_PI
85 #define M_PI 3.14159265358979323846
86 #endif
87 
88 #ifdef ENABLE_GD_TTF
89 static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int, int);
90 #endif
91 
92 #include "gd_ctx.c"
93 
94 /* as it is not really public, duplicate declaration here to avoid
95    pointless warnings */
96 int overflow2(int a, int b);
97 
98 /* Section Filters Declarations */
99 /* IMPORTANT NOTE FOR NEW FILTER
100  * Do not forget to update:
101  * IMAGE_FILTER_MAX: define the last filter index
102  * IMAGE_FILTER_MAX_ARGS: define the biggest amount of arguments
103  * image_filter array in PHP_FUNCTION(imagefilter)
104  * */
105 #define IMAGE_FILTER_NEGATE         0
106 #define IMAGE_FILTER_GRAYSCALE      1
107 #define IMAGE_FILTER_BRIGHTNESS     2
108 #define IMAGE_FILTER_CONTRAST       3
109 #define IMAGE_FILTER_COLORIZE       4
110 #define IMAGE_FILTER_EDGEDETECT     5
111 #define IMAGE_FILTER_EMBOSS         6
112 #define IMAGE_FILTER_GAUSSIAN_BLUR  7
113 #define IMAGE_FILTER_SELECTIVE_BLUR 8
114 #define IMAGE_FILTER_MEAN_REMOVAL   9
115 #define IMAGE_FILTER_SMOOTH         10
116 #define IMAGE_FILTER_PIXELATE       11
117 #define IMAGE_FILTER_MAX            11
118 #define IMAGE_FILTER_MAX_ARGS       6
119 static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
120 static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
121 static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
122 static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
123 static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
124 static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
125 static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
126 static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
127 static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
128 static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
129 static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
130 static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS);
131 
132 /* End Section filters declarations */
133 static gdImagePtr _php_image_create_from_string (zval *Data, char *tn, gdImagePtr (*ioctx_func_p)());
134 static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)());
135 static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)());
136 static int _php_image_type(char data[8]);
137 static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type);
138 static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold);
139 
140 /* {{{ arginfo */
141 ZEND_BEGIN_ARG_INFO(arginfo_gd_info, 0)
142 ZEND_END_ARG_INFO()
143 
144 ZEND_BEGIN_ARG_INFO(arginfo_imageloadfont, 0)
145 	ZEND_ARG_INFO(0, filename)
146 ZEND_END_ARG_INFO()
147 
148 ZEND_BEGIN_ARG_INFO(arginfo_imagesetstyle, 0)
149 	ZEND_ARG_INFO(0, im)
150 	ZEND_ARG_INFO(0, styles) /* ARRAY_INFO(0, styles, 0) */
151 ZEND_END_ARG_INFO()
152 
153 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatetruecolor, 0)
154 	ZEND_ARG_INFO(0, x_size)
155 	ZEND_ARG_INFO(0, y_size)
156 ZEND_END_ARG_INFO()
157 
158 ZEND_BEGIN_ARG_INFO(arginfo_imageistruecolor, 0)
159 	ZEND_ARG_INFO(0, im)
160 ZEND_END_ARG_INFO()
161 
162 ZEND_BEGIN_ARG_INFO(arginfo_imagetruecolortopalette, 0)
163 	ZEND_ARG_INFO(0, im)
164 	ZEND_ARG_INFO(0, ditherFlag)
165 	ZEND_ARG_INFO(0, colorsWanted)
166 ZEND_END_ARG_INFO()
167 
168 ZEND_BEGIN_ARG_INFO(arginfo_imagepalettetotruecolor, 0)
169 	ZEND_ARG_INFO(0, im)
170 ZEND_END_ARG_INFO()
171 
172 ZEND_BEGIN_ARG_INFO(arginfo_imagecolormatch, 0)
173 	ZEND_ARG_INFO(0, im1)
174 	ZEND_ARG_INFO(0, im2)
175 ZEND_END_ARG_INFO()
176 
177 ZEND_BEGIN_ARG_INFO(arginfo_imagesetthickness, 0)
178 	ZEND_ARG_INFO(0, im)
179 	ZEND_ARG_INFO(0, thickness)
180 ZEND_END_ARG_INFO()
181 
182 ZEND_BEGIN_ARG_INFO(arginfo_imagefilledellipse, 0)
183 	ZEND_ARG_INFO(0, im)
184 	ZEND_ARG_INFO(0, cx)
185 	ZEND_ARG_INFO(0, cy)
186 	ZEND_ARG_INFO(0, w)
187 	ZEND_ARG_INFO(0, h)
188 	ZEND_ARG_INFO(0, color)
189 ZEND_END_ARG_INFO()
190 
191 ZEND_BEGIN_ARG_INFO(arginfo_imagefilledarc, 0)
192 	ZEND_ARG_INFO(0, im)
193 	ZEND_ARG_INFO(0, cx)
194 	ZEND_ARG_INFO(0, cy)
195 	ZEND_ARG_INFO(0, w)
196 	ZEND_ARG_INFO(0, h)
197 	ZEND_ARG_INFO(0, s)
198 	ZEND_ARG_INFO(0, e)
199 	ZEND_ARG_INFO(0, col)
200 	ZEND_ARG_INFO(0, style)
201 ZEND_END_ARG_INFO()
202 
203 ZEND_BEGIN_ARG_INFO(arginfo_imagealphablending, 0)
204 	ZEND_ARG_INFO(0, im)
205 	ZEND_ARG_INFO(0, blend)
206 ZEND_END_ARG_INFO()
207 
208 ZEND_BEGIN_ARG_INFO(arginfo_imagesavealpha, 0)
209 	ZEND_ARG_INFO(0, im)
210 	ZEND_ARG_INFO(0, save)
211 ZEND_END_ARG_INFO()
212 
213 ZEND_BEGIN_ARG_INFO(arginfo_imagelayereffect, 0)
214 	ZEND_ARG_INFO(0, im)
215 	ZEND_ARG_INFO(0, effect)
216 ZEND_END_ARG_INFO()
217 
218 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorallocatealpha, 0)
219 	ZEND_ARG_INFO(0, im)
220 	ZEND_ARG_INFO(0, red)
221 	ZEND_ARG_INFO(0, green)
222 	ZEND_ARG_INFO(0, blue)
223 	ZEND_ARG_INFO(0, alpha)
224 ZEND_END_ARG_INFO()
225 
226 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorresolvealpha, 0)
227 	ZEND_ARG_INFO(0, im)
228 	ZEND_ARG_INFO(0, red)
229 	ZEND_ARG_INFO(0, green)
230 	ZEND_ARG_INFO(0, blue)
231 	ZEND_ARG_INFO(0, alpha)
232 ZEND_END_ARG_INFO()
233 
234 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorclosestalpha, 0)
235 	ZEND_ARG_INFO(0, im)
236 	ZEND_ARG_INFO(0, red)
237 	ZEND_ARG_INFO(0, green)
238 	ZEND_ARG_INFO(0, blue)
239 	ZEND_ARG_INFO(0, alpha)
240 ZEND_END_ARG_INFO()
241 
242 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorexactalpha, 0)
243 	ZEND_ARG_INFO(0, im)
244 	ZEND_ARG_INFO(0, red)
245 	ZEND_ARG_INFO(0, green)
246 	ZEND_ARG_INFO(0, blue)
247 	ZEND_ARG_INFO(0, alpha)
248 ZEND_END_ARG_INFO()
249 
250 ZEND_BEGIN_ARG_INFO(arginfo_imagecopyresampled, 0)
251 	ZEND_ARG_INFO(0, dst_im)
252 	ZEND_ARG_INFO(0, src_im)
253 	ZEND_ARG_INFO(0, dst_x)
254 	ZEND_ARG_INFO(0, dst_y)
255 	ZEND_ARG_INFO(0, src_x)
256 	ZEND_ARG_INFO(0, src_y)
257 	ZEND_ARG_INFO(0, dst_w)
258 	ZEND_ARG_INFO(0, dst_h)
259 	ZEND_ARG_INFO(0, src_w)
260 	ZEND_ARG_INFO(0, src_h)
261 ZEND_END_ARG_INFO()
262 
263 #ifdef PHP_WIN32
264 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegrabwindow, 0, 0, 1)
265 	ZEND_ARG_INFO(0, handle)
266 	ZEND_ARG_INFO(0, client_area)
267 ZEND_END_ARG_INFO()
268 
269 ZEND_BEGIN_ARG_INFO(arginfo_imagegrabscreen, 0)
270 ZEND_END_ARG_INFO()
271 #endif
272 
273 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagerotate, 0, 0, 3)
274 	ZEND_ARG_INFO(0, im)
275 	ZEND_ARG_INFO(0, angle)
276 	ZEND_ARG_INFO(0, bgdcolor)
277 	ZEND_ARG_INFO(0, ignoretransparent)
278 ZEND_END_ARG_INFO()
279 
280 ZEND_BEGIN_ARG_INFO(arginfo_imagesettile, 0)
281 	ZEND_ARG_INFO(0, im)
282 	ZEND_ARG_INFO(0, tile)
283 ZEND_END_ARG_INFO()
284 
285 ZEND_BEGIN_ARG_INFO(arginfo_imagesetbrush, 0)
286 	ZEND_ARG_INFO(0, im)
287 	ZEND_ARG_INFO(0, brush)
288 ZEND_END_ARG_INFO()
289 
290 ZEND_BEGIN_ARG_INFO(arginfo_imagecreate, 0)
291 	ZEND_ARG_INFO(0, x_size)
292 	ZEND_ARG_INFO(0, y_size)
293 ZEND_END_ARG_INFO()
294 
295 ZEND_BEGIN_ARG_INFO(arginfo_imagetypes, 0)
296 ZEND_END_ARG_INFO()
297 
298 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromstring, 0)
299 	ZEND_ARG_INFO(0, image)
300 ZEND_END_ARG_INFO()
301 
302 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgif, 0)
303 	ZEND_ARG_INFO(0, filename)
304 ZEND_END_ARG_INFO()
305 
306 #ifdef HAVE_GD_JPG
307 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromjpeg, 0)
308 	ZEND_ARG_INFO(0, filename)
309 ZEND_END_ARG_INFO()
310 #endif
311 
312 #ifdef HAVE_GD_PNG
313 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefrompng, 0)
314 	ZEND_ARG_INFO(0, filename)
315 ZEND_END_ARG_INFO()
316 #endif
317 
318 #ifdef HAVE_GD_WEBP
319 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromwebp, 0)
320 	ZEND_ARG_INFO(0, filename)
321 ZEND_END_ARG_INFO()
322 #endif
323 
324 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromxbm, 0)
325 	ZEND_ARG_INFO(0, filename)
326 ZEND_END_ARG_INFO()
327 
328 #if defined(HAVE_GD_XPM)
329 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromxpm, 0)
330 	ZEND_ARG_INFO(0, filename)
331 ZEND_END_ARG_INFO()
332 #endif
333 
334 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromwbmp, 0)
335 	ZEND_ARG_INFO(0, filename)
336 ZEND_END_ARG_INFO()
337 
338 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd, 0)
339 	ZEND_ARG_INFO(0, filename)
340 ZEND_END_ARG_INFO()
341 
342 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd2, 0)
343 	ZEND_ARG_INFO(0, filename)
344 ZEND_END_ARG_INFO()
345 
346 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd2part, 0)
347 	ZEND_ARG_INFO(0, filename)
348 	ZEND_ARG_INFO(0, srcX)
349 	ZEND_ARG_INFO(0, srcY)
350 	ZEND_ARG_INFO(0, width)
351 	ZEND_ARG_INFO(0, height)
352 ZEND_END_ARG_INFO()
353 
354 #if defined(HAVE_GD_BMP)
355 ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefrombmp, 0)
356 	ZEND_ARG_INFO(0, filename)
357 ZEND_END_ARG_INFO()
358 #endif
359 
360 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagexbm, 0, 0, 2)
361 	ZEND_ARG_INFO(0, im)
362 	ZEND_ARG_INFO(0, filename)
363 	ZEND_ARG_INFO(0, foreground)
364 ZEND_END_ARG_INFO()
365 
366 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegif, 0, 0, 1)
367 	ZEND_ARG_INFO(0, im)
368 	ZEND_ARG_INFO(0, to)
369 ZEND_END_ARG_INFO()
370 
371 #ifdef HAVE_GD_PNG
372 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagepng, 0, 0, 1)
373 	ZEND_ARG_INFO(0, im)
374 	ZEND_ARG_INFO(0, to)
375 	ZEND_ARG_INFO(0, quality)
376 	ZEND_ARG_INFO(0, filters)
377 ZEND_END_ARG_INFO()
378 #endif
379 
380 #ifdef HAVE_GD_WEBP
381 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagewebp, 0, 0, 1)
382 	ZEND_ARG_INFO(0, im)
383 	ZEND_ARG_INFO(0, to)
384 	ZEND_ARG_INFO(0, quality)
385 ZEND_END_ARG_INFO()
386 #endif
387 
388 #ifdef HAVE_GD_JPG
389 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagejpeg, 0, 0, 1)
390 	ZEND_ARG_INFO(0, im)
391 	ZEND_ARG_INFO(0, to)
392 	ZEND_ARG_INFO(0, quality)
393 ZEND_END_ARG_INFO()
394 #endif
395 
396 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagewbmp, 0, 0, 1)
397 	ZEND_ARG_INFO(0, im)
398 	ZEND_ARG_INFO(0, to)
399 	ZEND_ARG_INFO(0, foreground)
400 ZEND_END_ARG_INFO()
401 
402 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegd, 0, 0, 1)
403 	ZEND_ARG_INFO(0, im)
404 	ZEND_ARG_INFO(0, to)
405 ZEND_END_ARG_INFO()
406 
407 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegd2, 0, 0, 1)
408 	ZEND_ARG_INFO(0, im)
409 	ZEND_ARG_INFO(0, to)
410 	ZEND_ARG_INFO(0, chunk_size)
411 	ZEND_ARG_INFO(0, type)
412 ZEND_END_ARG_INFO()
413 
414 #if defined(HAVE_GD_BMP)
415 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagebmp, 0, 0, 1)
416 	ZEND_ARG_INFO(0, im)
417 	ZEND_ARG_INFO(0, to)
418 	ZEND_ARG_INFO(0, compressed)
419 ZEND_END_ARG_INFO()
420 #endif
421 
422 ZEND_BEGIN_ARG_INFO(arginfo_imagedestroy, 0)
423 	ZEND_ARG_INFO(0, im)
424 ZEND_END_ARG_INFO()
425 
426 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorallocate, 0)
427 	ZEND_ARG_INFO(0, im)
428 	ZEND_ARG_INFO(0, red)
429 	ZEND_ARG_INFO(0, green)
430 	ZEND_ARG_INFO(0, blue)
431 ZEND_END_ARG_INFO()
432 
433 ZEND_BEGIN_ARG_INFO(arginfo_imagepalettecopy, 0)
434 	ZEND_ARG_INFO(0, dst)
435 	ZEND_ARG_INFO(0, src)
436 ZEND_END_ARG_INFO()
437 
438 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorat, 0)
439 	ZEND_ARG_INFO(0, im)
440 	ZEND_ARG_INFO(0, x)
441 	ZEND_ARG_INFO(0, y)
442 ZEND_END_ARG_INFO()
443 
444 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorclosest, 0)
445 	ZEND_ARG_INFO(0, im)
446 	ZEND_ARG_INFO(0, red)
447 	ZEND_ARG_INFO(0, green)
448 	ZEND_ARG_INFO(0, blue)
449 ZEND_END_ARG_INFO()
450 
451 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorclosesthwb, 0)
452 	ZEND_ARG_INFO(0, im)
453 	ZEND_ARG_INFO(0, red)
454 	ZEND_ARG_INFO(0, green)
455 	ZEND_ARG_INFO(0, blue)
456 ZEND_END_ARG_INFO()
457 
458 ZEND_BEGIN_ARG_INFO(arginfo_imagecolordeallocate, 0)
459 	ZEND_ARG_INFO(0, im)
460 	ZEND_ARG_INFO(0, index)
461 ZEND_END_ARG_INFO()
462 
463 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorresolve, 0)
464 	ZEND_ARG_INFO(0, im)
465 	ZEND_ARG_INFO(0, red)
466 	ZEND_ARG_INFO(0, green)
467 	ZEND_ARG_INFO(0, blue)
468 ZEND_END_ARG_INFO()
469 
470 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorexact, 0)
471 	ZEND_ARG_INFO(0, im)
472 	ZEND_ARG_INFO(0, red)
473 	ZEND_ARG_INFO(0, green)
474 	ZEND_ARG_INFO(0, blue)
475 ZEND_END_ARG_INFO()
476 
477 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagecolorset, 0, 0, 5)
478 	ZEND_ARG_INFO(0, im)
479 	ZEND_ARG_INFO(0, color)
480 	ZEND_ARG_INFO(0, red)
481 	ZEND_ARG_INFO(0, green)
482 	ZEND_ARG_INFO(0, blue)
483 	ZEND_ARG_INFO(0, alpha)
484 ZEND_END_ARG_INFO()
485 
486 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorsforindex, 0)
487 	ZEND_ARG_INFO(0, im)
488 	ZEND_ARG_INFO(0, index)
489 ZEND_END_ARG_INFO()
490 
491 ZEND_BEGIN_ARG_INFO(arginfo_imagegammacorrect, 0)
492 	ZEND_ARG_INFO(0, im)
493 	ZEND_ARG_INFO(0, inputgamma)
494 	ZEND_ARG_INFO(0, outputgamma)
495 ZEND_END_ARG_INFO()
496 
497 ZEND_BEGIN_ARG_INFO(arginfo_imagesetpixel, 0)
498 	ZEND_ARG_INFO(0, im)
499 	ZEND_ARG_INFO(0, x)
500 	ZEND_ARG_INFO(0, y)
501 	ZEND_ARG_INFO(0, col)
502 ZEND_END_ARG_INFO()
503 
504 ZEND_BEGIN_ARG_INFO(arginfo_imageline, 0)
505 	ZEND_ARG_INFO(0, im)
506 	ZEND_ARG_INFO(0, x1)
507 	ZEND_ARG_INFO(0, y1)
508 	ZEND_ARG_INFO(0, x2)
509 	ZEND_ARG_INFO(0, y2)
510 	ZEND_ARG_INFO(0, col)
511 ZEND_END_ARG_INFO()
512 
513 ZEND_BEGIN_ARG_INFO(arginfo_imagedashedline, 0)
514 	ZEND_ARG_INFO(0, im)
515 	ZEND_ARG_INFO(0, x1)
516 	ZEND_ARG_INFO(0, y1)
517 	ZEND_ARG_INFO(0, x2)
518 	ZEND_ARG_INFO(0, y2)
519 	ZEND_ARG_INFO(0, col)
520 ZEND_END_ARG_INFO()
521 
522 ZEND_BEGIN_ARG_INFO(arginfo_imagerectangle, 0)
523 	ZEND_ARG_INFO(0, im)
524 	ZEND_ARG_INFO(0, x1)
525 	ZEND_ARG_INFO(0, y1)
526 	ZEND_ARG_INFO(0, x2)
527 	ZEND_ARG_INFO(0, y2)
528 	ZEND_ARG_INFO(0, col)
529 ZEND_END_ARG_INFO()
530 
531 ZEND_BEGIN_ARG_INFO(arginfo_imagefilledrectangle, 0)
532 	ZEND_ARG_INFO(0, im)
533 	ZEND_ARG_INFO(0, x1)
534 	ZEND_ARG_INFO(0, y1)
535 	ZEND_ARG_INFO(0, x2)
536 	ZEND_ARG_INFO(0, y2)
537 	ZEND_ARG_INFO(0, col)
538 ZEND_END_ARG_INFO()
539 
540 ZEND_BEGIN_ARG_INFO(arginfo_imagearc, 0)
541 	ZEND_ARG_INFO(0, im)
542 	ZEND_ARG_INFO(0, cx)
543 	ZEND_ARG_INFO(0, cy)
544 	ZEND_ARG_INFO(0, w)
545 	ZEND_ARG_INFO(0, h)
546 	ZEND_ARG_INFO(0, s)
547 	ZEND_ARG_INFO(0, e)
548 	ZEND_ARG_INFO(0, col)
549 ZEND_END_ARG_INFO()
550 
551 ZEND_BEGIN_ARG_INFO(arginfo_imageellipse, 0)
552 	ZEND_ARG_INFO(0, im)
553 	ZEND_ARG_INFO(0, cx)
554 	ZEND_ARG_INFO(0, cy)
555 	ZEND_ARG_INFO(0, w)
556 	ZEND_ARG_INFO(0, h)
557 	ZEND_ARG_INFO(0, color)
558 ZEND_END_ARG_INFO()
559 
560 ZEND_BEGIN_ARG_INFO(arginfo_imagefilltoborder, 0)
561 	ZEND_ARG_INFO(0, im)
562 	ZEND_ARG_INFO(0, x)
563 	ZEND_ARG_INFO(0, y)
564 	ZEND_ARG_INFO(0, border)
565 	ZEND_ARG_INFO(0, col)
566 ZEND_END_ARG_INFO()
567 
568 ZEND_BEGIN_ARG_INFO(arginfo_imagefill, 0)
569 	ZEND_ARG_INFO(0, im)
570 	ZEND_ARG_INFO(0, x)
571 	ZEND_ARG_INFO(0, y)
572 	ZEND_ARG_INFO(0, col)
573 ZEND_END_ARG_INFO()
574 
575 ZEND_BEGIN_ARG_INFO(arginfo_imagecolorstotal, 0)
576 	ZEND_ARG_INFO(0, im)
577 ZEND_END_ARG_INFO()
578 
579 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagecolortransparent, 0, 0, 1)
580 	ZEND_ARG_INFO(0, im)
581 	ZEND_ARG_INFO(0, col)
582 ZEND_END_ARG_INFO()
583 
584 ZEND_BEGIN_ARG_INFO_EX(arginfo_imageinterlace, 0, 0, 1)
585 	ZEND_ARG_INFO(0, im)
586 	ZEND_ARG_INFO(0, interlace)
587 ZEND_END_ARG_INFO()
588 
589 ZEND_BEGIN_ARG_INFO(arginfo_imagepolygon, 0)
590 	ZEND_ARG_INFO(0, im)
591 	ZEND_ARG_INFO(0, points) /* ARRAY_INFO(0, points, 0) */
592 	ZEND_ARG_INFO(0, num_pos)
593 	ZEND_ARG_INFO(0, col)
594 ZEND_END_ARG_INFO()
595 
596 ZEND_BEGIN_ARG_INFO(arginfo_imageopenpolygon, 0)
597 	ZEND_ARG_INFO(0, im)
598 	ZEND_ARG_INFO(0, points) /* ARRAY_INFO(0, points, 0) */
599 	ZEND_ARG_INFO(0, num_pos)
600 	ZEND_ARG_INFO(0, col)
601 ZEND_END_ARG_INFO()
602 
603 ZEND_BEGIN_ARG_INFO(arginfo_imagefilledpolygon, 0)
604 	ZEND_ARG_INFO(0, im)
605 	ZEND_ARG_INFO(0, points) /* ARRAY_INFO(0, points, 0) */
606 	ZEND_ARG_INFO(0, num_pos)
607 	ZEND_ARG_INFO(0, col)
608 ZEND_END_ARG_INFO()
609 
610 ZEND_BEGIN_ARG_INFO(arginfo_imagefontwidth, 0)
611 	ZEND_ARG_INFO(0, font)
612 ZEND_END_ARG_INFO()
613 
614 ZEND_BEGIN_ARG_INFO(arginfo_imagefontheight, 0)
615 	ZEND_ARG_INFO(0, font)
616 ZEND_END_ARG_INFO()
617 
618 ZEND_BEGIN_ARG_INFO(arginfo_imagechar, 0)
619 	ZEND_ARG_INFO(0, im)
620 	ZEND_ARG_INFO(0, font)
621 	ZEND_ARG_INFO(0, x)
622 	ZEND_ARG_INFO(0, y)
623 	ZEND_ARG_INFO(0, c)
624 	ZEND_ARG_INFO(0, col)
625 ZEND_END_ARG_INFO()
626 
627 ZEND_BEGIN_ARG_INFO(arginfo_imagecharup, 0)
628 	ZEND_ARG_INFO(0, im)
629 	ZEND_ARG_INFO(0, font)
630 	ZEND_ARG_INFO(0, x)
631 	ZEND_ARG_INFO(0, y)
632 	ZEND_ARG_INFO(0, c)
633 	ZEND_ARG_INFO(0, col)
634 ZEND_END_ARG_INFO()
635 
636 ZEND_BEGIN_ARG_INFO(arginfo_imagestring, 0)
637 	ZEND_ARG_INFO(0, im)
638 	ZEND_ARG_INFO(0, font)
639 	ZEND_ARG_INFO(0, x)
640 	ZEND_ARG_INFO(0, y)
641 	ZEND_ARG_INFO(0, str)
642 	ZEND_ARG_INFO(0, col)
643 ZEND_END_ARG_INFO()
644 
645 ZEND_BEGIN_ARG_INFO(arginfo_imagestringup, 0)
646 	ZEND_ARG_INFO(0, im)
647 	ZEND_ARG_INFO(0, font)
648 	ZEND_ARG_INFO(0, x)
649 	ZEND_ARG_INFO(0, y)
650 	ZEND_ARG_INFO(0, str)
651 	ZEND_ARG_INFO(0, col)
652 ZEND_END_ARG_INFO()
653 
654 ZEND_BEGIN_ARG_INFO(arginfo_imagecopy, 0)
655 	ZEND_ARG_INFO(0, dst_im)
656 	ZEND_ARG_INFO(0, src_im)
657 	ZEND_ARG_INFO(0, dst_x)
658 	ZEND_ARG_INFO(0, dst_y)
659 	ZEND_ARG_INFO(0, src_x)
660 	ZEND_ARG_INFO(0, src_y)
661 	ZEND_ARG_INFO(0, src_w)
662 	ZEND_ARG_INFO(0, src_h)
663 ZEND_END_ARG_INFO()
664 
665 ZEND_BEGIN_ARG_INFO(arginfo_imagecopymerge, 0)
666 	ZEND_ARG_INFO(0, src_im)
667 	ZEND_ARG_INFO(0, dst_im)
668 	ZEND_ARG_INFO(0, dst_x)
669 	ZEND_ARG_INFO(0, dst_y)
670 	ZEND_ARG_INFO(0, src_x)
671 	ZEND_ARG_INFO(0, src_y)
672 	ZEND_ARG_INFO(0, src_w)
673 	ZEND_ARG_INFO(0, src_h)
674 	ZEND_ARG_INFO(0, pct)
675 ZEND_END_ARG_INFO()
676 
677 ZEND_BEGIN_ARG_INFO(arginfo_imagecopymergegray, 0)
678 	ZEND_ARG_INFO(0, src_im)
679 	ZEND_ARG_INFO(0, dst_im)
680 	ZEND_ARG_INFO(0, dst_x)
681 	ZEND_ARG_INFO(0, dst_y)
682 	ZEND_ARG_INFO(0, src_x)
683 	ZEND_ARG_INFO(0, src_y)
684 	ZEND_ARG_INFO(0, src_w)
685 	ZEND_ARG_INFO(0, src_h)
686 	ZEND_ARG_INFO(0, pct)
687 ZEND_END_ARG_INFO()
688 
689 ZEND_BEGIN_ARG_INFO(arginfo_imagecopyresized, 0)
690 	ZEND_ARG_INFO(0, dst_im)
691 	ZEND_ARG_INFO(0, src_im)
692 	ZEND_ARG_INFO(0, dst_x)
693 	ZEND_ARG_INFO(0, dst_y)
694 	ZEND_ARG_INFO(0, src_x)
695 	ZEND_ARG_INFO(0, src_y)
696 	ZEND_ARG_INFO(0, dst_w)
697 	ZEND_ARG_INFO(0, dst_h)
698 	ZEND_ARG_INFO(0, src_w)
699 	ZEND_ARG_INFO(0, src_h)
700 ZEND_END_ARG_INFO()
701 
702 ZEND_BEGIN_ARG_INFO(arginfo_imagesx, 0)
703 	ZEND_ARG_INFO(0, im)
704 ZEND_END_ARG_INFO()
705 
706 ZEND_BEGIN_ARG_INFO(arginfo_imagesy, 0)
707 	ZEND_ARG_INFO(0, im)
708 ZEND_END_ARG_INFO()
709 
710 ZEND_BEGIN_ARG_INFO(arginfo_imagesetclip, 0)
711 	ZEND_ARG_INFO(0, im)
712 	ZEND_ARG_INFO(0, x1)
713 	ZEND_ARG_INFO(0, y1)
714 	ZEND_ARG_INFO(0, x2)
715 	ZEND_ARG_INFO(0, y2)
716 ZEND_END_ARG_INFO()
717 
718 ZEND_BEGIN_ARG_INFO(arginfo_imagegetclip, 0)
719 	ZEND_ARG_INFO(0, im)
720 ZEND_END_ARG_INFO()
721 
722 #ifdef ENABLE_GD_TTF
723 #if HAVE_LIBFREETYPE
724 ZEND_BEGIN_ARG_INFO_EX(arginfo_imageftbbox, 0, 0, 4)
725 	ZEND_ARG_INFO(0, size)
726 	ZEND_ARG_INFO(0, angle)
727 	ZEND_ARG_INFO(0, font_file)
728 	ZEND_ARG_INFO(0, text)
729 	ZEND_ARG_INFO(0, extrainfo) /* ARRAY_INFO(0, extrainfo, 0) */
730 ZEND_END_ARG_INFO()
731 
732 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagefttext, 0, 0, 8)
733 	ZEND_ARG_INFO(0, im)
734 	ZEND_ARG_INFO(0, size)
735 	ZEND_ARG_INFO(0, angle)
736 	ZEND_ARG_INFO(0, x)
737 	ZEND_ARG_INFO(0, y)
738 	ZEND_ARG_INFO(0, col)
739 	ZEND_ARG_INFO(0, font_file)
740 	ZEND_ARG_INFO(0, text)
741 	ZEND_ARG_INFO(0, extrainfo) /* ARRAY_INFO(0, extrainfo, 0) */
742 ZEND_END_ARG_INFO()
743 #endif
744 
745 ZEND_BEGIN_ARG_INFO(arginfo_imagettfbbox, 0)
746 	ZEND_ARG_INFO(0, size)
747 	ZEND_ARG_INFO(0, angle)
748 	ZEND_ARG_INFO(0, font_file)
749 	ZEND_ARG_INFO(0, text)
750 ZEND_END_ARG_INFO()
751 
752 ZEND_BEGIN_ARG_INFO(arginfo_imagettftext, 0)
753 	ZEND_ARG_INFO(0, im)
754 	ZEND_ARG_INFO(0, size)
755 	ZEND_ARG_INFO(0, angle)
756 	ZEND_ARG_INFO(0, x)
757 	ZEND_ARG_INFO(0, y)
758 	ZEND_ARG_INFO(0, col)
759 	ZEND_ARG_INFO(0, font_file)
760 	ZEND_ARG_INFO(0, text)
761 ZEND_END_ARG_INFO()
762 #endif
763 
764 ZEND_BEGIN_ARG_INFO_EX(arginfo_image2wbmp, 0, 0, 1)
765 	ZEND_ARG_INFO(0, im)
766 	ZEND_ARG_INFO(0, filename)
767 	ZEND_ARG_INFO(0, threshold)
768 ZEND_END_ARG_INFO()
769 
770 #if defined(HAVE_GD_JPG)
771 ZEND_BEGIN_ARG_INFO(arginfo_jpeg2wbmp, 0)
772 	ZEND_ARG_INFO(0, f_org)
773 	ZEND_ARG_INFO(0, f_dest)
774 	ZEND_ARG_INFO(0, d_height)
775 	ZEND_ARG_INFO(0, d_width)
776 	ZEND_ARG_INFO(0, d_threshold)
777 ZEND_END_ARG_INFO()
778 #endif
779 
780 #if defined(HAVE_GD_PNG)
781 ZEND_BEGIN_ARG_INFO(arginfo_png2wbmp, 0)
782 	ZEND_ARG_INFO(0, f_org)
783 	ZEND_ARG_INFO(0, f_dest)
784 	ZEND_ARG_INFO(0, d_height)
785 	ZEND_ARG_INFO(0, d_width)
786 	ZEND_ARG_INFO(0, d_threshold)
787 ZEND_END_ARG_INFO()
788 #endif
789 
790 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagefilter, 0, 0, 2)
791 	ZEND_ARG_INFO(0, im)
792 	ZEND_ARG_INFO(0, filtertype)
793 	ZEND_ARG_INFO(0, arg1)
794 	ZEND_ARG_INFO(0, arg2)
795 	ZEND_ARG_INFO(0, arg3)
796 	ZEND_ARG_INFO(0, arg4)
797 ZEND_END_ARG_INFO()
798 
799 ZEND_BEGIN_ARG_INFO(arginfo_imageconvolution, 0)
800 	ZEND_ARG_INFO(0, im)
801 	ZEND_ARG_INFO(0, matrix3x3) /* ARRAY_INFO(0, matrix3x3, 0) */
802 	ZEND_ARG_INFO(0, div)
803 	ZEND_ARG_INFO(0, offset)
804 ZEND_END_ARG_INFO()
805 
806 ZEND_BEGIN_ARG_INFO(arginfo_imageflip, 0)
807 	ZEND_ARG_INFO(0, im)
808 	ZEND_ARG_INFO(0, mode)
809 ZEND_END_ARG_INFO()
810 
811 ZEND_BEGIN_ARG_INFO(arginfo_imageantialias, 0)
812 	ZEND_ARG_INFO(0, im)
813 	ZEND_ARG_INFO(0, on)
814 ZEND_END_ARG_INFO()
815 
816 ZEND_BEGIN_ARG_INFO(arginfo_imagecrop, 0)
817 	ZEND_ARG_INFO(0, im)
818 	ZEND_ARG_INFO(0, rect)
819 ZEND_END_ARG_INFO()
820 
821 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagecropauto, 0, 0, 1)
822 	ZEND_ARG_INFO(0, im)
823 	ZEND_ARG_INFO(0, mode)
824 	ZEND_ARG_INFO(0, threshold)
825 	ZEND_ARG_INFO(0, color)
826 ZEND_END_ARG_INFO()
827 
828 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagescale, 0, 0, 2)
829 	ZEND_ARG_INFO(0, im)
830 	ZEND_ARG_INFO(0, new_width)
831 	ZEND_ARG_INFO(0, new_height)
832 	ZEND_ARG_INFO(0, mode)
833 ZEND_END_ARG_INFO()
834 
835 ZEND_BEGIN_ARG_INFO_EX(arginfo_imageaffine, 0, 0, 2)
836 	ZEND_ARG_INFO(0, im)
837 	ZEND_ARG_INFO(0, affine)
838 	ZEND_ARG_INFO(0, clip)
839 ZEND_END_ARG_INFO()
840 
841 ZEND_BEGIN_ARG_INFO_EX(arginfo_imageaffinematrixget, 0, 0, 1)
842 	ZEND_ARG_INFO(0, type)
843 	ZEND_ARG_INFO(0, options)
844 ZEND_END_ARG_INFO()
845 
846 ZEND_BEGIN_ARG_INFO(arginfo_imageaffinematrixconcat, 0)
847 	ZEND_ARG_INFO(0, m1)
848 	ZEND_ARG_INFO(0, m2)
849 ZEND_END_ARG_INFO()
850 
851 ZEND_BEGIN_ARG_INFO_EX(arginfo_imagesetinterpolation, 0, 0, 1)
852 	ZEND_ARG_INFO(0, im)
853 	ZEND_ARG_INFO(0, method)
854 ZEND_END_ARG_INFO()
855 
856 ZEND_BEGIN_ARG_INFO_EX(arginfo_imageresolution, 0, 0, 1)
857 	ZEND_ARG_INFO(0, im)
858 	ZEND_ARG_INFO(0, res_x)
859 	ZEND_ARG_INFO(0, res_y)
860 ZEND_END_ARG_INFO()
861 
862 /* }}} */
863 
864 /* {{{ gd_functions[]
865  */
866 const zend_function_entry gd_functions[] = {
867 	PHP_FE(gd_info,                                 arginfo_gd_info)
868 	PHP_FE(imagearc,								arginfo_imagearc)
869 	PHP_FE(imageellipse,							arginfo_imageellipse)
870 	PHP_FE(imagechar,								arginfo_imagechar)
871 	PHP_FE(imagecharup,								arginfo_imagecharup)
872 	PHP_FE(imagecolorat,							arginfo_imagecolorat)
873 	PHP_FE(imagecolorallocate,						arginfo_imagecolorallocate)
874 	PHP_FE(imagepalettecopy,						arginfo_imagepalettecopy)
875 	PHP_FE(imagecreatefromstring,					arginfo_imagecreatefromstring)
876 	PHP_FE(imagecolorclosest,						arginfo_imagecolorclosest)
877 	PHP_FE(imagecolorclosesthwb,					arginfo_imagecolorclosesthwb)
878 	PHP_FE(imagecolordeallocate,					arginfo_imagecolordeallocate)
879 	PHP_FE(imagecolorresolve,						arginfo_imagecolorresolve)
880 	PHP_FE(imagecolorexact,							arginfo_imagecolorexact)
881 	PHP_FE(imagecolorset,							arginfo_imagecolorset)
882 	PHP_FE(imagecolortransparent,					arginfo_imagecolortransparent)
883 	PHP_FE(imagecolorstotal,						arginfo_imagecolorstotal)
884 	PHP_FE(imagecolorsforindex,						arginfo_imagecolorsforindex)
885 	PHP_FE(imagecopy,								arginfo_imagecopy)
886 	PHP_FE(imagecopymerge,							arginfo_imagecopymerge)
887 	PHP_FE(imagecopymergegray,						arginfo_imagecopymergegray)
888 	PHP_FE(imagecopyresized,						arginfo_imagecopyresized)
889 	PHP_FE(imagecreate,								arginfo_imagecreate)
890 	PHP_FE(imagecreatetruecolor,					arginfo_imagecreatetruecolor)
891 	PHP_FE(imageistruecolor,						arginfo_imageistruecolor)
892 	PHP_FE(imagetruecolortopalette,					arginfo_imagetruecolortopalette)
893 	PHP_FE(imagepalettetotruecolor,					arginfo_imagepalettetotruecolor)
894 	PHP_FE(imagesetthickness,						arginfo_imagesetthickness)
895 	PHP_FE(imagefilledarc,							arginfo_imagefilledarc)
896 	PHP_FE(imagefilledellipse,						arginfo_imagefilledellipse)
897 	PHP_FE(imagealphablending,						arginfo_imagealphablending)
898 	PHP_FE(imagesavealpha,							arginfo_imagesavealpha)
899 	PHP_FE(imagecolorallocatealpha,					arginfo_imagecolorallocatealpha)
900 	PHP_FE(imagecolorresolvealpha, 					arginfo_imagecolorresolvealpha)
901 	PHP_FE(imagecolorclosestalpha,					arginfo_imagecolorclosestalpha)
902 	PHP_FE(imagecolorexactalpha,					arginfo_imagecolorexactalpha)
903 	PHP_FE(imagecopyresampled,						arginfo_imagecopyresampled)
904 
905 #ifdef PHP_WIN32
906 	PHP_FE(imagegrabwindow,							arginfo_imagegrabwindow)
907 	PHP_FE(imagegrabscreen,							arginfo_imagegrabscreen)
908 #endif
909 
910 	PHP_FE(imagerotate,     						arginfo_imagerotate)
911 	PHP_FE(imageflip,								arginfo_imageflip)
912 
913 	PHP_FE(imageantialias,							arginfo_imageantialias)
914 	PHP_FE(imagecrop,								arginfo_imagecrop)
915 	PHP_FE(imagecropauto,							arginfo_imagecropauto)
916 	PHP_FE(imagescale,								arginfo_imagescale)
917 	PHP_FE(imageaffine,								arginfo_imageaffine)
918 	PHP_FE(imageaffinematrixconcat,					arginfo_imageaffinematrixconcat)
919 	PHP_FE(imageaffinematrixget,					arginfo_imageaffinematrixget)
920 	PHP_FE(imagesetinterpolation,                   arginfo_imagesetinterpolation)
921 	PHP_FE(imagesettile,							arginfo_imagesettile)
922 	PHP_FE(imagesetbrush,							arginfo_imagesetbrush)
923 	PHP_FE(imagesetstyle,							arginfo_imagesetstyle)
924 
925 #ifdef HAVE_GD_PNG
926 	PHP_FE(imagecreatefrompng,						arginfo_imagecreatefrompng)
927 #endif
928 #ifdef HAVE_GD_WEBP
929 	PHP_FE(imagecreatefromwebp,						arginfo_imagecreatefromwebp)
930 #endif
931 	PHP_FE(imagecreatefromgif,						arginfo_imagecreatefromgif)
932 #ifdef HAVE_GD_JPG
933 	PHP_FE(imagecreatefromjpeg,						arginfo_imagecreatefromjpeg)
934 #endif
935 	PHP_FE(imagecreatefromwbmp,						arginfo_imagecreatefromwbmp)
936 	PHP_FE(imagecreatefromxbm,						arginfo_imagecreatefromxbm)
937 #if defined(HAVE_GD_XPM)
938 	PHP_FE(imagecreatefromxpm,						arginfo_imagecreatefromxpm)
939 #endif
940 	PHP_FE(imagecreatefromgd,						arginfo_imagecreatefromgd)
941 	PHP_FE(imagecreatefromgd2,						arginfo_imagecreatefromgd2)
942 	PHP_FE(imagecreatefromgd2part,					arginfo_imagecreatefromgd2part)
943 #ifdef HAVE_GD_BMP
944 	PHP_FE(imagecreatefrombmp,						arginfo_imagecreatefrombmp)
945 #endif
946 #ifdef HAVE_GD_PNG
947 	PHP_FE(imagepng,								arginfo_imagepng)
948 #endif
949 #ifdef HAVE_GD_WEBP
950 	PHP_FE(imagewebp,								arginfo_imagewebp)
951 #endif
952 	PHP_FE(imagegif,								arginfo_imagegif)
953 #ifdef HAVE_GD_JPG
954 	PHP_FE(imagejpeg,								arginfo_imagejpeg)
955 #endif
956 	PHP_FE(imagewbmp,                               arginfo_imagewbmp)
957 	PHP_FE(imagegd,									arginfo_imagegd)
958 	PHP_FE(imagegd2,								arginfo_imagegd2)
959 #ifdef HAVE_GD_BMP
960 	PHP_FE(imagebmp,								arginfo_imagebmp)
961 #endif
962 
963 	PHP_FE(imagedestroy,							arginfo_imagedestroy)
964 	PHP_FE(imagegammacorrect,						arginfo_imagegammacorrect)
965 	PHP_FE(imagefill,								arginfo_imagefill)
966 	PHP_FE(imagefilledpolygon,						arginfo_imagefilledpolygon)
967 	PHP_FE(imagefilledrectangle,					arginfo_imagefilledrectangle)
968 	PHP_FE(imagefilltoborder,						arginfo_imagefilltoborder)
969 	PHP_FE(imagefontwidth,							arginfo_imagefontwidth)
970 	PHP_FE(imagefontheight,							arginfo_imagefontheight)
971 	PHP_FE(imageinterlace,							arginfo_imageinterlace)
972 	PHP_FE(imageline,								arginfo_imageline)
973 	PHP_FE(imageloadfont,							arginfo_imageloadfont)
974 	PHP_FE(imagepolygon,							arginfo_imagepolygon)
975 	PHP_FE(imageopenpolygon,						arginfo_imageopenpolygon)
976 	PHP_FE(imagerectangle,							arginfo_imagerectangle)
977 	PHP_FE(imagesetpixel,							arginfo_imagesetpixel)
978 	PHP_FE(imagestring,								arginfo_imagestring)
979 	PHP_FE(imagestringup,							arginfo_imagestringup)
980 	PHP_FE(imagesx,									arginfo_imagesx)
981 	PHP_FE(imagesy,									arginfo_imagesy)
982 	PHP_FE(imagesetclip,							arginfo_imagesetclip)
983 	PHP_FE(imagegetclip,							arginfo_imagegetclip)
984 	PHP_FE(imagedashedline,							arginfo_imagedashedline)
985 
986 #ifdef ENABLE_GD_TTF
987 	PHP_FE(imagettfbbox,							arginfo_imagettfbbox)
988 	PHP_FE(imagettftext,							arginfo_imagettftext)
989 #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
990 	PHP_FE(imageftbbox,								arginfo_imageftbbox)
991 	PHP_FE(imagefttext,								arginfo_imagefttext)
992 #endif
993 #endif
994 
995 	PHP_FE(imagetypes,								arginfo_imagetypes)
996 
997 #if defined(HAVE_GD_JPG)
998 	PHP_DEP_FE(jpeg2wbmp,							arginfo_jpeg2wbmp)
999 #endif
1000 #if defined(HAVE_GD_PNG)
1001 	PHP_DEP_FE(png2wbmp,							arginfo_png2wbmp)
1002 #endif
1003 	PHP_FE(image2wbmp,								arginfo_image2wbmp)
1004 	PHP_FE(imagelayereffect,						arginfo_imagelayereffect)
1005 	PHP_FE(imagexbm,                                arginfo_imagexbm)
1006 
1007 	PHP_FE(imagecolormatch,							arginfo_imagecolormatch)
1008 
1009 /* gd filters */
1010 	PHP_FE(imagefilter,     						arginfo_imagefilter)
1011 	PHP_FE(imageconvolution,						arginfo_imageconvolution)
1012 
1013 	PHP_FE(imageresolution,							arginfo_imageresolution)
1014 
1015 	PHP_FE_END
1016 };
1017 /* }}} */
1018 
1019 zend_module_entry gd_module_entry = {
1020 	STANDARD_MODULE_HEADER,
1021 	"gd",
1022 	gd_functions,
1023 	PHP_MINIT(gd),
1024 	NULL,
1025 	NULL,
1026 #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
1027 	PHP_RSHUTDOWN(gd),
1028 #else
1029 	NULL,
1030 #endif
1031 	PHP_MINFO(gd),
1032 	PHP_GD_VERSION,
1033 	STANDARD_MODULE_PROPERTIES
1034 };
1035 
1036 #ifdef COMPILE_DL_GD
1037 ZEND_GET_MODULE(gd)
1038 #endif
1039 
1040 /* {{{ PHP_INI_BEGIN */
PHP_INI_BEGIN()1041 PHP_INI_BEGIN()
1042 	PHP_INI_ENTRY("gd.jpeg_ignore_warning", "1", PHP_INI_ALL, NULL)
1043 PHP_INI_END()
1044 /* }}} */
1045 
1046 /* {{{ php_free_gd_image
1047  */
1048 static void php_free_gd_image(zend_resource *rsrc)
1049 {
1050 	gdImageDestroy((gdImagePtr) rsrc->ptr);
1051 }
1052 /* }}} */
1053 
1054 /* {{{ php_free_gd_font
1055  */
php_free_gd_font(zend_resource * rsrc)1056 static void php_free_gd_font(zend_resource *rsrc)
1057 {
1058 	gdFontPtr fp = (gdFontPtr) rsrc->ptr;
1059 
1060 	if (fp->data) {
1061 		efree(fp->data);
1062 	}
1063 
1064 	efree(fp);
1065 }
1066 /* }}} */
1067 
1068 /* {{{ php_gd_error_method
1069  */
php_gd_error_method(int type,const char * format,va_list args)1070 void php_gd_error_method(int type, const char *format, va_list args)
1071 {
1072 
1073 	switch (type) {
1074 #ifndef PHP_WIN32
1075 		case GD_DEBUG:
1076 		case GD_INFO:
1077 #endif
1078 		case GD_NOTICE:
1079 			type = E_NOTICE;
1080 			break;
1081 		case GD_WARNING:
1082 			type = E_WARNING;
1083 			break;
1084 		default:
1085 			type = E_ERROR;
1086 	}
1087 	php_verror(NULL, "", type, format, args);
1088 }
1089 /* }}} */
1090 
1091 /* {{{ PHP_MINIT_FUNCTION
1092  */
PHP_MINIT_FUNCTION(gd)1093 PHP_MINIT_FUNCTION(gd)
1094 {
1095 	le_gd = zend_register_list_destructors_ex(php_free_gd_image, NULL, "gd", module_number);
1096 	le_gd_font = zend_register_list_destructors_ex(php_free_gd_font, NULL, "gd font", module_number);
1097 
1098 #if HAVE_GD_BUNDLED && HAVE_LIBFREETYPE
1099 	gdFontCacheMutexSetup();
1100 #endif
1101 	gdSetErrorMethod(php_gd_error_method);
1102 
1103 	REGISTER_INI_ENTRIES();
1104 
1105 	REGISTER_LONG_CONSTANT("IMG_GIF", 1, CONST_CS | CONST_PERSISTENT);
1106 	REGISTER_LONG_CONSTANT("IMG_JPG", 2, CONST_CS | CONST_PERSISTENT);
1107 	REGISTER_LONG_CONSTANT("IMG_JPEG", 2, CONST_CS | CONST_PERSISTENT);
1108 	REGISTER_LONG_CONSTANT("IMG_PNG", 4, CONST_CS | CONST_PERSISTENT);
1109 	REGISTER_LONG_CONSTANT("IMG_WBMP", 8, CONST_CS | CONST_PERSISTENT);
1110 	REGISTER_LONG_CONSTANT("IMG_XPM", 16, CONST_CS | CONST_PERSISTENT);
1111 	REGISTER_LONG_CONSTANT("IMG_WEBP", 32, CONST_CS | CONST_PERSISTENT);
1112 	REGISTER_LONG_CONSTANT("IMG_BMP", 64, CONST_CS | CONST_PERSISTENT);
1113 
1114 	/* special colours for gd */
1115 	REGISTER_LONG_CONSTANT("IMG_COLOR_TILED", gdTiled, CONST_CS | CONST_PERSISTENT);
1116 	REGISTER_LONG_CONSTANT("IMG_COLOR_STYLED", gdStyled, CONST_CS | CONST_PERSISTENT);
1117 	REGISTER_LONG_CONSTANT("IMG_COLOR_BRUSHED", gdBrushed, CONST_CS | CONST_PERSISTENT);
1118 	REGISTER_LONG_CONSTANT("IMG_COLOR_STYLEDBRUSHED", gdStyledBrushed, CONST_CS | CONST_PERSISTENT);
1119 	REGISTER_LONG_CONSTANT("IMG_COLOR_TRANSPARENT", gdTransparent, CONST_CS | CONST_PERSISTENT);
1120 
1121 	/* for imagefilledarc */
1122 	REGISTER_LONG_CONSTANT("IMG_ARC_ROUNDED", gdArc, CONST_CS | CONST_PERSISTENT);
1123 	REGISTER_LONG_CONSTANT("IMG_ARC_PIE", gdPie, CONST_CS | CONST_PERSISTENT);
1124 	REGISTER_LONG_CONSTANT("IMG_ARC_CHORD", gdChord, CONST_CS | CONST_PERSISTENT);
1125 	REGISTER_LONG_CONSTANT("IMG_ARC_NOFILL", gdNoFill, CONST_CS | CONST_PERSISTENT);
1126 	REGISTER_LONG_CONSTANT("IMG_ARC_EDGED", gdEdged, CONST_CS | CONST_PERSISTENT);
1127 
1128     /* GD2 image format types */
1129 	REGISTER_LONG_CONSTANT("IMG_GD2_RAW", GD2_FMT_RAW, CONST_CS | CONST_PERSISTENT);
1130 	REGISTER_LONG_CONSTANT("IMG_GD2_COMPRESSED", GD2_FMT_COMPRESSED, CONST_CS | CONST_PERSISTENT);
1131 	REGISTER_LONG_CONSTANT("IMG_FLIP_HORIZONTAL", GD_FLIP_HORINZONTAL, CONST_CS | CONST_PERSISTENT);
1132 	REGISTER_LONG_CONSTANT("IMG_FLIP_VERTICAL", GD_FLIP_VERTICAL, CONST_CS | CONST_PERSISTENT);
1133 	REGISTER_LONG_CONSTANT("IMG_FLIP_BOTH", GD_FLIP_BOTH, CONST_CS | CONST_PERSISTENT);
1134 	REGISTER_LONG_CONSTANT("IMG_EFFECT_REPLACE", gdEffectReplace, CONST_CS | CONST_PERSISTENT);
1135 	REGISTER_LONG_CONSTANT("IMG_EFFECT_ALPHABLEND", gdEffectAlphaBlend, CONST_CS | CONST_PERSISTENT);
1136 	REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
1137 	REGISTER_LONG_CONSTANT("IMG_EFFECT_OVERLAY", gdEffectOverlay, CONST_CS | CONST_PERSISTENT);
1138 #ifdef gdEffectMultiply
1139 	REGISTER_LONG_CONSTANT("IMG_EFFECT_MULTIPLY", gdEffectMultiply, CONST_CS | CONST_PERSISTENT);
1140 #endif
1141 
1142 	REGISTER_LONG_CONSTANT("IMG_CROP_DEFAULT", GD_CROP_DEFAULT, CONST_CS | CONST_PERSISTENT);
1143 	REGISTER_LONG_CONSTANT("IMG_CROP_TRANSPARENT", GD_CROP_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
1144 	REGISTER_LONG_CONSTANT("IMG_CROP_BLACK", GD_CROP_BLACK, CONST_CS | CONST_PERSISTENT);
1145 	REGISTER_LONG_CONSTANT("IMG_CROP_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
1146 	REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, CONST_CS | CONST_PERSISTENT);
1147 	REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
1148 
1149 
1150 	REGISTER_LONG_CONSTANT("IMG_BELL", GD_BELL, CONST_CS | CONST_PERSISTENT);
1151 	REGISTER_LONG_CONSTANT("IMG_BESSEL", GD_BESSEL, CONST_CS | CONST_PERSISTENT);
1152 	REGISTER_LONG_CONSTANT("IMG_BILINEAR_FIXED", GD_BILINEAR_FIXED, CONST_CS | CONST_PERSISTENT);
1153 	REGISTER_LONG_CONSTANT("IMG_BICUBIC", GD_BICUBIC, CONST_CS | CONST_PERSISTENT);
1154 	REGISTER_LONG_CONSTANT("IMG_BICUBIC_FIXED", GD_BICUBIC_FIXED, CONST_CS | CONST_PERSISTENT);
1155 	REGISTER_LONG_CONSTANT("IMG_BLACKMAN", GD_BLACKMAN, CONST_CS | CONST_PERSISTENT);
1156 	REGISTER_LONG_CONSTANT("IMG_BOX", GD_BOX, CONST_CS | CONST_PERSISTENT);
1157 	REGISTER_LONG_CONSTANT("IMG_BSPLINE", GD_BSPLINE, CONST_CS | CONST_PERSISTENT);
1158 	REGISTER_LONG_CONSTANT("IMG_CATMULLROM", GD_CATMULLROM, CONST_CS | CONST_PERSISTENT);
1159 	REGISTER_LONG_CONSTANT("IMG_GAUSSIAN", GD_GAUSSIAN, CONST_CS | CONST_PERSISTENT);
1160 	REGISTER_LONG_CONSTANT("IMG_GENERALIZED_CUBIC", GD_GENERALIZED_CUBIC, CONST_CS | CONST_PERSISTENT);
1161 	REGISTER_LONG_CONSTANT("IMG_HERMITE", GD_HERMITE, CONST_CS | CONST_PERSISTENT);
1162 	REGISTER_LONG_CONSTANT("IMG_HAMMING", GD_HAMMING, CONST_CS | CONST_PERSISTENT);
1163 	REGISTER_LONG_CONSTANT("IMG_HANNING", GD_HANNING, CONST_CS | CONST_PERSISTENT);
1164 	REGISTER_LONG_CONSTANT("IMG_MITCHELL", GD_MITCHELL, CONST_CS | CONST_PERSISTENT);
1165 	REGISTER_LONG_CONSTANT("IMG_POWER", GD_POWER, CONST_CS | CONST_PERSISTENT);
1166 	REGISTER_LONG_CONSTANT("IMG_QUADRATIC", GD_QUADRATIC, CONST_CS | CONST_PERSISTENT);
1167 	REGISTER_LONG_CONSTANT("IMG_SINC", GD_SINC, CONST_CS | CONST_PERSISTENT);
1168 	REGISTER_LONG_CONSTANT("IMG_NEAREST_NEIGHBOUR", GD_NEAREST_NEIGHBOUR, CONST_CS | CONST_PERSISTENT);
1169 	REGISTER_LONG_CONSTANT("IMG_WEIGHTED4", GD_WEIGHTED4, CONST_CS | CONST_PERSISTENT);
1170 	REGISTER_LONG_CONSTANT("IMG_TRIANGLE", GD_TRIANGLE, CONST_CS | CONST_PERSISTENT);
1171 
1172 	REGISTER_LONG_CONSTANT("IMG_AFFINE_TRANSLATE", GD_AFFINE_TRANSLATE, CONST_CS | CONST_PERSISTENT);
1173 	REGISTER_LONG_CONSTANT("IMG_AFFINE_SCALE", GD_AFFINE_SCALE, CONST_CS | CONST_PERSISTENT);
1174 	REGISTER_LONG_CONSTANT("IMG_AFFINE_ROTATE", GD_AFFINE_ROTATE, CONST_CS | CONST_PERSISTENT);
1175 	REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_HORIZONTAL", GD_AFFINE_SHEAR_HORIZONTAL, CONST_CS | CONST_PERSISTENT);
1176 	REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_VERTICAL", GD_AFFINE_SHEAR_VERTICAL, CONST_CS | CONST_PERSISTENT);
1177 
1178 #if defined(HAVE_GD_BUNDLED)
1179 	REGISTER_LONG_CONSTANT("GD_BUNDLED", 1, CONST_CS | CONST_PERSISTENT);
1180 #else
1181 	REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
1182 #endif
1183 
1184 	/* Section Filters */
1185 	REGISTER_LONG_CONSTANT("IMG_FILTER_NEGATE", IMAGE_FILTER_NEGATE, CONST_CS | CONST_PERSISTENT);
1186 	REGISTER_LONG_CONSTANT("IMG_FILTER_GRAYSCALE", IMAGE_FILTER_GRAYSCALE, CONST_CS | CONST_PERSISTENT);
1187 	REGISTER_LONG_CONSTANT("IMG_FILTER_BRIGHTNESS", IMAGE_FILTER_BRIGHTNESS, CONST_CS | CONST_PERSISTENT);
1188 	REGISTER_LONG_CONSTANT("IMG_FILTER_CONTRAST", IMAGE_FILTER_CONTRAST, CONST_CS | CONST_PERSISTENT);
1189 	REGISTER_LONG_CONSTANT("IMG_FILTER_COLORIZE", IMAGE_FILTER_COLORIZE, CONST_CS | CONST_PERSISTENT);
1190 	REGISTER_LONG_CONSTANT("IMG_FILTER_EDGEDETECT", IMAGE_FILTER_EDGEDETECT, CONST_CS | CONST_PERSISTENT);
1191 	REGISTER_LONG_CONSTANT("IMG_FILTER_GAUSSIAN_BLUR", IMAGE_FILTER_GAUSSIAN_BLUR, CONST_CS | CONST_PERSISTENT);
1192 	REGISTER_LONG_CONSTANT("IMG_FILTER_SELECTIVE_BLUR", IMAGE_FILTER_SELECTIVE_BLUR, CONST_CS | CONST_PERSISTENT);
1193 	REGISTER_LONG_CONSTANT("IMG_FILTER_EMBOSS", IMAGE_FILTER_EMBOSS, CONST_CS | CONST_PERSISTENT);
1194 	REGISTER_LONG_CONSTANT("IMG_FILTER_MEAN_REMOVAL", IMAGE_FILTER_MEAN_REMOVAL, CONST_CS | CONST_PERSISTENT);
1195 	REGISTER_LONG_CONSTANT("IMG_FILTER_SMOOTH", IMAGE_FILTER_SMOOTH, CONST_CS | CONST_PERSISTENT);
1196 	REGISTER_LONG_CONSTANT("IMG_FILTER_PIXELATE", IMAGE_FILTER_PIXELATE, CONST_CS | CONST_PERSISTENT);
1197 	/* End Section Filters */
1198 
1199 #ifdef GD_VERSION_STRING
1200 	REGISTER_STRING_CONSTANT("GD_VERSION", GD_VERSION_STRING, CONST_CS | CONST_PERSISTENT);
1201 #endif
1202 
1203 #if defined(GD_MAJOR_VERSION) && defined(GD_MINOR_VERSION) && defined(GD_RELEASE_VERSION) && defined(GD_EXTRA_VERSION)
1204 	REGISTER_LONG_CONSTANT("GD_MAJOR_VERSION", GD_MAJOR_VERSION, CONST_CS | CONST_PERSISTENT);
1205 	REGISTER_LONG_CONSTANT("GD_MINOR_VERSION", GD_MINOR_VERSION, CONST_CS | CONST_PERSISTENT);
1206 	REGISTER_LONG_CONSTANT("GD_RELEASE_VERSION", GD_RELEASE_VERSION, CONST_CS | CONST_PERSISTENT);
1207 	REGISTER_STRING_CONSTANT("GD_EXTRA_VERSION", GD_EXTRA_VERSION, CONST_CS | CONST_PERSISTENT);
1208 #endif
1209 
1210 
1211 #ifdef HAVE_GD_PNG
1212 
1213 	/*
1214 	 * cannot include #include "png.h"
1215 	 * /usr/include/pngconf.h:310:2: error: #error png.h already includes setjmp.h with some additional fixup.
1216 	 * as error, use the values for now...
1217 	 */
1218 	REGISTER_LONG_CONSTANT("PNG_NO_FILTER",	    0x00, CONST_CS | CONST_PERSISTENT);
1219 	REGISTER_LONG_CONSTANT("PNG_FILTER_NONE",   0x08, CONST_CS | CONST_PERSISTENT);
1220 	REGISTER_LONG_CONSTANT("PNG_FILTER_SUB",    0x10, CONST_CS | CONST_PERSISTENT);
1221 	REGISTER_LONG_CONSTANT("PNG_FILTER_UP",     0x20, CONST_CS | CONST_PERSISTENT);
1222 	REGISTER_LONG_CONSTANT("PNG_FILTER_AVG",    0x40, CONST_CS | CONST_PERSISTENT);
1223 	REGISTER_LONG_CONSTANT("PNG_FILTER_PAETH",  0x80, CONST_CS | CONST_PERSISTENT);
1224 	REGISTER_LONG_CONSTANT("PNG_ALL_FILTERS",   0x08 | 0x10 | 0x20 | 0x40 | 0x80, CONST_CS | CONST_PERSISTENT);
1225 #endif
1226 
1227 	return SUCCESS;
1228 }
1229 /* }}} */
1230 
1231 /* {{{ PHP_RSHUTDOWN_FUNCTION
1232  */
1233 #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
PHP_RSHUTDOWN_FUNCTION(gd)1234 PHP_RSHUTDOWN_FUNCTION(gd)
1235 {
1236 	gdFontCacheShutdown();
1237 	return SUCCESS;
1238 }
1239 #endif
1240 /* }}} */
1241 
1242 #if defined(HAVE_GD_BUNDLED)
1243 #define PHP_GD_VERSION_STRING "bundled (2.1.0 compatible)"
1244 #else
1245 # define PHP_GD_VERSION_STRING GD_VERSION_STRING
1246 #endif
1247 
1248 /* {{{ PHP_MINFO_FUNCTION
1249  */
PHP_MINFO_FUNCTION(gd)1250 PHP_MINFO_FUNCTION(gd)
1251 {
1252 	php_info_print_table_start();
1253 	php_info_print_table_row(2, "GD Support", "enabled");
1254 
1255 	/* need to use a PHPAPI function here because it is external module in windows */
1256 
1257 #if defined(HAVE_GD_BUNDLED)
1258 	php_info_print_table_row(2, "GD Version", PHP_GD_VERSION_STRING);
1259 #else
1260 	php_info_print_table_row(2, "GD headers Version", PHP_GD_VERSION_STRING);
1261 #if defined(HAVE_GD_LIBVERSION)
1262 	php_info_print_table_row(2, "GD library Version", gdVersionString());
1263 #endif
1264 #endif
1265 
1266 #ifdef ENABLE_GD_TTF
1267 	php_info_print_table_row(2, "FreeType Support", "enabled");
1268 #if HAVE_LIBFREETYPE
1269 	php_info_print_table_row(2, "FreeType Linkage", "with freetype");
1270 	{
1271 		char tmp[256];
1272 
1273 #ifdef FREETYPE_PATCH
1274 		snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
1275 #elif defined(FREETYPE_MAJOR)
1276 		snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
1277 #else
1278 		snprintf(tmp, sizeof(tmp), "1.x");
1279 #endif
1280 		php_info_print_table_row(2, "FreeType Version", tmp);
1281 	}
1282 #else
1283 	php_info_print_table_row(2, "FreeType Linkage", "with unknown library");
1284 #endif
1285 #endif
1286 
1287 	php_info_print_table_row(2, "GIF Read Support", "enabled");
1288 	php_info_print_table_row(2, "GIF Create Support", "enabled");
1289 
1290 #ifdef HAVE_GD_JPG
1291 	{
1292 		php_info_print_table_row(2, "JPEG Support", "enabled");
1293 		php_info_print_table_row(2, "libJPEG Version", gdJpegGetVersionString());
1294 	}
1295 #endif
1296 
1297 #ifdef HAVE_GD_PNG
1298 	php_info_print_table_row(2, "PNG Support", "enabled");
1299 	php_info_print_table_row(2, "libPNG Version", gdPngGetVersionString());
1300 #endif
1301 	php_info_print_table_row(2, "WBMP Support", "enabled");
1302 #if defined(HAVE_GD_XPM)
1303 	php_info_print_table_row(2, "XPM Support", "enabled");
1304 	{
1305 		char tmp[12];
1306 		snprintf(tmp, sizeof(tmp), "%d", XpmLibraryVersion());
1307 		php_info_print_table_row(2, "libXpm Version", tmp);
1308 	}
1309 #endif
1310 	php_info_print_table_row(2, "XBM Support", "enabled");
1311 #if defined(USE_GD_JISX0208)
1312 	php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
1313 #endif
1314 #ifdef HAVE_GD_WEBP
1315 	php_info_print_table_row(2, "WebP Support", "enabled");
1316 #endif
1317 	php_info_print_table_end();
1318 	DISPLAY_INI_ENTRIES();
1319 }
1320 /* }}} */
1321 
1322 /* {{{ proto array gd_info()
1323  */
PHP_FUNCTION(gd_info)1324 PHP_FUNCTION(gd_info)
1325 {
1326 	if (zend_parse_parameters_none() == FAILURE) {
1327 		RETURN_FALSE;
1328 	}
1329 
1330 	array_init(return_value);
1331 
1332 	add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING);
1333 
1334 #ifdef ENABLE_GD_TTF
1335 	add_assoc_bool(return_value, "FreeType Support", 1);
1336 #if HAVE_LIBFREETYPE
1337 	add_assoc_string(return_value, "FreeType Linkage", "with freetype");
1338 #else
1339 	add_assoc_string(return_value, "FreeType Linkage", "with unknown library");
1340 #endif
1341 #else
1342 	add_assoc_bool(return_value, "FreeType Support", 0);
1343 #endif
1344 	add_assoc_bool(return_value, "GIF Read Support", 1);
1345 	add_assoc_bool(return_value, "GIF Create Support", 1);
1346 #ifdef HAVE_GD_JPG
1347 	add_assoc_bool(return_value, "JPEG Support", 1);
1348 #else
1349 	add_assoc_bool(return_value, "JPEG Support", 0);
1350 #endif
1351 #ifdef HAVE_GD_PNG
1352 	add_assoc_bool(return_value, "PNG Support", 1);
1353 #else
1354 	add_assoc_bool(return_value, "PNG Support", 0);
1355 #endif
1356 	add_assoc_bool(return_value, "WBMP Support", 1);
1357 #if defined(HAVE_GD_XPM)
1358 	add_assoc_bool(return_value, "XPM Support", 1);
1359 #else
1360 	add_assoc_bool(return_value, "XPM Support", 0);
1361 #endif
1362 	add_assoc_bool(return_value, "XBM Support", 1);
1363 #ifdef HAVE_GD_WEBP
1364 	add_assoc_bool(return_value, "WebP Support", 1);
1365 #else
1366 	add_assoc_bool(return_value, "WebP Support", 0);
1367 #endif
1368 #ifdef HAVE_GD_BMP
1369 	add_assoc_bool(return_value, "BMP Support", 1);
1370 #else
1371 	add_assoc_bool(return_value, "BMP Support", 0);
1372 #endif
1373 #if defined(USE_GD_JISX0208)
1374 	add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
1375 #else
1376 	add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
1377 #endif
1378 }
1379 /* }}} */
1380 
1381 /* Need this for cpdf. See also comment in file.c php3i_get_le_fp() */
phpi_get_le_gd(void)1382 PHP_GD_API int phpi_get_le_gd(void)
1383 {
1384 	return le_gd;
1385 }
1386 /* }}} */
1387 
1388 #define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
1389 
1390 /* {{{ proto int imageloadfont(string filename)
1391    Load a new font */
PHP_FUNCTION(imageloadfont)1392 PHP_FUNCTION(imageloadfont)
1393 {
1394 	zval *ind;
1395 	zend_string *file;
1396 	int hdr_size = sizeof(gdFont) - sizeof(char *);
1397 	int body_size, n = 0, b, i, body_size_check;
1398 	gdFontPtr font;
1399 	php_stream *stream;
1400 
1401 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file) == FAILURE) {
1402 		return;
1403 	}
1404 
1405 	stream = php_stream_open_wrapper(ZSTR_VAL(file), "rb", IGNORE_PATH | IGNORE_URL_WIN | REPORT_ERRORS, NULL);
1406 	if (stream == NULL) {
1407 		RETURN_FALSE;
1408 	}
1409 
1410 	/* Only supports a architecture-dependent binary dump format
1411 	 * at the moment.
1412 	 * The file format is like this on machines with 32-byte integers:
1413 	 *
1414 	 * byte 0-3:   (int) number of characters in the font
1415 	 * byte 4-7:   (int) value of first character in the font (often 32, space)
1416 	 * byte 8-11:  (int) pixel width of each character
1417 	 * byte 12-15: (int) pixel height of each character
1418 	 * bytes 16-:  (char) array with character data, one byte per pixel
1419 	 *                    in each character, for a total of
1420 	 *                    (nchars*width*height) bytes.
1421 	 */
1422 	font = (gdFontPtr) emalloc(sizeof(gdFont));
1423 	b = 0;
1424 	while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b))) {
1425 		b += n;
1426 	}
1427 
1428 	if (!n) {
1429 		efree(font);
1430 		if (php_stream_eof(stream)) {
1431 			php_error_docref(NULL, E_WARNING, "End of file while reading header");
1432 		} else {
1433 			php_error_docref(NULL, E_WARNING, "Error while reading header");
1434 		}
1435 		php_stream_close(stream);
1436 		RETURN_FALSE;
1437 	}
1438 	i = php_stream_tell(stream);
1439 	php_stream_seek(stream, 0, SEEK_END);
1440 	body_size_check = php_stream_tell(stream) - hdr_size;
1441 	php_stream_seek(stream, i, SEEK_SET);
1442 
1443 	body_size = font->w * font->h * font->nchars;
1444 	if (body_size != body_size_check) {
1445 		font->w = FLIPWORD(font->w);
1446 		font->h = FLIPWORD(font->h);
1447 		font->nchars = FLIPWORD(font->nchars);
1448 		body_size = font->w * font->h * font->nchars;
1449 	}
1450 
1451 	if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
1452 		php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
1453 		efree(font);
1454 		php_stream_close(stream);
1455 		RETURN_FALSE;
1456 	}
1457 
1458 	if (body_size != body_size_check) {
1459 		php_error_docref(NULL, E_WARNING, "Error reading font");
1460 		efree(font);
1461 		php_stream_close(stream);
1462 		RETURN_FALSE;
1463 	}
1464 
1465 	font->data = emalloc(body_size);
1466 	b = 0;
1467 	while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b))) {
1468 		b += n;
1469 	}
1470 
1471 	if (!n) {
1472 		efree(font->data);
1473 		efree(font);
1474 		if (php_stream_eof(stream)) {
1475 			php_error_docref(NULL, E_WARNING, "End of file while reading body");
1476 		} else {
1477 			php_error_docref(NULL, E_WARNING, "Error while reading body");
1478 		}
1479 		php_stream_close(stream);
1480 		RETURN_FALSE;
1481 	}
1482 	php_stream_close(stream);
1483 
1484 	ind = zend_list_insert(font, le_gd_font);
1485 
1486 	/* Adding 5 to the font index so we will never have font indices
1487 	 * that overlap with the old fonts (with indices 1-5).  The first
1488 	 * list index given out is always 1.
1489 	 */
1490 	RETURN_LONG(Z_RES_HANDLE_P(ind) + 5);
1491 }
1492 /* }}} */
1493 
1494 /* {{{ proto bool imagesetstyle(resource im, array styles)
1495    Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
PHP_FUNCTION(imagesetstyle)1496 PHP_FUNCTION(imagesetstyle)
1497 {
1498 	zval *IM, *styles, *item;
1499 	gdImagePtr im;
1500 	int *stylearr;
1501 	int index = 0;
1502     uint32_t num_styles;
1503 
1504 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra", &IM, &styles) == FAILURE)  {
1505 		return;
1506 	}
1507 
1508 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1509 		RETURN_FALSE;
1510 	}
1511 
1512     num_styles = zend_hash_num_elements(Z_ARRVAL_P(styles));
1513     if (num_styles == 0) {
1514         php_error_docref(NULL, E_WARNING, "styles array must not be empty");
1515         RETURN_FALSE;
1516     }
1517 
1518 	/* copy the style values in the stylearr */
1519 	stylearr = safe_emalloc(sizeof(int), num_styles, 0);
1520 
1521 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(styles), item) {
1522 		stylearr[index++] = zval_get_long(item);
1523 	} ZEND_HASH_FOREACH_END();
1524 
1525 	gdImageSetStyle(im, stylearr, index);
1526 
1527 	efree(stylearr);
1528 
1529 	RETURN_TRUE;
1530 }
1531 /* }}} */
1532 
1533 /* {{{ proto resource imagecreatetruecolor(int x_size, int y_size)
1534    Create a new true color image */
PHP_FUNCTION(imagecreatetruecolor)1535 PHP_FUNCTION(imagecreatetruecolor)
1536 {
1537 	zend_long x_size, y_size;
1538 	gdImagePtr im;
1539 
1540 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
1541 		return;
1542 	}
1543 
1544 	if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) {
1545 		php_error_docref(NULL, E_WARNING, "Invalid image dimensions");
1546 		RETURN_FALSE;
1547 	}
1548 
1549 	im = gdImageCreateTrueColor(x_size, y_size);
1550 
1551 	if (!im) {
1552 		RETURN_FALSE;
1553 	}
1554 
1555 	RETURN_RES(zend_register_resource(im, le_gd));
1556 }
1557 /* }}} */
1558 
1559 /* {{{ proto bool imageistruecolor(resource im)
1560    return true if the image uses truecolor */
PHP_FUNCTION(imageistruecolor)1561 PHP_FUNCTION(imageistruecolor)
1562 {
1563 	zval *IM;
1564 	gdImagePtr im;
1565 
1566 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
1567 		return;
1568 	}
1569 
1570 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1571 		RETURN_FALSE;
1572 	}
1573 
1574 	RETURN_BOOL(im->trueColor);
1575 }
1576 /* }}} */
1577 
1578 /* {{{ proto void imagetruecolortopalette(resource im, bool ditherFlag, int colorsWanted)
1579    Convert a true color image to a palette based image with a number of colors, optionally using dithering. */
PHP_FUNCTION(imagetruecolortopalette)1580 PHP_FUNCTION(imagetruecolortopalette)
1581 {
1582 	zval *IM;
1583 	zend_bool dither;
1584 	zend_long ncolors;
1585 	gdImagePtr im;
1586 
1587 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rbl", &IM, &dither, &ncolors) == FAILURE)  {
1588 		return;
1589 	}
1590 
1591 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1592 		RETURN_FALSE;
1593 	}
1594 
1595 	if (ncolors <= 0 || ZEND_LONG_INT_OVFL(ncolors)) {
1596 		php_error_docref(NULL, E_WARNING, "Number of colors has to be greater than zero and no more than %d", INT_MAX);
1597 		RETURN_FALSE;
1598 	}
1599 	if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) {
1600 		RETURN_TRUE;
1601 	} else {
1602 		php_error_docref(NULL, E_WARNING, "Couldn't convert to palette");
1603 		RETURN_FALSE;
1604 	}
1605 }
1606 /* }}} */
1607 
1608 /* {{{ proto void imagepalettetotruecolor(resource im)
1609    Convert a palette based image to a true color image. */
PHP_FUNCTION(imagepalettetotruecolor)1610 PHP_FUNCTION(imagepalettetotruecolor)
1611 {
1612 	zval *IM;
1613 	gdImagePtr im;
1614 
1615 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE)  {
1616 		return;
1617 	}
1618 
1619 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1620 		RETURN_FALSE;
1621 	}
1622 
1623 	if (gdImagePaletteToTrueColor(im) == 0) {
1624 		RETURN_FALSE;
1625 	}
1626 
1627 	RETURN_TRUE;
1628 }
1629 /* }}} */
1630 
1631 /* {{{ proto bool imagecolormatch(resource im1, resource im2)
1632    Makes the colors of the palette version of an image more closely match the true color version */
PHP_FUNCTION(imagecolormatch)1633 PHP_FUNCTION(imagecolormatch)
1634 {
1635 	zval *IM1, *IM2;
1636 	gdImagePtr im1, im2;
1637 	int result;
1638 
1639 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &IM1, &IM2) == FAILURE) {
1640 		return;
1641 	}
1642 
1643 	if ((im1 = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM1), "Image", le_gd)) == NULL) {
1644 		RETURN_FALSE;
1645 	}
1646 	if ((im2 = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM2), "Image", le_gd)) == NULL) {
1647 		RETURN_FALSE;
1648 	}
1649 
1650 	result = gdImageColorMatch(im1, im2);
1651 	switch (result) {
1652 		case -1:
1653 			php_error_docref(NULL, E_WARNING, "Image1 must be TrueColor" );
1654 			RETURN_FALSE;
1655 			break;
1656 		case -2:
1657 			php_error_docref(NULL, E_WARNING, "Image2 must be Palette" );
1658 			RETURN_FALSE;
1659 			break;
1660 		case -3:
1661 			php_error_docref(NULL, E_WARNING, "Image1 and Image2 must be the same size" );
1662 			RETURN_FALSE;
1663 			break;
1664 		case -4:
1665 			php_error_docref(NULL, E_WARNING, "Image2 must have at least one color" );
1666 			RETURN_FALSE;
1667 			break;
1668 	}
1669 
1670 	RETURN_TRUE;
1671 }
1672 /* }}} */
1673 
1674 /* {{{ proto bool imagesetthickness(resource im, int thickness)
1675    Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
PHP_FUNCTION(imagesetthickness)1676 PHP_FUNCTION(imagesetthickness)
1677 {
1678 	zval *IM;
1679 	zend_long thick;
1680 	gdImagePtr im;
1681 
1682 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &IM, &thick) == FAILURE) {
1683 		return;
1684 	}
1685 
1686 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1687 		RETURN_FALSE;
1688 	}
1689 
1690 	gdImageSetThickness(im, thick);
1691 
1692 	RETURN_TRUE;
1693 }
1694 /* }}} */
1695 
1696 /* {{{ proto bool imagefilledellipse(resource im, int cx, int cy, int w, int h, int color)
1697    Draw an ellipse */
PHP_FUNCTION(imagefilledellipse)1698 PHP_FUNCTION(imagefilledellipse)
1699 {
1700 	zval *IM;
1701 	zend_long cx, cy, w, h, color;
1702 	gdImagePtr im;
1703 
1704 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllll", &IM, &cx, &cy, &w, &h, &color) == FAILURE) {
1705 		return;
1706 	}
1707 
1708 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1709 		RETURN_FALSE;
1710 	}
1711 
1712 	gdImageFilledEllipse(im, cx, cy, w, h, color);
1713 
1714 	RETURN_TRUE;
1715 }
1716 /* }}} */
1717 
1718 /* {{{ proto bool imagefilledarc(resource im, int cx, int cy, int w, int h, int s, int e, int col, int style)
1719    Draw a filled partial ellipse */
PHP_FUNCTION(imagefilledarc)1720 PHP_FUNCTION(imagefilledarc)
1721 {
1722 	zval *IM;
1723 	zend_long cx, cy, w, h, ST, E, col, style;
1724 	gdImagePtr im;
1725 	int e, st;
1726 
1727 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllllllll", &IM, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
1728 		return;
1729 	}
1730 
1731 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1732 		RETURN_FALSE;
1733 	}
1734 
1735 	e = E;
1736 	if (e < 0) {
1737 		e %= 360;
1738 	}
1739 
1740 	st = ST;
1741 	if (st < 0) {
1742 		st %= 360;
1743 	}
1744 
1745 	gdImageFilledArc(im, cx, cy, w, h, st, e, col, style);
1746 
1747 	RETURN_TRUE;
1748 }
1749 /* }}} */
1750 
1751 /* {{{ proto bool imagealphablending(resource im, bool on)
1752    Turn alpha blending mode on or off for the given image */
PHP_FUNCTION(imagealphablending)1753 PHP_FUNCTION(imagealphablending)
1754 {
1755 	zval *IM;
1756 	zend_bool blend;
1757 	gdImagePtr im;
1758 
1759 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rb", &IM, &blend) == FAILURE) {
1760 		return;
1761 	}
1762 
1763 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1764 		RETURN_FALSE;
1765 	}
1766 
1767 	gdImageAlphaBlending(im, blend);
1768 
1769 	RETURN_TRUE;
1770 }
1771 /* }}} */
1772 
1773 /* {{{ proto bool imagesavealpha(resource im, bool on)
1774    Include alpha channel to a saved image */
PHP_FUNCTION(imagesavealpha)1775 PHP_FUNCTION(imagesavealpha)
1776 {
1777 	zval *IM;
1778 	zend_bool save;
1779 	gdImagePtr im;
1780 
1781 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rb", &IM, &save) == FAILURE) {
1782 		return;
1783 	}
1784 
1785 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1786 		RETURN_FALSE;
1787 	}
1788 
1789 	gdImageSaveAlpha(im, save);
1790 
1791 	RETURN_TRUE;
1792 }
1793 /* }}} */
1794 
1795 /* {{{ proto bool imagelayereffect(resource im, int effect)
1796    Set the alpha blending flag to use the bundled libgd layering effects */
PHP_FUNCTION(imagelayereffect)1797 PHP_FUNCTION(imagelayereffect)
1798 {
1799 	zval *IM;
1800 	zend_long effect;
1801 	gdImagePtr im;
1802 
1803 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &IM, &effect) == FAILURE) {
1804 		return;
1805 	}
1806 
1807 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1808 		RETURN_FALSE;
1809 	}
1810 
1811 	gdImageAlphaBlending(im, effect);
1812 
1813 	RETURN_TRUE;
1814 }
1815 /* }}} */
1816 
1817 /* {{{ proto int imagecolorallocatealpha(resource im, int red, int green, int blue, int alpha)
1818    Allocate a color with an alpha level.  Works for true color and palette based images */
PHP_FUNCTION(imagecolorallocatealpha)1819 PHP_FUNCTION(imagecolorallocatealpha)
1820 {
1821 	zval *IM;
1822 	zend_long red, green, blue, alpha;
1823 	gdImagePtr im;
1824 	int ct = (-1);
1825 
1826 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
1827 		RETURN_FALSE;
1828 	}
1829 
1830 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1831 		RETURN_FALSE;
1832 	}
1833 
1834 	ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
1835 	if (ct < 0) {
1836 		RETURN_FALSE;
1837 	}
1838 	RETURN_LONG((zend_long)ct);
1839 }
1840 /* }}} */
1841 
1842 /* {{{ proto int imagecolorresolvealpha(resource im, int red, int green, int blue, int alpha)
1843    Resolve/Allocate a colour with an alpha level.  Works for true colour and palette based images */
PHP_FUNCTION(imagecolorresolvealpha)1844 PHP_FUNCTION(imagecolorresolvealpha)
1845 {
1846 	zval *IM;
1847 	zend_long red, green, blue, alpha;
1848 	gdImagePtr im;
1849 
1850 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
1851 		return;
1852 	}
1853 
1854 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1855 		RETURN_FALSE;
1856 	}
1857 
1858 	RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha));
1859 }
1860 /* }}} */
1861 
1862 /* {{{ proto int imagecolorclosestalpha(resource im, int red, int green, int blue, int alpha)
1863    Find the closest matching colour with alpha transparency */
PHP_FUNCTION(imagecolorclosestalpha)1864 PHP_FUNCTION(imagecolorclosestalpha)
1865 {
1866 	zval *IM;
1867 	zend_long red, green, blue, alpha;
1868 	gdImagePtr im;
1869 
1870 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
1871 		return;
1872 	}
1873 
1874 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1875 		RETURN_FALSE;
1876 	}
1877 
1878 	RETURN_LONG(gdImageColorClosestAlpha(im, red, green, blue, alpha));
1879 }
1880 /* }}} */
1881 
1882 /* {{{ proto int imagecolorexactalpha(resource im, int red, int green, int blue, int alpha)
1883    Find exact match for colour with transparency */
PHP_FUNCTION(imagecolorexactalpha)1884 PHP_FUNCTION(imagecolorexactalpha)
1885 {
1886 	zval *IM;
1887 	zend_long red, green, blue, alpha;
1888 	gdImagePtr im;
1889 
1890 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
1891 		return;
1892 	}
1893 
1894 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
1895 		RETURN_FALSE;
1896 	}
1897 
1898 	RETURN_LONG(gdImageColorExactAlpha(im, red, green, blue, alpha));
1899 }
1900 /* }}} */
1901 
1902 /* {{{ proto bool imagecopyresampled(resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h)
1903    Copy and resize part of an image using resampling to help ensure clarity */
PHP_FUNCTION(imagecopyresampled)1904 PHP_FUNCTION(imagecopyresampled)
1905 {
1906 	zval *SIM, *DIM;
1907 	zend_long SX, SY, SW, SH, DX, DY, DW, DH;
1908 	gdImagePtr im_dst, im_src;
1909 	int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
1910 
1911 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrllllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
1912 		return;
1913 	}
1914 
1915 	if ((im_dst = (gdImagePtr)zend_fetch_resource(Z_RES_P(DIM), "Image", le_gd)) == NULL) {
1916 		RETURN_FALSE;
1917 	}
1918 
1919 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
1920 		RETURN_FALSE;
1921 	}
1922 
1923 	srcX = SX;
1924 	srcY = SY;
1925 	srcH = SH;
1926 	srcW = SW;
1927 	dstX = DX;
1928 	dstY = DY;
1929 	dstH = DH;
1930 	dstW = DW;
1931 
1932 	gdImageCopyResampled(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
1933 
1934 	RETURN_TRUE;
1935 }
1936 /* }}} */
1937 
1938 #ifdef PHP_WIN32
1939 /* {{{ proto resource imagegrabwindow(int window_handle [, int client_area])
1940    Grab a window or its client area using a windows handle (HWND property in COM instance) */
PHP_FUNCTION(imagegrabwindow)1941 PHP_FUNCTION(imagegrabwindow)
1942 {
1943 	HWND window;
1944 	zend_long client_area = 0;
1945 	RECT rc = {0};
1946 	RECT rc_win = {0};
1947 	int Width, Height;
1948 	HDC		hdc;
1949 	HDC memDC;
1950 	HBITMAP memBM;
1951 	HBITMAP hOld;
1952 	zend_long lwindow_handle;
1953 	gdImagePtr im = NULL;
1954 
1955 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &lwindow_handle, &client_area) == FAILURE) {
1956 		RETURN_FALSE;
1957 	}
1958 
1959 	window = (HWND) lwindow_handle;
1960 
1961 	if (!IsWindow(window)) {
1962 		php_error_docref(NULL, E_NOTICE, "Invalid window handle");
1963 		RETURN_FALSE;
1964 	}
1965 
1966 	hdc		= GetDC(0);
1967 
1968 	if (client_area) {
1969 		GetClientRect(window, &rc);
1970 		Width = rc.right;
1971 		Height = rc.bottom;
1972 	} else {
1973 		GetWindowRect(window, &rc);
1974 		Width	= rc.right - rc.left;
1975 		Height	= rc.bottom - rc.top;
1976 	}
1977 
1978 	Width		= (Width/4)*4;
1979 
1980 	memDC	= CreateCompatibleDC(hdc);
1981 	memBM	= CreateCompatibleBitmap(hdc, Width, Height);
1982 	hOld	= (HBITMAP) SelectObject (memDC, memBM);
1983 
1984 	PrintWindow(window, memDC, (UINT) client_area);
1985 
1986 	im = gdImageCreateTrueColor(Width, Height);
1987 	if (im) {
1988 		int x,y;
1989 		for (y=0; y <= Height; y++) {
1990 			for (x=0; x <= Width; x++) {
1991 				int c = GetPixel(memDC, x,y);
1992 				gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
1993 			}
1994 		}
1995 	}
1996 
1997 	SelectObject(memDC,hOld);
1998 	DeleteObject(memBM);
1999 	DeleteDC(memDC);
2000 	ReleaseDC( 0, hdc );
2001 
2002 	if (!im) {
2003 		RETURN_FALSE;
2004 	} else {
2005 		RETURN_RES(zend_register_resource(im, le_gd));
2006 	}
2007 }
2008 /* }}} */
2009 
2010 /* {{{ proto resource imagegrabscreen()
2011    Grab a screenshot */
PHP_FUNCTION(imagegrabscreen)2012 PHP_FUNCTION(imagegrabscreen)
2013 {
2014 	HWND window = GetDesktopWindow();
2015 	RECT rc = {0};
2016 	int Width, Height;
2017 	HDC		hdc;
2018 	HDC memDC;
2019 	HBITMAP memBM;
2020 	HBITMAP hOld;
2021 	typedef BOOL (WINAPI *tPrintWindow)(HWND, HDC,UINT);
2022 	tPrintWindow pPrintWindow = 0;
2023 	gdImagePtr im;
2024 	hdc		= GetDC(0);
2025 
2026 	if (zend_parse_parameters_none() == FAILURE) {
2027 		return;
2028 	}
2029 
2030 	if (!hdc) {
2031 		RETURN_FALSE;
2032 	}
2033 
2034 	GetWindowRect(window, &rc);
2035 	Width	= rc.right - rc.left;
2036 	Height	= rc.bottom - rc.top;
2037 
2038 	Width		= (Width/4)*4;
2039 
2040 	memDC	= CreateCompatibleDC(hdc);
2041 	memBM	= CreateCompatibleBitmap(hdc, Width, Height);
2042 	hOld	= (HBITMAP) SelectObject (memDC, memBM);
2043 	BitBlt( memDC, 0, 0, Width, Height , hdc, rc.left, rc.top , SRCCOPY );
2044 
2045 	im = gdImageCreateTrueColor(Width, Height);
2046 	if (im) {
2047 		int x,y;
2048 		for (y=0; y <= Height; y++) {
2049 			for (x=0; x <= Width; x++) {
2050 				int c = GetPixel(memDC, x,y);
2051 				gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
2052 			}
2053 		}
2054 	}
2055 
2056 	SelectObject(memDC,hOld);
2057 	DeleteObject(memBM);
2058 	DeleteDC(memDC);
2059 	ReleaseDC( 0, hdc );
2060 
2061 	if (!im) {
2062 		RETURN_FALSE;
2063 	} else {
2064 		RETURN_RES(zend_register_resource(im, le_gd));
2065 	}
2066 }
2067 /* }}} */
2068 #endif /* PHP_WIN32 */
2069 
2070 /* {{{ proto resource imagerotate(resource src_im, float angle, int bgdcolor [, int ignoretransparent])
2071    Rotate an image using a custom angle */
PHP_FUNCTION(imagerotate)2072 PHP_FUNCTION(imagerotate)
2073 {
2074 	zval *SIM;
2075 	gdImagePtr im_dst, im_src;
2076 	double degrees;
2077 	zend_long color;
2078 	zend_long ignoretransparent = 0;
2079 
2080 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rdl|l", &SIM, &degrees, &color, &ignoretransparent) == FAILURE) {
2081 		RETURN_FALSE;
2082 	}
2083 
2084 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
2085 		RETURN_FALSE;
2086 	}
2087 
2088 	im_dst = gdImageRotateInterpolated(im_src, (const float)degrees, color);
2089 
2090 	if (im_dst != NULL) {
2091 		RETURN_RES(zend_register_resource(im_dst, le_gd));
2092 	} else {
2093 		RETURN_FALSE;
2094 	}
2095 }
2096 /* }}} */
2097 
2098 /* {{{ proto bool imagesettile(resource image, resource tile)
2099    Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color */
PHP_FUNCTION(imagesettile)2100 PHP_FUNCTION(imagesettile)
2101 {
2102 	zval *IM, *TILE;
2103 	gdImagePtr im, tile;
2104 
2105 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &IM, &TILE) == FAILURE) {
2106 		return;
2107 	}
2108 
2109 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2110 		RETURN_FALSE;
2111 	}
2112 
2113 	if ((tile = (gdImagePtr)zend_fetch_resource(Z_RES_P(TILE), "Image", le_gd)) == NULL) {
2114 		RETURN_FALSE;
2115 	}
2116 
2117 	gdImageSetTile(im, tile);
2118 
2119 	RETURN_TRUE;
2120 }
2121 /* }}} */
2122 
2123 /* {{{ proto bool imagesetbrush(resource image, resource brush)
2124    Set the brush image to $brush when filling $image with the "IMG_COLOR_BRUSHED" color */
PHP_FUNCTION(imagesetbrush)2125 PHP_FUNCTION(imagesetbrush)
2126 {
2127 	zval *IM, *TILE;
2128 	gdImagePtr im, tile;
2129 
2130 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &IM, &TILE) == FAILURE) {
2131 		return;
2132 	}
2133 
2134 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2135 		RETURN_FALSE;
2136 	}
2137 
2138 	if ((tile = (gdImagePtr)zend_fetch_resource(Z_RES_P(TILE), "Image", le_gd)) == NULL) {
2139 		RETURN_FALSE;
2140 	}
2141 
2142 	gdImageSetBrush(im, tile);
2143 
2144 	RETURN_TRUE;
2145 }
2146 /* }}} */
2147 
2148 /* {{{ proto resource imagecreate(int x_size, int y_size)
2149    Create a new image */
PHP_FUNCTION(imagecreate)2150 PHP_FUNCTION(imagecreate)
2151 {
2152 	zend_long x_size, y_size;
2153 	gdImagePtr im;
2154 
2155 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
2156 		return;
2157 	}
2158 
2159 	if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) {
2160 		php_error_docref(NULL, E_WARNING, "Invalid image dimensions");
2161 		RETURN_FALSE;
2162 	}
2163 
2164 	im = gdImageCreate(x_size, y_size);
2165 
2166 	if (!im) {
2167 		RETURN_FALSE;
2168 	}
2169 
2170 	RETURN_RES(zend_register_resource(im, le_gd));
2171 }
2172 /* }}} */
2173 
2174 /* {{{ proto int imagetypes(void)
2175    Return the types of images supported in a bitfield - 1=GIF, 2=JPEG, 4=PNG, 8=WBMP, 16=XPM */
PHP_FUNCTION(imagetypes)2176 PHP_FUNCTION(imagetypes)
2177 {
2178 	int ret=0;
2179 	ret = 1;
2180 #ifdef HAVE_GD_JPG
2181 	ret |= 2;
2182 #endif
2183 #ifdef HAVE_GD_PNG
2184 	ret |= 4;
2185 #endif
2186 	ret |= 8;
2187 #if defined(HAVE_GD_XPM)
2188 	ret |= 16;
2189 #endif
2190 #ifdef HAVE_GD_WEBP
2191 	ret |= 32;
2192 #endif
2193 #ifdef HAVE_GD_BMP
2194 	ret |= 64;
2195 #endif
2196 
2197 	if (zend_parse_parameters_none() == FAILURE) {
2198 		return;
2199 	}
2200 
2201 	RETURN_LONG(ret);
2202 }
2203 /* }}} */
2204 
2205 /* {{{ _php_ctx_getmbi
2206  */
2207 
_php_ctx_getmbi(gdIOCtx * ctx)2208 static int _php_ctx_getmbi(gdIOCtx *ctx)
2209 {
2210 	int i, mbi = 0;
2211 
2212 	do {
2213 		i = (ctx->getC)(ctx);
2214 		if (i < 0) {
2215 			return -1;
2216 		}
2217 		mbi = (mbi << 7) | (i & 0x7f);
2218 	} while (i & 0x80);
2219 
2220 	return mbi;
2221 }
2222 /* }}} */
2223 
2224 /* {{{ _php_image_type
2225  */
2226 static const char php_sig_gd2[3] = {'g', 'd', '2'};
2227 
_php_image_type(char data[8])2228 static int _php_image_type (char data[8])
2229 {
2230 	/* Based on ext/standard/image.c */
2231 
2232 	if (data == NULL) {
2233 		return -1;
2234 	}
2235 
2236 	if (!memcmp(data, php_sig_gd2, 3)) {
2237 		return PHP_GDIMG_TYPE_GD2;
2238 	} else if (!memcmp(data, php_sig_jpg, 3)) {
2239 		return PHP_GDIMG_TYPE_JPG;
2240 	} else if (!memcmp(data, php_sig_png, 3)) {
2241 		if (!memcmp(data, php_sig_png, 8)) {
2242 			return PHP_GDIMG_TYPE_PNG;
2243 		}
2244 	} else if (!memcmp(data, php_sig_gif, 3)) {
2245 		return PHP_GDIMG_TYPE_GIF;
2246 	} else if (!memcmp(data, php_sig_bmp, sizeof(php_sig_bmp))) {
2247 		return PHP_GDIMG_TYPE_BMP;
2248 	}
2249 	else {
2250 		gdIOCtx *io_ctx;
2251 		io_ctx = gdNewDynamicCtxEx(8, data, 0);
2252 		if (io_ctx) {
2253 			if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
2254 				io_ctx->gd_free(io_ctx);
2255 				return PHP_GDIMG_TYPE_WBM;
2256 			} else {
2257 				io_ctx->gd_free(io_ctx);
2258 			}
2259 		}
2260 	}
2261 	return -1;
2262 }
2263 /* }}} */
2264 
2265 /* {{{ _php_image_create_from_string
2266  */
_php_image_create_from_string(zval * data,char * tn,gdImagePtr (* ioctx_func_p)())2267 gdImagePtr _php_image_create_from_string(zval *data, char *tn, gdImagePtr (*ioctx_func_p)())
2268 {
2269 	gdImagePtr im;
2270 	gdIOCtx *io_ctx;
2271 
2272 	io_ctx = gdNewDynamicCtxEx(Z_STRLEN_P(data), Z_STRVAL_P(data), 0);
2273 
2274 	if (!io_ctx) {
2275 		return NULL;
2276 	}
2277 
2278 	im = (*ioctx_func_p)(io_ctx);
2279 	if (!im) {
2280 		php_error_docref(NULL, E_WARNING, "Passed data is not in '%s' format", tn);
2281 		io_ctx->gd_free(io_ctx);
2282 		return NULL;
2283 	}
2284 
2285 	io_ctx->gd_free(io_ctx);
2286 
2287 	return im;
2288 }
2289 /* }}} */
2290 
2291 /* {{{ proto resource imagecreatefromstring(string image)
2292    Create a new image from the image stream in the string */
PHP_FUNCTION(imagecreatefromstring)2293 PHP_FUNCTION(imagecreatefromstring)
2294 {
2295 	zval *data;
2296 	gdImagePtr im;
2297 	int imtype;
2298 	char sig[8];
2299 
2300 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &data) == FAILURE) {
2301 		return;
2302 	}
2303 
2304 	convert_to_string_ex(data);
2305 	if (Z_STRLEN_P(data) < 8) {
2306 		php_error_docref(NULL, E_WARNING, "Empty string or invalid image");
2307 		RETURN_FALSE;
2308 	}
2309 
2310 	memcpy(sig, Z_STRVAL_P(data), 8);
2311 
2312 	imtype = _php_image_type(sig);
2313 
2314 	switch (imtype) {
2315 		case PHP_GDIMG_TYPE_JPG:
2316 #ifdef HAVE_GD_JPG
2317 			im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx);
2318 #else
2319 			php_error_docref(NULL, E_WARNING, "No JPEG support in this PHP build");
2320 			RETURN_FALSE;
2321 #endif
2322 			break;
2323 
2324 		case PHP_GDIMG_TYPE_PNG:
2325 #ifdef HAVE_GD_PNG
2326 			im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx);
2327 #else
2328 			php_error_docref(NULL, E_WARNING, "No PNG support in this PHP build");
2329 			RETURN_FALSE;
2330 #endif
2331 			break;
2332 
2333 		case PHP_GDIMG_TYPE_GIF:
2334 			im = _php_image_create_from_string(data, "GIF", gdImageCreateFromGifCtx);
2335 			break;
2336 
2337 		case PHP_GDIMG_TYPE_WBM:
2338 			im = _php_image_create_from_string(data, "WBMP", gdImageCreateFromWBMPCtx);
2339 			break;
2340 
2341 		case PHP_GDIMG_TYPE_GD2:
2342 			im = _php_image_create_from_string(data, "GD2", gdImageCreateFromGd2Ctx);
2343 			break;
2344 
2345 		case PHP_GDIMG_TYPE_BMP:
2346 			im = _php_image_create_from_string(data, "BMP", gdImageCreateFromBmpCtx);
2347 			break;
2348 
2349 		default:
2350 			php_error_docref(NULL, E_WARNING, "Data is not in a recognized format");
2351 			RETURN_FALSE;
2352 	}
2353 
2354 	if (!im) {
2355 		php_error_docref(NULL, E_WARNING, "Couldn't create GD Image Stream out of Data");
2356 		RETURN_FALSE;
2357 	}
2358 
2359 	RETURN_RES(zend_register_resource(im, le_gd));
2360 }
2361 /* }}} */
2362 
2363 /* {{{ _php_image_create_from
2364  */
_php_image_create_from(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn,gdImagePtr (* func_p)(),gdImagePtr (* ioctx_func_p)())2365 static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)())
2366 {
2367 	char *file;
2368 	size_t file_len;
2369 	zend_long srcx, srcy, width, height;
2370 	gdImagePtr im = NULL;
2371 	php_stream *stream;
2372 	FILE * fp = NULL;
2373 #ifdef HAVE_GD_JPG
2374 	long ignore_warning;
2375 #endif
2376 
2377 	if (image_type == PHP_GDIMG_TYPE_GD2PART) {
2378 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "pllll", &file, &file_len, &srcx, &srcy, &width, &height) == FAILURE) {
2379 			return;
2380 		}
2381 		if (width < 1 || height < 1) {
2382 			php_error_docref(NULL, E_WARNING, "Zero width or height not allowed");
2383 			RETURN_FALSE;
2384 		}
2385 	} else {
2386 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &file, &file_len) == FAILURE) {
2387 			return;
2388 		}
2389 	}
2390 
2391 
2392 	stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
2393 	if (stream == NULL)	{
2394 		RETURN_FALSE;
2395 	}
2396 
2397 	/* try and avoid allocating a FILE* if the stream is not naturally a FILE* */
2398 	if (php_stream_is(stream, PHP_STREAM_IS_STDIO))	{
2399 		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
2400 			goto out_err;
2401 		}
2402 	} else if (ioctx_func_p) {
2403 		/* we can create an io context */
2404 		gdIOCtx* io_ctx;
2405 		zend_string *buff;
2406 		char *pstr;
2407 
2408 		buff = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
2409 
2410 		if (!buff) {
2411 			php_error_docref(NULL, E_WARNING,"Cannot read image data");
2412 			goto out_err;
2413 		}
2414 
2415 		/* needs to be malloc (persistent) - GD will free() it later */
2416 		pstr = pestrndup(ZSTR_VAL(buff), ZSTR_LEN(buff), 1);
2417 		io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(buff), pstr, 0);
2418 		if (!io_ctx) {
2419 			pefree(pstr, 1);
2420 			zend_string_release(buff);
2421 			php_error_docref(NULL, E_WARNING,"Cannot allocate GD IO context");
2422 			goto out_err;
2423 		}
2424 
2425 		if (image_type == PHP_GDIMG_TYPE_GD2PART) {
2426 			im = (*ioctx_func_p)(io_ctx, srcx, srcy, width, height);
2427 		} else {
2428 			im = (*ioctx_func_p)(io_ctx);
2429 		}
2430 		io_ctx->gd_free(io_ctx);
2431 		pefree(pstr, 1);
2432 		zend_string_release(buff);
2433 	}
2434 	else if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO)) {
2435 		/* try and force the stream to be FILE* */
2436 		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {
2437 			goto out_err;
2438 		}
2439 	}
2440 
2441 	if (!im && fp) {
2442 		switch (image_type) {
2443 			case PHP_GDIMG_TYPE_GD2PART:
2444 				im = (*func_p)(fp, srcx, srcy, width, height);
2445 				break;
2446 #if defined(HAVE_GD_XPM)
2447 			case PHP_GDIMG_TYPE_XPM:
2448 				im = gdImageCreateFromXpm(file);
2449 				break;
2450 #endif
2451 
2452 #ifdef HAVE_GD_JPG
2453 			case PHP_GDIMG_TYPE_JPG:
2454 				ignore_warning = INI_INT("gd.jpeg_ignore_warning");
2455 				im = gdImageCreateFromJpegEx(fp, ignore_warning);
2456 			break;
2457 #endif
2458 
2459 			default:
2460 				im = (*func_p)(fp);
2461 				break;
2462 		}
2463 
2464 		fflush(fp);
2465 	}
2466 
2467 /* register_im: */
2468 	if (im) {
2469 		RETVAL_RES(zend_register_resource(im, le_gd));
2470 		php_stream_close(stream);
2471 		return;
2472 	}
2473 
2474 	php_error_docref(NULL, E_WARNING, "'%s' is not a valid %s file", file, tn);
2475 out_err:
2476 	php_stream_close(stream);
2477 	RETURN_FALSE;
2478 
2479 }
2480 /* }}} */
2481 
2482 /* {{{ proto resource imagecreatefromgif(string filename)
2483    Create a new image from GIF file or URL */
PHP_FUNCTION(imagecreatefromgif)2484 PHP_FUNCTION(imagecreatefromgif)
2485 {
2486 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
2487 }
2488 /* }}} */
2489 
2490 #ifdef HAVE_GD_JPG
2491 /* {{{ proto resource imagecreatefromjpeg(string filename)
2492    Create a new image from JPEG file or URL */
PHP_FUNCTION(imagecreatefromjpeg)2493 PHP_FUNCTION(imagecreatefromjpeg)
2494 {
2495 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
2496 }
2497 /* }}} */
2498 #endif /* HAVE_GD_JPG */
2499 
2500 #ifdef HAVE_GD_PNG
2501 /* {{{ proto resource imagecreatefrompng(string filename)
2502    Create a new image from PNG file or URL */
PHP_FUNCTION(imagecreatefrompng)2503 PHP_FUNCTION(imagecreatefrompng)
2504 {
2505 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
2506 }
2507 /* }}} */
2508 #endif /* HAVE_GD_PNG */
2509 
2510 #ifdef HAVE_GD_WEBP
2511 /* {{{ proto resource imagecreatefromwebp(string filename)
2512    Create a new image from WEBP file or URL */
PHP_FUNCTION(imagecreatefromwebp)2513 PHP_FUNCTION(imagecreatefromwebp)
2514 {
2515 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageCreateFromWebp, gdImageCreateFromWebpCtx);
2516 }
2517 /* }}} */
2518 #endif /* HAVE_GD_WEBP */
2519 
2520 /* {{{ proto resource imagecreatefromxbm(string filename)
2521    Create a new image from XBM file or URL */
PHP_FUNCTION(imagecreatefromxbm)2522 PHP_FUNCTION(imagecreatefromxbm)
2523 {
2524 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);
2525 }
2526 /* }}} */
2527 
2528 #if defined(HAVE_GD_XPM)
2529 /* {{{ proto resource imagecreatefromxpm(string filename)
2530    Create a new image from XPM file or URL */
PHP_FUNCTION(imagecreatefromxpm)2531 PHP_FUNCTION(imagecreatefromxpm)
2532 {
2533 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", gdImageCreateFromXpm, NULL);
2534 }
2535 /* }}} */
2536 #endif
2537 
2538 /* {{{ proto resource imagecreatefromwbmp(string filename)
2539    Create a new image from WBMP file or URL */
PHP_FUNCTION(imagecreatefromwbmp)2540 PHP_FUNCTION(imagecreatefromwbmp)
2541 {
2542 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);
2543 }
2544 /* }}} */
2545 
2546 /* {{{ proto resource imagecreatefromgd(string filename)
2547    Create a new image from GD file or URL */
PHP_FUNCTION(imagecreatefromgd)2548 PHP_FUNCTION(imagecreatefromgd)
2549 {
2550 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);
2551 }
2552 /* }}} */
2553 
2554 /* {{{ proto resource imagecreatefromgd2(string filename)
2555    Create a new image from GD2 file or URL */
PHP_FUNCTION(imagecreatefromgd2)2556 PHP_FUNCTION(imagecreatefromgd2)
2557 {
2558 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);
2559 }
2560 /* }}} */
2561 
2562 /* {{{ proto resource imagecreatefromgd2part(string filename, int srcX, int srcY, int width, int height)
2563    Create a new image from a given part of GD2 file or URL */
PHP_FUNCTION(imagecreatefromgd2part)2564 PHP_FUNCTION(imagecreatefromgd2part)
2565 {
2566 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", gdImageCreateFromGd2Part, gdImageCreateFromGd2PartCtx);
2567 }
2568 /* }}} */
2569 
2570 #if defined(HAVE_GD_BMP)
2571 /* {{{ proto resource imagecreatefrombmp(string filename)
2572    Create a new image from BMP file or URL */
PHP_FUNCTION(imagecreatefrombmp)2573 PHP_FUNCTION(imagecreatefrombmp)
2574 {
2575 	_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_BMP, "BMP", gdImageCreateFromBmp, gdImageCreateFromBmpCtx);
2576 }
2577 /* }}} */
2578 #endif
2579 
2580 /* {{{ _php_image_output
2581  */
_php_image_output(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn,void (* func_p)())2582 static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
2583 {
2584 	zval *imgind;
2585 	char *file = NULL;
2586 	zend_long quality = 0, type = 0;
2587 	gdImagePtr im;
2588 	char *fn = NULL;
2589 	FILE *fp;
2590 	size_t file_len = 0;
2591 	int argc = ZEND_NUM_ARGS();
2592 	int q = -1, i, t = 1;
2593 
2594 	/* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */
2595 	/* When called from imagewbmp() the quality parameter stands for the foreground color. Default: black. */
2596 	/* The quality parameter for gd2 stands for chunk size */
2597 
2598 	if (zend_parse_parameters(argc, "r|pll", &imgind, &file, &file_len, &quality, &type) == FAILURE) {
2599 		return;
2600 	}
2601 
2602 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", le_gd)) == NULL) {
2603 		RETURN_FALSE;
2604 	}
2605 
2606 	if (argc > 1) {
2607 		fn = file;
2608 		if (argc >= 3) {
2609 			q = quality;
2610 			if (argc == 4) {
2611 				t = type;
2612 			}
2613 		}
2614 	}
2615 
2616 	if (argc >= 2 && file_len) {
2617 		PHP_GD_CHECK_OPEN_BASEDIR(fn, "Invalid filename");
2618 
2619 		fp = VCWD_FOPEN(fn, "wb");
2620 		if (!fp) {
2621 			php_error_docref(NULL, E_WARNING, "Unable to open '%s' for writing", fn);
2622 			RETURN_FALSE;
2623 		}
2624 
2625 		switch (image_type) {
2626 			case PHP_GDIMG_CONVERT_WBM:
2627 				if (q == -1) {
2628 					q = 0;
2629 				} else if (q < 0 || q > 255) {
2630 					php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
2631 					q = 0;
2632 				}
2633 				gdImageWBMP(im, q, fp);
2634 				break;
2635 			case PHP_GDIMG_TYPE_JPG:
2636 				(*func_p)(im, fp, q);
2637 				break;
2638 			case PHP_GDIMG_TYPE_WBM:
2639 				for (i = 0; i < gdImageColorsTotal(im); i++) {
2640 					if (gdImageRed(im, i) == 0) break;
2641 				}
2642 				(*func_p)(im, i, fp);
2643 				break;
2644 			case PHP_GDIMG_TYPE_GD:
2645 				(*func_p)(im, fp);
2646 				break;
2647 			case PHP_GDIMG_TYPE_GD2:
2648 				if (q == -1) {
2649 					q = 128;
2650 				}
2651 				(*func_p)(im, fp, q, t);
2652 				break;
2653 			default:
2654 				if (q == -1) {
2655 					q = 128;
2656 				}
2657 				(*func_p)(im, fp, q, t);
2658 				break;
2659 		}
2660 		fflush(fp);
2661 		fclose(fp);
2662 	} else {
2663 		int   b;
2664 		FILE *tmp;
2665 		char  buf[4096];
2666 		zend_string *path;
2667 
2668 		tmp = php_open_temporary_file(NULL, NULL, &path);
2669 		if (tmp == NULL) {
2670 			php_error_docref(NULL, E_WARNING, "Unable to open temporary file");
2671 			RETURN_FALSE;
2672 		}
2673 
2674 		switch (image_type) {
2675 			case PHP_GDIMG_CONVERT_WBM:
2676  				if (q == -1) {
2677   					q = 0;
2678   				} else if (q < 0 || q > 255) {
2679   					php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
2680  					q = 0;
2681   				}
2682 				gdImageWBMP(im, q, tmp);
2683 				break;
2684 			case PHP_GDIMG_TYPE_JPG:
2685 				(*func_p)(im, tmp, q);
2686 				break;
2687 			case PHP_GDIMG_TYPE_WBM:
2688 				for (i = 0; i < gdImageColorsTotal(im); i++) {
2689 					if (gdImageRed(im, i) == 0) {
2690 						break;
2691 					}
2692 				}
2693 				(*func_p)(im, q, tmp);
2694 				break;
2695 			case PHP_GDIMG_TYPE_GD:
2696 				(*func_p)(im, tmp);
2697 				break;
2698 			case PHP_GDIMG_TYPE_GD2:
2699 				if (q == -1) {
2700 					q = 128;
2701 				}
2702 				(*func_p)(im, tmp, q, t);
2703 				break;
2704 			default:
2705 				(*func_p)(im, tmp);
2706 				break;
2707 		}
2708 
2709 		fseek(tmp, 0, SEEK_SET);
2710 
2711 #if APACHE && defined(CHARSET_EBCDIC)
2712 		/* XXX this is unlikely to work any more thies@thieso.net */
2713 
2714 		/* This is a binary file already: avoid EBCDIC->ASCII conversion */
2715 		ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
2716 #endif
2717 		while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
2718 			php_write(buf, b);
2719 		}
2720 
2721 		fclose(tmp);
2722 		VCWD_UNLINK((const char *)ZSTR_VAL(path)); /* make sure that the temporary file is removed */
2723 		zend_string_release(path);
2724 	}
2725 	RETURN_TRUE;
2726 }
2727 /* }}} */
2728 
2729 /* {{{ proto int imagexbm(int im, string filename [, int foreground])
2730    Output XBM image to browser or file */
PHP_FUNCTION(imagexbm)2731 PHP_FUNCTION(imagexbm)
2732 {
2733 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageXbmCtx);
2734 }
2735 /* }}} */
2736 
2737 /* {{{ proto bool imagegif(resource im [, mixed to])
2738    Output GIF image to browser or file */
PHP_FUNCTION(imagegif)2739 PHP_FUNCTION(imagegif)
2740 {
2741 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageGifCtx);
2742 }
2743 /* }}} */
2744 
2745 #ifdef HAVE_GD_PNG
2746 /* {{{ proto bool imagepng(resource im [, mixed to])
2747    Output PNG image to browser or file */
PHP_FUNCTION(imagepng)2748 PHP_FUNCTION(imagepng)
2749 {
2750 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePngCtxEx);
2751 }
2752 /* }}} */
2753 #endif /* HAVE_GD_PNG */
2754 
2755 
2756 #ifdef HAVE_GD_WEBP
2757 /* {{{ proto bool imagewebp(resource im [, mixed to[, int quality]] )
2758    Output WEBP image to browser or file */
PHP_FUNCTION(imagewebp)2759 PHP_FUNCTION(imagewebp)
2760 {
2761 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageWebpCtx);
2762 }
2763 /* }}} */
2764 #endif /* HAVE_GD_WEBP */
2765 
2766 
2767 #ifdef HAVE_GD_JPG
2768 /* {{{ proto bool imagejpeg(resource im [, mixed to [, int quality]])
2769    Output JPEG image to browser or file */
PHP_FUNCTION(imagejpeg)2770 PHP_FUNCTION(imagejpeg)
2771 {
2772 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageJpegCtx);
2773 }
2774 /* }}} */
2775 #endif /* HAVE_GD_JPG */
2776 
2777 /* {{{ proto bool imagewbmp(resource im [, mixed to [, int foreground]])
2778    Output WBMP image to browser or file */
PHP_FUNCTION(imagewbmp)2779 PHP_FUNCTION(imagewbmp)
2780 {
2781 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageWBMPCtx);
2782 }
2783 /* }}} */
2784 
2785 /* {{{ proto bool imagegd(resource im [, mixed to])
2786    Output GD image to browser or file */
PHP_FUNCTION(imagegd)2787 PHP_FUNCTION(imagegd)
2788 {
2789 	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageGd);
2790 }
2791 /* }}} */
2792 
2793 /* {{{ proto bool imagegd2(resource im [, mixed to [, int chunk_size [, int type]]])
2794    Output GD2 image to browser or file */
PHP_FUNCTION(imagegd2)2795 PHP_FUNCTION(imagegd2)
2796 {
2797 	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageGd2);
2798 }
2799 /* }}} */
2800 
2801 #ifdef HAVE_GD_BMP
2802 /* {{{ proto bool imagebmp(resource im [, mixed to [, bool compressed]])
2803    Output BMP image to browser or file */
PHP_FUNCTION(imagebmp)2804 PHP_FUNCTION(imagebmp)
2805 {
2806 	_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_BMP, "BMP", gdImageBmpCtx);
2807 }
2808 /* }}} */
2809 #endif
2810 
2811 /* {{{ proto bool imagedestroy(resource im)
2812    Destroy an image */
PHP_FUNCTION(imagedestroy)2813 PHP_FUNCTION(imagedestroy)
2814 {
2815 	zval *IM;
2816 	gdImagePtr im;
2817 
2818 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
2819 		return;
2820 	}
2821 
2822 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2823 		RETURN_FALSE;
2824 	}
2825 
2826 	zend_list_close(Z_RES_P(IM));
2827 
2828 	RETURN_TRUE;
2829 }
2830 /* }}} */
2831 
2832 
2833 /* {{{ proto int imagecolorallocate(resource im, int red, int green, int blue)
2834    Allocate a color for an image */
PHP_FUNCTION(imagecolorallocate)2835 PHP_FUNCTION(imagecolorallocate)
2836 {
2837 	zval *IM;
2838 	zend_long red, green, blue;
2839 	gdImagePtr im;
2840 	int ct = (-1);
2841 
2842 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &IM, &red, &green, &blue) == FAILURE) {
2843 		return;
2844 	}
2845 
2846 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2847 		RETURN_FALSE;
2848 	}
2849 
2850 	ct = gdImageColorAllocate(im, red, green, blue);
2851 	if (ct < 0) {
2852 		RETURN_FALSE;
2853 	}
2854 	RETURN_LONG(ct);
2855 }
2856 /* }}} */
2857 
2858 /* {{{ proto void imagepalettecopy(resource dst, resource src)
2859    Copy the palette from the src image onto the dst image */
PHP_FUNCTION(imagepalettecopy)2860 PHP_FUNCTION(imagepalettecopy)
2861 {
2862 	zval *dstim, *srcim;
2863 	gdImagePtr dst, src;
2864 
2865 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &dstim, &srcim) == FAILURE) {
2866 		return;
2867 	}
2868 
2869 	if ((dst = (gdImagePtr)zend_fetch_resource(Z_RES_P(dstim), "Image", le_gd)) == NULL) {
2870 		RETURN_FALSE;
2871 	}
2872 
2873 	if ((src = (gdImagePtr)zend_fetch_resource(Z_RES_P(srcim), "Image", le_gd)) == NULL) {
2874 		RETURN_FALSE;
2875 	}
2876 
2877 	gdImagePaletteCopy(dst, src);
2878 }
2879 /* }}} */
2880 
2881 /* {{{ proto int imagecolorat(resource im, int x, int y)
2882    Get the index of the color of a pixel */
PHP_FUNCTION(imagecolorat)2883 PHP_FUNCTION(imagecolorat)
2884 {
2885 	zval *IM;
2886 	zend_long x, y;
2887 	gdImagePtr im;
2888 
2889 	ZEND_PARSE_PARAMETERS_START(3, 3)
2890 		Z_PARAM_RESOURCE(IM)
2891 		Z_PARAM_LONG(x)
2892 		Z_PARAM_LONG(y)
2893 	ZEND_PARSE_PARAMETERS_END();
2894 
2895 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2896 		RETURN_FALSE;
2897 	}
2898 
2899 	if (gdImageTrueColor(im)) {
2900 		if (im->tpixels && gdImageBoundsSafe(im, x, y)) {
2901 			RETURN_LONG(gdImageTrueColorPixel(im, x, y));
2902 		} else {
2903 			php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
2904 			RETURN_FALSE;
2905 		}
2906 	} else {
2907 		if (im->pixels && gdImageBoundsSafe(im, x, y)) {
2908 			RETURN_LONG(im->pixels[y][x]);
2909 		} else {
2910 			php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
2911 			RETURN_FALSE;
2912 		}
2913 	}
2914 }
2915 /* }}} */
2916 
2917 /* {{{ proto int imagecolorclosest(resource im, int red, int green, int blue)
2918    Get the index of the closest color to the specified color */
PHP_FUNCTION(imagecolorclosest)2919 PHP_FUNCTION(imagecolorclosest)
2920 {
2921 	zval *IM;
2922 	zend_long red, green, blue;
2923 	gdImagePtr im;
2924 
2925 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &IM, &red, &green, &blue) == FAILURE) {
2926 		return;
2927 	}
2928 
2929 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2930 		RETURN_FALSE;
2931 	}
2932 
2933 	RETURN_LONG(gdImageColorClosest(im, red, green, blue));
2934 }
2935 /* }}} */
2936 
2937 /* {{{ proto int imagecolorclosesthwb(resource im, int red, int green, int blue)
2938    Get the index of the color which has the hue, white and blackness nearest to the given color */
PHP_FUNCTION(imagecolorclosesthwb)2939 PHP_FUNCTION(imagecolorclosesthwb)
2940 {
2941 	zval *IM;
2942 	zend_long red, green, blue;
2943 	gdImagePtr im;
2944 
2945 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &IM, &red, &green, &blue) == FAILURE) {
2946 		return;
2947 	}
2948 
2949 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2950 		RETURN_FALSE;
2951 	}
2952 
2953 	RETURN_LONG(gdImageColorClosestHWB(im, red, green, blue));
2954 }
2955 /* }}} */
2956 
2957 /* {{{ proto bool imagecolordeallocate(resource im, int index)
2958    De-allocate a color for an image */
PHP_FUNCTION(imagecolordeallocate)2959 PHP_FUNCTION(imagecolordeallocate)
2960 {
2961 	zval *IM;
2962 	zend_long index;
2963 	int col;
2964 	gdImagePtr im;
2965 
2966 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &IM, &index) == FAILURE) {
2967 		return;
2968 	}
2969 
2970 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
2971 		RETURN_FALSE;
2972 	}
2973 
2974 	/* We can return right away for a truecolor image as deallocating colours is meaningless here */
2975 	if (gdImageTrueColor(im)) {
2976 		RETURN_TRUE;
2977 	}
2978 
2979 	col = index;
2980 
2981 	if (col >= 0 && col < gdImageColorsTotal(im)) {
2982 		gdImageColorDeallocate(im, col);
2983 		RETURN_TRUE;
2984 	} else {
2985 		php_error_docref(NULL, E_WARNING, "Color index %d out of range",	col);
2986 		RETURN_FALSE;
2987 	}
2988 }
2989 /* }}} */
2990 
2991 /* {{{ proto int imagecolorresolve(resource im, int red, int green, int blue)
2992    Get the index of the specified color or its closest possible alternative */
PHP_FUNCTION(imagecolorresolve)2993 PHP_FUNCTION(imagecolorresolve)
2994 {
2995 	zval *IM;
2996 	zend_long red, green, blue;
2997 	gdImagePtr im;
2998 
2999 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &IM, &red, &green, &blue) == FAILURE) {
3000 		return;
3001 	}
3002 
3003 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3004 		RETURN_FALSE;
3005 	}
3006 
3007 	RETURN_LONG(gdImageColorResolve(im, red, green, blue));
3008 }
3009 /* }}} */
3010 
3011 /* {{{ proto int imagecolorexact(resource im, int red, int green, int blue)
3012    Get the index of the specified color */
PHP_FUNCTION(imagecolorexact)3013 PHP_FUNCTION(imagecolorexact)
3014 {
3015 	zval *IM;
3016 	zend_long red, green, blue;
3017 	gdImagePtr im;
3018 
3019 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &IM, &red, &green, &blue) == FAILURE) {
3020 		return;
3021 	}
3022 
3023 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3024 		RETURN_FALSE;
3025 	}
3026 
3027 	RETURN_LONG(gdImageColorExact(im, red, green, blue));
3028 }
3029 /* }}} */
3030 
3031 /* {{{ proto void imagecolorset(resource im, int col, int red, int green, int blue)
3032    Set the color for the specified palette index */
PHP_FUNCTION(imagecolorset)3033 PHP_FUNCTION(imagecolorset)
3034 {
3035 	zval *IM;
3036 	zend_long color, red, green, blue, alpha = 0;
3037 	int col;
3038 	gdImagePtr im;
3039 
3040 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll|l", &IM, &color, &red, &green, &blue, &alpha) == FAILURE) {
3041 		return;
3042 	}
3043 
3044 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3045 		RETURN_FALSE;
3046 	}
3047 
3048 	col = color;
3049 
3050 	if (col >= 0 && col < gdImageColorsTotal(im)) {
3051 		im->red[col]   = red;
3052 		im->green[col] = green;
3053 		im->blue[col]  = blue;
3054 		im->alpha[col]  = alpha;
3055 	} else {
3056 		RETURN_FALSE;
3057 	}
3058 }
3059 /* }}} */
3060 
3061 /* {{{ proto array imagecolorsforindex(resource im, int col)
3062    Get the colors for an index */
PHP_FUNCTION(imagecolorsforindex)3063 PHP_FUNCTION(imagecolorsforindex)
3064 {
3065 	zval *IM;
3066 	zend_long index;
3067 	int col;
3068 	gdImagePtr im;
3069 
3070 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &IM, &index) == FAILURE) {
3071 		return;
3072 	}
3073 
3074 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3075 		RETURN_FALSE;
3076 	}
3077 
3078 	col = index;
3079 
3080 	if ((col >= 0 && gdImageTrueColor(im)) || (!gdImageTrueColor(im) && col >= 0 && col < gdImageColorsTotal(im))) {
3081 		array_init(return_value);
3082 
3083 		add_assoc_long(return_value,"red",  gdImageRed(im,col));
3084 		add_assoc_long(return_value,"green", gdImageGreen(im,col));
3085 		add_assoc_long(return_value,"blue", gdImageBlue(im,col));
3086 		add_assoc_long(return_value,"alpha", gdImageAlpha(im,col));
3087 	} else {
3088 		php_error_docref(NULL, E_WARNING, "Color index %d out of range", col);
3089 		RETURN_FALSE;
3090 	}
3091 }
3092 /* }}} */
3093 
3094 /* {{{ proto bool imagegammacorrect(resource im, float inputgamma, float outputgamma)
3095    Apply a gamma correction to a GD image */
PHP_FUNCTION(imagegammacorrect)3096 PHP_FUNCTION(imagegammacorrect)
3097 {
3098 	zval *IM;
3099 	gdImagePtr im;
3100 	int i;
3101 	double input, output, gamma;
3102 
3103 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rdd", &IM, &input, &output) == FAILURE) {
3104 		return;
3105 	}
3106 
3107 	if ( input <= 0.0 || output <= 0.0 ) {
3108 		php_error_docref(NULL, E_WARNING, "Gamma values should be positive");
3109 		RETURN_FALSE;
3110 	}
3111 
3112 	gamma = input / output;
3113 
3114 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3115 		RETURN_FALSE;
3116 	}
3117 
3118 	if (gdImageTrueColor(im))	{
3119 		int x, y, c;
3120 
3121 		for (y = 0; y < gdImageSY(im); y++)	{
3122 			for (x = 0; x < gdImageSX(im); x++)	{
3123 				c = gdImageGetPixel(im, x, y);
3124 				gdImageSetPixel(im, x, y,
3125 					gdTrueColorAlpha(
3126 						(int) ((pow((gdTrueColorGetRed(c)   / 255.0), gamma) * 255) + .5),
3127 						(int) ((pow((gdTrueColorGetGreen(c) / 255.0), gamma) * 255) + .5),
3128 						(int) ((pow((gdTrueColorGetBlue(c)  / 255.0), gamma) * 255) + .5),
3129 						gdTrueColorGetAlpha(c)
3130 					)
3131 				);
3132 			}
3133 		}
3134 		RETURN_TRUE;
3135 	}
3136 
3137 	for (i = 0; i < gdImageColorsTotal(im); i++) {
3138 		im->red[i]   = (int)((pow((im->red[i]   / 255.0), gamma) * 255) + .5);
3139 		im->green[i] = (int)((pow((im->green[i] / 255.0), gamma) * 255) + .5);
3140 		im->blue[i]  = (int)((pow((im->blue[i]  / 255.0), gamma) * 255) + .5);
3141 	}
3142 
3143 	RETURN_TRUE;
3144 }
3145 /* }}} */
3146 
3147 /* {{{ proto bool imagesetpixel(resource im, int x, int y, int col)
3148    Set a single pixel */
PHP_FUNCTION(imagesetpixel)3149 PHP_FUNCTION(imagesetpixel)
3150 {
3151 	zval *IM;
3152 	zend_long x, y, col;
3153 	gdImagePtr im;
3154 
3155 	ZEND_PARSE_PARAMETERS_START(4, 4)
3156 		Z_PARAM_RESOURCE(IM)
3157 		Z_PARAM_LONG(x)
3158 		Z_PARAM_LONG(y)
3159 		Z_PARAM_LONG(col)
3160 	ZEND_PARSE_PARAMETERS_END();
3161 
3162 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3163 		RETURN_FALSE;
3164 	}
3165 
3166 	gdImageSetPixel(im, x, y, col);
3167 	RETURN_TRUE;
3168 }
3169 /* }}} */
3170 
3171 /* {{{ proto bool imageline(resource im, int x1, int y1, int x2, int y2, int col)
3172    Draw a line */
PHP_FUNCTION(imageline)3173 PHP_FUNCTION(imageline)
3174 {
3175 	zval *IM;
3176 	zend_long x1, y1, x2, y2, col;
3177 	gdImagePtr im;
3178 
3179 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
3180 		return;
3181 	}
3182 
3183 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3184 		RETURN_FALSE;
3185 	}
3186 
3187 	if (im->AA) {
3188 		gdImageSetAntiAliased(im, col);
3189 		col = gdAntiAliased;
3190 	}
3191 	gdImageLine(im, x1, y1, x2, y2, col);
3192 	RETURN_TRUE;
3193 }
3194 /* }}} */
3195 
3196 /* {{{ proto bool imagedashedline(resource im, int x1, int y1, int x2, int y2, int col)
3197    Draw a dashed line */
PHP_FUNCTION(imagedashedline)3198 PHP_FUNCTION(imagedashedline)
3199 {
3200 	zval *IM;
3201 	zend_long x1, y1, x2, y2, col;
3202 	gdImagePtr im;
3203 
3204 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
3205 		return;
3206 	}
3207 
3208 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3209 		RETURN_FALSE;
3210 	}
3211 
3212 	gdImageDashedLine(im, x1, y1, x2, y2, col);
3213 	RETURN_TRUE;
3214 }
3215 /* }}} */
3216 
3217 /* {{{ proto bool imagerectangle(resource im, int x1, int y1, int x2, int y2, int col)
3218    Draw a rectangle */
PHP_FUNCTION(imagerectangle)3219 PHP_FUNCTION(imagerectangle)
3220 {
3221 	zval *IM;
3222 	zend_long x1, y1, x2, y2, col;
3223 	gdImagePtr im;
3224 
3225 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
3226 		return;
3227 	}
3228 
3229 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3230 		RETURN_FALSE;
3231 	}
3232 
3233 	gdImageRectangle(im, x1, y1, x2, y2, col);
3234 	RETURN_TRUE;
3235 }
3236 /* }}} */
3237 
3238 /* {{{ proto bool imagefilledrectangle(resource im, int x1, int y1, int x2, int y2, int col)
3239    Draw a filled rectangle */
PHP_FUNCTION(imagefilledrectangle)3240 PHP_FUNCTION(imagefilledrectangle)
3241 {
3242 	zval *IM;
3243 	zend_long x1, y1, x2, y2, col;
3244 	gdImagePtr im;
3245 
3246 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
3247 		return;
3248 	}
3249 
3250 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3251 		RETURN_FALSE;
3252 	}
3253 	gdImageFilledRectangle(im, x1, y1, x2, y2, col);
3254 	RETURN_TRUE;
3255 }
3256 /* }}} */
3257 
3258 /* {{{ proto bool imagearc(resource im, int cx, int cy, int w, int h, int s, int e, int col)
3259    Draw a partial ellipse */
PHP_FUNCTION(imagearc)3260 PHP_FUNCTION(imagearc)
3261 {
3262 	zval *IM;
3263 	zend_long cx, cy, w, h, ST, E, col;
3264 	gdImagePtr im;
3265 	int e, st;
3266 
3267 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllllll", &IM, &cx, &cy, &w, &h, &ST, &E, &col) == FAILURE) {
3268 		return;
3269 	}
3270 
3271 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3272 		RETURN_FALSE;
3273 	}
3274 
3275 	e = E;
3276 	if (e < 0) {
3277 		e %= 360;
3278 	}
3279 
3280 	st = ST;
3281 	if (st < 0) {
3282 		st %= 360;
3283 	}
3284 
3285 	gdImageArc(im, cx, cy, w, h, st, e, col);
3286 	RETURN_TRUE;
3287 }
3288 /* }}} */
3289 
3290 /* {{{ proto bool imageellipse(resource im, int cx, int cy, int w, int h, int color)
3291    Draw an ellipse */
PHP_FUNCTION(imageellipse)3292 PHP_FUNCTION(imageellipse)
3293 {
3294 	zval *IM;
3295 	zend_long cx, cy, w, h, color;
3296 	gdImagePtr im;
3297 
3298 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllll", &IM, &cx, &cy, &w, &h, &color) == FAILURE) {
3299 		return;
3300 	}
3301 
3302 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3303 		RETURN_FALSE;
3304 	}
3305 
3306 	gdImageEllipse(im, cx, cy, w, h, color);
3307 	RETURN_TRUE;
3308 }
3309 /* }}} */
3310 
3311 /* {{{ proto bool imagefilltoborder(resource im, int x, int y, int border, int col)
3312    Flood fill to specific color */
PHP_FUNCTION(imagefilltoborder)3313 PHP_FUNCTION(imagefilltoborder)
3314 {
3315 	zval *IM;
3316 	zend_long x, y, border, col;
3317 	gdImagePtr im;
3318 
3319 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &IM, &x, &y, &border, &col) == FAILURE) {
3320 		return;
3321 	}
3322 
3323 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3324 		RETURN_FALSE;
3325 	}
3326 
3327 	gdImageFillToBorder(im, x, y, border, col);
3328 	RETURN_TRUE;
3329 }
3330 /* }}} */
3331 
3332 /* {{{ proto bool imagefill(resource im, int x, int y, int col)
3333    Flood fill */
PHP_FUNCTION(imagefill)3334 PHP_FUNCTION(imagefill)
3335 {
3336 	zval *IM;
3337 	zend_long x, y, col;
3338 	gdImagePtr im;
3339 
3340 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &IM, &x, &y, &col) == FAILURE) {
3341 		return;
3342 	}
3343 
3344 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3345 		RETURN_FALSE;
3346 	}
3347 
3348 	gdImageFill(im, x, y, col);
3349 	RETURN_TRUE;
3350 }
3351 /* }}} */
3352 
3353 /* {{{ proto int imagecolorstotal(resource im)
3354    Find out the number of colors in an image's palette */
PHP_FUNCTION(imagecolorstotal)3355 PHP_FUNCTION(imagecolorstotal)
3356 {
3357 	zval *IM;
3358 	gdImagePtr im;
3359 
3360 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
3361 		return;
3362 	}
3363 
3364 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3365 		RETURN_FALSE;
3366 	}
3367 
3368 	RETURN_LONG(gdImageColorsTotal(im));
3369 }
3370 /* }}} */
3371 
3372 /* {{{ proto int imagecolortransparent(resource im [, int col])
3373    Define a color as transparent */
PHP_FUNCTION(imagecolortransparent)3374 PHP_FUNCTION(imagecolortransparent)
3375 {
3376 	zval *IM;
3377 	zend_long COL = 0;
3378 	gdImagePtr im;
3379 	int argc = ZEND_NUM_ARGS();
3380 
3381 	if (zend_parse_parameters(argc, "r|l", &IM, &COL) == FAILURE) {
3382 		return;
3383 	}
3384 
3385 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3386 		RETURN_FALSE;
3387 	}
3388 
3389 	if (argc > 1) {
3390 		gdImageColorTransparent(im, COL);
3391 	}
3392 
3393 	RETURN_LONG(gdImageGetTransparent(im));
3394 }
3395 /* }}} */
3396 
3397 /* {{{ proto int imageinterlace(resource im [, int interlace])
3398    Enable or disable interlace */
PHP_FUNCTION(imageinterlace)3399 PHP_FUNCTION(imageinterlace)
3400 {
3401 	zval *IM;
3402 	int argc = ZEND_NUM_ARGS();
3403 	zend_long INT = 0;
3404 	gdImagePtr im;
3405 
3406 	if (zend_parse_parameters(argc, "r|l", &IM, &INT) == FAILURE) {
3407 		return;
3408 	}
3409 
3410 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3411 		RETURN_FALSE;
3412 	}
3413 
3414 	if (argc > 1) {
3415 		gdImageInterlace(im, INT);
3416 	}
3417 
3418 	RETURN_LONG(gdImageGetInterlaced(im));
3419 }
3420 /* }}} */
3421 
3422 /* {{{ php_imagepolygon
3423    arg = -1 open polygon
3424    arg = 0  normal polygon
3425    arg = 1  filled polygon */
3426 /* im, points, num_points, col */
php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS,int filled)3427 static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
3428 {
3429 	zval *IM, *POINTS;
3430 	zend_long NPOINTS, COL;
3431 	zval *var = NULL;
3432 	gdImagePtr im;
3433 	gdPointPtr points;
3434 	int npoints, col, nelem, i;
3435 
3436 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rall", &IM, &POINTS, &NPOINTS, &COL) == FAILURE) {
3437 		return;
3438 	}
3439 
3440 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3441 		RETURN_FALSE;
3442 	}
3443 
3444 	npoints = NPOINTS;
3445 	col = COL;
3446 
3447 	nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
3448 	if (nelem < 6) {
3449 		php_error_docref(NULL, E_WARNING, "You must have at least 3 points in your array");
3450 		RETURN_FALSE;
3451 	}
3452 	if (npoints <= 0) {
3453 		php_error_docref(NULL, E_WARNING, "You must give a positive number of points");
3454 		RETURN_FALSE;
3455 	}
3456 	if (nelem < npoints * 2) {
3457 		php_error_docref(NULL, E_WARNING, "Trying to use %d points in array with only %d points", npoints, nelem/2);
3458 		RETURN_FALSE;
3459 	}
3460 
3461 	points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
3462 
3463 	for (i = 0; i < npoints; i++) {
3464 		if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2))) != NULL) {
3465 			points[i].x = zval_get_long(var);
3466 		}
3467 		if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2) + 1)) != NULL) {
3468 			points[i].y = zval_get_long(var);
3469 		}
3470 	}
3471 
3472 	if (im->AA) {
3473 		gdImageSetAntiAliased(im, col);
3474 		col = gdAntiAliased;
3475 	}
3476 	switch (filled) {
3477 		case -1:
3478 			gdImageOpenPolygon(im, points, npoints, col);
3479 			break;
3480 		case 0:
3481 			gdImagePolygon(im, points, npoints, col);
3482 			break;
3483 		case 1:
3484 			gdImageFilledPolygon(im, points, npoints, col);
3485 			break;
3486 	}
3487 
3488 	efree(points);
3489 	RETURN_TRUE;
3490 }
3491 /* }}} */
3492 
3493 /* {{{ proto bool imagepolygon(resource im, array point, int num_points, int col)
3494    Draw a polygon */
PHP_FUNCTION(imagepolygon)3495 PHP_FUNCTION(imagepolygon)
3496 {
3497 	php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3498 }
3499 /* }}} */
3500 
3501 /* {{{ proto bool imageopenpolygon(resource im, array point, int num_points, int col)
3502    Draw a polygon */
PHP_FUNCTION(imageopenpolygon)3503 PHP_FUNCTION(imageopenpolygon)
3504 {
3505 	php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1);
3506 }
3507 /* }}} */
3508 
3509 /* {{{ proto bool imagefilledpolygon(resource im, array point, int num_points, int col)
3510    Draw a filled polygon */
PHP_FUNCTION(imagefilledpolygon)3511 PHP_FUNCTION(imagefilledpolygon)
3512 {
3513 	php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3514 }
3515 /* }}} */
3516 
3517 /* {{{ php_find_gd_font
3518  */
php_find_gd_font(int size)3519 static gdFontPtr php_find_gd_font(int size)
3520 {
3521 	gdFontPtr font;
3522 
3523 	switch (size) {
3524 		case 1:
3525 			font = gdFontTiny;
3526 			break;
3527 		case 2:
3528 			font = gdFontSmall;
3529 			break;
3530 		case 3:
3531 			font = gdFontMediumBold;
3532 			break;
3533 		case 4:
3534 			font = gdFontLarge;
3535 			break;
3536 		case 5:
3537 			font = gdFontGiant;
3538 			break;
3539 		default: {
3540 			 zval *zv = zend_hash_index_find(&EG(regular_list), size - 5);
3541 			 if (!zv || (Z_RES_P(zv))->type != le_gd_font) {
3542 				 if (size < 1) {
3543 					 font = gdFontTiny;
3544 				 } else {
3545 					 font = gdFontGiant;
3546 				 }
3547 			 } else {
3548 				 font = (gdFontPtr)Z_RES_P(zv)->ptr;
3549 			 }
3550 		 }
3551 		 break;
3552 	}
3553 
3554 	return font;
3555 }
3556 /* }}} */
3557 
3558 /* {{{ php_imagefontsize
3559  * arg = 0  ImageFontWidth
3560  * arg = 1  ImageFontHeight
3561  */
php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS,int arg)3562 static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg)
3563 {
3564 	zend_long SIZE;
3565 	gdFontPtr font;
3566 
3567 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &SIZE) == FAILURE) {
3568 		return;
3569 	}
3570 
3571 	font = php_find_gd_font(SIZE);
3572 	RETURN_LONG(arg ? font->h : font->w);
3573 }
3574 /* }}} */
3575 
3576 /* {{{ proto int imagefontwidth(int font)
3577    Get font width */
PHP_FUNCTION(imagefontwidth)3578 PHP_FUNCTION(imagefontwidth)
3579 {
3580 	php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3581 }
3582 /* }}} */
3583 
3584 /* {{{ proto int imagefontheight(int font)
3585    Get font height */
PHP_FUNCTION(imagefontheight)3586 PHP_FUNCTION(imagefontheight)
3587 {
3588 	php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3589 }
3590 /* }}} */
3591 
3592 /* {{{ php_gdimagecharup
3593  * workaround for a bug in gd 1.2 */
php_gdimagecharup(gdImagePtr im,gdFontPtr f,int x,int y,int c,int color)3594 static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color)
3595 {
3596 	int cx, cy, px, py, fline;
3597 	cx = 0;
3598 	cy = 0;
3599 
3600 	if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
3601 		return;
3602 	}
3603 
3604 	fline = (c - f->offset) * f->h * f->w;
3605 	for (py = y; (py > (y - f->w)); py--) {
3606 		for (px = x; (px < (x + f->h)); px++) {
3607 			if (f->data[fline + cy * f->w + cx]) {
3608 				gdImageSetPixel(im, px, py, color);
3609 			}
3610 			cy++;
3611 		}
3612 		cy = 0;
3613 		cx++;
3614 	}
3615 }
3616 /* }}} */
3617 
3618 /* {{{ php_imagechar
3619  * arg = 0  ImageChar
3620  * arg = 1  ImageCharUp
3621  * arg = 2  ImageString
3622  * arg = 3  ImageStringUp
3623  */
php_imagechar(INTERNAL_FUNCTION_PARAMETERS,int mode)3624 static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode)
3625 {
3626 	zval *IM;
3627 	zend_long SIZE, X, Y, COL;
3628 	char *C;
3629 	size_t C_len;
3630 	gdImagePtr im;
3631 	int ch = 0, col, x, y, size, i, l = 0;
3632 	unsigned char *str = NULL;
3633 	gdFontPtr font;
3634 
3635 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlllsl", &IM, &SIZE, &X, &Y, &C, &C_len, &COL) == FAILURE) {
3636 		return;
3637 	}
3638 
3639 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3640 		RETURN_FALSE;
3641 	}
3642 
3643 	col = COL;
3644 
3645 	if (mode < 2) {
3646 		ch = (int)((unsigned char)*C);
3647 	} else {
3648 		str = (unsigned char *) estrndup(C, C_len);
3649 		l = strlen((char *)str);
3650 	}
3651 
3652 	y = Y;
3653 	x = X;
3654 	size = SIZE;
3655 
3656 	font = php_find_gd_font(size);
3657 
3658 	switch (mode) {
3659 		case 0:
3660 			gdImageChar(im, font, x, y, ch, col);
3661 			break;
3662 		case 1:
3663 			php_gdimagecharup(im, font, x, y, ch, col);
3664 			break;
3665 		case 2:
3666 			for (i = 0; (i < l); i++) {
3667 				gdImageChar(im, font, x, y, (int) ((unsigned char) str[i]), col);
3668 				x += font->w;
3669 			}
3670 			break;
3671 		case 3: {
3672 			for (i = 0; (i < l); i++) {
3673 				/* php_gdimagecharup(im, font, x, y, (int) str[i], col); */
3674 				gdImageCharUp(im, font, x, y, (int) str[i], col);
3675 				y -= font->w;
3676 			}
3677 			break;
3678 		}
3679 	}
3680 	if (str) {
3681 		efree(str);
3682 	}
3683 	RETURN_TRUE;
3684 }
3685 /* }}} */
3686 
3687 /* {{{ proto bool imagechar(resource im, int font, int x, int y, string c, int col)
3688    Draw a character */
PHP_FUNCTION(imagechar)3689 PHP_FUNCTION(imagechar)
3690 {
3691 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3692 }
3693 /* }}} */
3694 
3695 /* {{{ proto bool imagecharup(resource im, int font, int x, int y, string c, int col)
3696    Draw a character rotated 90 degrees counter-clockwise */
PHP_FUNCTION(imagecharup)3697 PHP_FUNCTION(imagecharup)
3698 {
3699 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3700 }
3701 /* }}} */
3702 
3703 /* {{{ proto bool imagestring(resource im, int font, int x, int y, string str, int col)
3704    Draw a string horizontally */
PHP_FUNCTION(imagestring)3705 PHP_FUNCTION(imagestring)
3706 {
3707 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
3708 }
3709 /* }}} */
3710 
3711 /* {{{ proto bool imagestringup(resource im, int font, int x, int y, string str, int col)
3712    Draw a string vertically - rotated 90 degrees counter-clockwise */
PHP_FUNCTION(imagestringup)3713 PHP_FUNCTION(imagestringup)
3714 {
3715 	php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
3716 }
3717 /* }}} */
3718 
3719 /* {{{ proto bool imagecopy(resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h)
3720    Copy part of an image */
PHP_FUNCTION(imagecopy)3721 PHP_FUNCTION(imagecopy)
3722 {
3723 	zval *SIM, *DIM;
3724 	zend_long SX, SY, SW, SH, DX, DY;
3725 	gdImagePtr im_dst, im_src;
3726 	int srcH, srcW, srcY, srcX, dstY, dstX;
3727 
3728 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH) == FAILURE) {
3729 		return;
3730 	}
3731 
3732 	if ((im_dst = (gdImagePtr)zend_fetch_resource(Z_RES_P(DIM), "Image", le_gd)) == NULL) {
3733 		RETURN_FALSE;
3734 	}
3735 
3736 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
3737 		RETURN_FALSE;
3738 	}
3739 
3740 	srcX = SX;
3741 	srcY = SY;
3742 	srcH = SH;
3743 	srcW = SW;
3744 	dstX = DX;
3745 	dstY = DY;
3746 
3747 	gdImageCopy(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH);
3748 	RETURN_TRUE;
3749 }
3750 /* }}} */
3751 
3752 /* {{{ proto bool imagecopymerge(resource src_im, resource dst_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)
3753    Merge one part of an image with another */
PHP_FUNCTION(imagecopymerge)3754 PHP_FUNCTION(imagecopymerge)
3755 {
3756 	zval *SIM, *DIM;
3757 	zend_long SX, SY, SW, SH, DX, DY, PCT;
3758 	gdImagePtr im_dst, im_src;
3759 	int srcH, srcW, srcY, srcX, dstY, dstX, pct;
3760 
3761 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrlllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
3762 		return;
3763 	}
3764 
3765 	if ((im_dst = (gdImagePtr)zend_fetch_resource(Z_RES_P(DIM), "Image", le_gd)) == NULL) {
3766 		RETURN_FALSE;
3767 	}
3768 
3769 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
3770 		RETURN_FALSE;
3771 	}
3772 
3773 	srcX = SX;
3774 	srcY = SY;
3775 	srcH = SH;
3776 	srcW = SW;
3777 	dstX = DX;
3778 	dstY = DY;
3779 	pct  = PCT;
3780 
3781 	gdImageCopyMerge(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
3782 	RETURN_TRUE;
3783 }
3784 /* }}} */
3785 
3786 /* {{{ proto bool imagecopymergegray(resource src_im, resource dst_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)
3787    Merge one part of an image with another */
PHP_FUNCTION(imagecopymergegray)3788 PHP_FUNCTION(imagecopymergegray)
3789 {
3790 	zval *SIM, *DIM;
3791 	zend_long SX, SY, SW, SH, DX, DY, PCT;
3792 	gdImagePtr im_dst, im_src;
3793 	int srcH, srcW, srcY, srcX, dstY, dstX, pct;
3794 
3795 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrlllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
3796 		return;
3797 	}
3798 
3799 	if ((im_dst = (gdImagePtr)zend_fetch_resource(Z_RES_P(DIM), "Image", le_gd)) == NULL) {
3800 		RETURN_FALSE;
3801 	}
3802 
3803 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
3804 		RETURN_FALSE;
3805 	}
3806 
3807 	srcX = SX;
3808 	srcY = SY;
3809 	srcH = SH;
3810 	srcW = SW;
3811 	dstX = DX;
3812 	dstY = DY;
3813 	pct  = PCT;
3814 
3815 	gdImageCopyMergeGray(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
3816 	RETURN_TRUE;
3817 }
3818 /* }}} */
3819 
3820 /* {{{ proto bool imagecopyresized(resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h)
3821    Copy and resize part of an image */
PHP_FUNCTION(imagecopyresized)3822 PHP_FUNCTION(imagecopyresized)
3823 {
3824 	zval *SIM, *DIM;
3825 	zend_long SX, SY, SW, SH, DX, DY, DW, DH;
3826 	gdImagePtr im_dst, im_src;
3827 	int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
3828 
3829 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrllllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
3830 		return;
3831 	}
3832 
3833 	if ((im_dst = (gdImagePtr)zend_fetch_resource(Z_RES_P(DIM), "Image", le_gd)) == NULL) {
3834 		RETURN_FALSE;
3835 	}
3836 
3837 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
3838 		RETURN_FALSE;
3839 	}
3840 
3841 	srcX = SX;
3842 	srcY = SY;
3843 	srcH = SH;
3844 	srcW = SW;
3845 	dstX = DX;
3846 	dstY = DY;
3847 	dstH = DH;
3848 	dstW = DW;
3849 
3850 	if (dstW <= 0 || dstH <= 0 || srcW <= 0 || srcH <= 0) {
3851 		php_error_docref(NULL, E_WARNING, "Invalid image dimensions");
3852 		RETURN_FALSE;
3853 	}
3854 
3855 	gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
3856 	RETURN_TRUE;
3857 }
3858 /* }}} */
3859 
3860 /* {{{ proto int imagesx(resource im)
3861    Get image width */
PHP_FUNCTION(imagesx)3862 PHP_FUNCTION(imagesx)
3863 {
3864 	zval *IM;
3865 	gdImagePtr im;
3866 
3867 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
3868 		return;
3869 	}
3870 
3871 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3872 		RETURN_FALSE;
3873 	}
3874 
3875 	RETURN_LONG(gdImageSX(im));
3876 }
3877 /* }}} */
3878 
3879 /* {{{ proto int imagesy(resource im)
3880    Get image height */
PHP_FUNCTION(imagesy)3881 PHP_FUNCTION(imagesy)
3882 {
3883 	zval *IM;
3884 	gdImagePtr im;
3885 
3886 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
3887 		return;
3888 	}
3889 
3890 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
3891 		RETURN_FALSE;
3892 	}
3893 
3894 	RETURN_LONG(gdImageSY(im));
3895 }
3896 /* }}} */
3897 
3898 /* {{{ proto bool imagesetclip(resource im, int x1, int y1, int x2, int y2)
3899    Set the clipping rectangle. */
PHP_FUNCTION(imagesetclip)3900 PHP_FUNCTION(imagesetclip)
3901 {
3902 	zval *im_zval;
3903 	gdImagePtr im;
3904 	zend_long x1, y1, x2, y2;
3905 
3906 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll", &im_zval, &x1, &y1, &x2, &y2) == FAILURE) {
3907 		return;
3908 	}
3909 
3910 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(im_zval), "Image", le_gd)) == NULL) {
3911 		RETURN_FALSE;
3912 	}
3913 
3914 	gdImageSetClip(im, x1, y1, x2, y2);
3915 	RETURN_TRUE;
3916 }
3917 /* }}} */
3918 
3919 /* {{{ proto array imagegetclip(resource im)
3920    Get the clipping rectangle. */
PHP_FUNCTION(imagegetclip)3921 PHP_FUNCTION(imagegetclip)
3922 {
3923 	zval *im_zval;
3924 	gdImagePtr im;
3925 	int x1, y1, x2, y2;
3926 
3927 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &im_zval) == FAILURE) {
3928 		return;
3929 	}
3930 
3931 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(im_zval), "Image", le_gd)) == NULL) {
3932 		RETURN_FALSE;
3933 	}
3934 
3935 	gdImageGetClip(im, &x1, &y1, &x2, &y2);
3936 
3937 	array_init(return_value);
3938 	add_next_index_long(return_value, x1);
3939 	add_next_index_long(return_value, y1);
3940 	add_next_index_long(return_value, x2);
3941 	add_next_index_long(return_value, y2);
3942 }
3943 /* }}} */
3944 
3945 #ifdef ENABLE_GD_TTF
3946 #define TTFTEXT_DRAW 0
3947 #define TTFTEXT_BBOX 1
3948 #endif
3949 
3950 #ifdef ENABLE_GD_TTF
3951 
3952 #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
3953 /* {{{ proto array imageftbbox(float size, float angle, string font_file, string text [, array extrainfo])
3954    Give the bounding box of a text using fonts via freetype2 */
PHP_FUNCTION(imageftbbox)3955 PHP_FUNCTION(imageftbbox)
3956 {
3957 	php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX, 1);
3958 }
3959 /* }}} */
3960 
3961 /* {{{ proto array imagefttext(resource im, float size, float angle, int x, int y, int col, string font_file, string text [, array extrainfo])
3962    Write text to the image using fonts via freetype2 */
PHP_FUNCTION(imagefttext)3963 PHP_FUNCTION(imagefttext)
3964 {
3965 	php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW, 1);
3966 }
3967 /* }}} */
3968 #endif /* HAVE_GD_FREETYPE && HAVE_LIBFREETYPE */
3969 
3970 /* {{{ proto array imagettfbbox(float size, float angle, string font_file, string text)
3971    Give the bounding box of a text using TrueType fonts */
PHP_FUNCTION(imagettfbbox)3972 PHP_FUNCTION(imagettfbbox)
3973 {
3974 	php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX, 0);
3975 }
3976 /* }}} */
3977 
3978 /* {{{ proto array imagettftext(resource im, float size, float angle, int x, int y, int col, string font_file, string text)
3979    Write text to the image using a TrueType font */
PHP_FUNCTION(imagettftext)3980 PHP_FUNCTION(imagettftext)
3981 {
3982 	php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW, 0);
3983 }
3984 /* }}} */
3985 
3986 /* {{{ php_imagettftext_common
3987  */
php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS,int mode,int extended)3988 static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int mode, int extended)
3989 {
3990 	zval *IM, *EXT = NULL;
3991 	gdImagePtr im=NULL;
3992 	zend_long col = -1, x = 0, y = 0;
3993 	size_t str_len, fontname_len;
3994 	int i, brect[8];
3995 	double ptsize, angle;
3996 	char *str = NULL, *fontname = NULL;
3997 	char *error = NULL;
3998 	int argc = ZEND_NUM_ARGS();
3999 	gdFTStringExtra strex = {0};
4000 
4001 	if (mode == TTFTEXT_BBOX) {
4002 		if (argc < 4 || argc > ((extended) ? 5 : 4)) {
4003 			ZEND_WRONG_PARAM_COUNT();
4004 		} else if (zend_parse_parameters(argc, "ddss|a", &ptsize, &angle, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
4005 			RETURN_FALSE;
4006 		}
4007 	} else {
4008 		if (argc < 8 || argc > ((extended) ? 9 : 8)) {
4009 			ZEND_WRONG_PARAM_COUNT();
4010 		} else if (zend_parse_parameters(argc, "rddlllss|a", &IM, &ptsize, &angle, &x, &y, &col, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
4011 			RETURN_FALSE;
4012 		}
4013 		if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4014 			RETURN_FALSE;
4015 		}
4016 	}
4017 
4018 	/* convert angle to radians */
4019 	angle = angle * (M_PI/180);
4020 
4021 	if (extended && EXT) {	/* parse extended info */
4022 		zval *item;
4023 		zend_string *key;
4024 
4025 		/* walk the assoc array */
4026 		ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(EXT), key, item) {
4027 			if (key == NULL) {
4028 				continue;
4029 			}
4030 			if (strcmp("linespacing", ZSTR_VAL(key)) == 0) {
4031 				strex.flags |= gdFTEX_LINESPACE;
4032 				strex.linespacing = zval_get_double(item);
4033 			}
4034 		} ZEND_HASH_FOREACH_END();
4035 	}
4036 
4037 #ifdef VIRTUAL_DIR
4038 	{
4039 		char tmp_font_path[MAXPATHLEN];
4040 
4041 		if (!VCWD_REALPATH(fontname, tmp_font_path)) {
4042 			fontname = NULL;
4043 		}
4044 	}
4045 #endif /* VIRTUAL_DIR */
4046 
4047 	PHP_GD_CHECK_OPEN_BASEDIR(fontname, "Invalid font filename");
4048 
4049 #ifdef HAVE_GD_FREETYPE
4050 	if (extended) {
4051 		error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
4052 	}
4053 	else
4054 		error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
4055 
4056 #endif /* HAVE_GD_FREETYPE */
4057 
4058 	if (error) {
4059 		php_error_docref(NULL, E_WARNING, "%s", error);
4060 		RETURN_FALSE;
4061 	}
4062 
4063 	array_init(return_value);
4064 
4065 	/* return array with the text's bounding box */
4066 	for (i = 0; i < 8; i++) {
4067 		add_next_index_long(return_value, brect[i]);
4068 	}
4069 }
4070 /* }}} */
4071 #endif	/* ENABLE_GD_TTF */
4072 
4073 /* {{{ proto bool image2wbmp(resource im [, string filename [, int threshold]])
4074    Output WBMP image to browser or file */
PHP_FUNCTION(image2wbmp)4075 PHP_FUNCTION(image2wbmp)
4076 {
4077 	_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_CONVERT_WBM, "WBMP", _php_image_bw_convert);
4078 }
4079 /* }}} */
4080 
4081 #if defined(HAVE_GD_JPG)
4082 /* {{{ proto bool jpeg2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold)
4083    Convert JPEG image to WBMP image */
PHP_FUNCTION(jpeg2wbmp)4084 PHP_FUNCTION(jpeg2wbmp)
4085 {
4086 	_php_image_convert(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG);
4087 }
4088 /* }}} */
4089 #endif
4090 
4091 #if defined(HAVE_GD_PNG)
4092 /* {{{ proto bool png2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold)
4093    Convert PNG image to WBMP image */
PHP_FUNCTION(png2wbmp)4094 PHP_FUNCTION(png2wbmp)
4095 {
4096 	_php_image_convert(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG);
4097 }
4098 /* }}} */
4099 #endif
4100 
4101 /* {{{ _php_image_bw_convert
4102  * It converts a gd Image to bw using a threshold value */
_php_image_bw_convert(gdImagePtr im_org,gdIOCtx * out,int threshold)4103 static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold)
4104 {
4105 	gdImagePtr im_dest;
4106 	int white, black;
4107 	int color, color_org, median;
4108 	int dest_height = gdImageSY(im_org);
4109 	int dest_width = gdImageSX(im_org);
4110 	int x, y;
4111 
4112 	im_dest = gdImageCreate(dest_width, dest_height);
4113 	if (im_dest == NULL) {
4114 		php_error_docref(NULL, E_WARNING, "Unable to allocate temporary buffer");
4115 		return;
4116 	}
4117 
4118 	white = gdImageColorAllocate(im_dest, 255, 255, 255);
4119 	if (white == -1) {
4120 		php_error_docref(NULL, E_WARNING, "Unable to allocate the colors for the destination buffer");
4121 		return;
4122 	}
4123 
4124 	black = gdImageColorAllocate(im_dest, 0, 0, 0);
4125 	if (black == -1) {
4126 		php_error_docref(NULL, E_WARNING, "Unable to allocate the colors for the destination buffer");
4127 		return;
4128 	}
4129 
4130 	if (im_org->trueColor) {
4131 		if (!gdImageTrueColorToPalette(im_org, 1, 256)) {
4132 			php_error_docref(NULL, E_WARNING, "Unable to convert to palette");
4133 			return;
4134 		}
4135 	}
4136 
4137 	for (y = 0; y < dest_height; y++) {
4138 		for (x = 0; x < dest_width; x++) {
4139 			color_org = gdImageGetPixel(im_org, x, y);
4140 			median = (im_org->red[color_org] + im_org->green[color_org] + im_org->blue[color_org]) / 3;
4141 			if (median < threshold) {
4142 				color = black;
4143 			} else {
4144 				color = white;
4145 			}
4146 			gdImageSetPixel (im_dest, x, y, color);
4147 		}
4148 	}
4149 	gdImageWBMPCtx (im_dest, black, out);
4150 
4151 }
4152 /* }}} */
4153 
4154 /* {{{ _php_image_convert
4155  * _php_image_convert converts jpeg/png images to wbmp and resizes them as needed  */
_php_image_convert(INTERNAL_FUNCTION_PARAMETERS,int image_type)4156 static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
4157 {
4158 	char *f_org, *f_dest;
4159 	size_t f_org_len, f_dest_len;
4160 	zend_long height, width, threshold;
4161 	gdImagePtr im_org, im_dest, im_tmp;
4162 	char *fn_org = NULL;
4163 	char *fn_dest = NULL;
4164 	FILE *org, *dest;
4165 	int dest_height = -1;
4166 	int dest_width = -1;
4167 	int org_height, org_width;
4168 	int white, black;
4169 	int color, color_org, median;
4170 	int int_threshold;
4171 	int x, y;
4172 	float x_ratio, y_ratio;
4173 #ifdef HAVE_GD_JPG
4174     zend_long ignore_warning;
4175 #endif
4176 
4177 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pplll", &f_org, &f_org_len, &f_dest, &f_dest_len, &height, &width, &threshold) == FAILURE) {
4178 		return;
4179 	}
4180 
4181 	fn_org  = f_org;
4182 	fn_dest = f_dest;
4183 	dest_height = height;
4184 	dest_width = width;
4185 	int_threshold = threshold;
4186 
4187 	/* Check threshold value */
4188 	if (int_threshold < 0 || int_threshold > 8) {
4189 		php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'", int_threshold);
4190 		RETURN_FALSE;
4191 	}
4192 
4193 	/* Check origin file */
4194 	PHP_GD_CHECK_OPEN_BASEDIR(fn_org, "Invalid origin filename");
4195 
4196 	/* Check destination file */
4197 	PHP_GD_CHECK_OPEN_BASEDIR(fn_dest, "Invalid destination filename");
4198 
4199 	/* Open origin file */
4200 	org = VCWD_FOPEN(fn_org, "rb");
4201 	if (!org) {
4202 		php_error_docref(NULL, E_WARNING, "Unable to open '%s' for reading", fn_org);
4203 		RETURN_FALSE;
4204 	}
4205 
4206 	/* Open destination file */
4207 	dest = VCWD_FOPEN(fn_dest, "wb");
4208 	if (!dest) {
4209 		php_error_docref(NULL, E_WARNING, "Unable to open '%s' for writing", fn_dest);
4210         fclose(org);
4211 		RETURN_FALSE;
4212 	}
4213 
4214 	switch (image_type) {
4215 
4216 #ifdef HAVE_GD_JPG
4217 		case PHP_GDIMG_TYPE_JPG:
4218 			ignore_warning = INI_INT("gd.jpeg_ignore_warning");
4219 			im_org = gdImageCreateFromJpegEx(org, ignore_warning);
4220 			if (im_org == NULL) {
4221 				php_error_docref(NULL, E_WARNING, "Unable to open '%s' Not a valid JPEG file", fn_dest);
4222                 fclose(org);
4223                 fclose(dest);
4224 				RETURN_FALSE;
4225 			}
4226 			break;
4227 #endif /* HAVE_GD_JPG */
4228 
4229 #ifdef HAVE_GD_PNG
4230 		case PHP_GDIMG_TYPE_PNG:
4231 			im_org = gdImageCreateFromPng(org);
4232 			if (im_org == NULL) {
4233 				php_error_docref(NULL, E_WARNING, "Unable to open '%s' Not a valid PNG file", fn_dest);
4234                 fclose(org);
4235                 fclose(dest);
4236 				RETURN_FALSE;
4237 			}
4238 			break;
4239 #endif /* HAVE_GD_PNG */
4240 
4241 		default:
4242 			php_error_docref(NULL, E_WARNING, "Format not supported");
4243             fclose(org);
4244             fclose(dest);
4245 			RETURN_FALSE;
4246 			break;
4247 	}
4248 
4249 	fclose(org);
4250 
4251 	org_width  = gdImageSX (im_org);
4252 	org_height = gdImageSY (im_org);
4253 
4254 	x_ratio = (float) org_width / (float) dest_width;
4255 	y_ratio = (float) org_height / (float) dest_height;
4256 
4257 	if (x_ratio > 1 && y_ratio > 1) {
4258 		if (y_ratio > x_ratio) {
4259 			x_ratio = y_ratio;
4260 		} else {
4261 			y_ratio = x_ratio;
4262 		}
4263 		dest_width = (int) (org_width / x_ratio);
4264 		dest_height = (int) (org_height / y_ratio);
4265 	} else {
4266 		x_ratio = (float) dest_width / (float) org_width;
4267 		y_ratio = (float) dest_height / (float) org_height;
4268 
4269 		if (y_ratio < x_ratio) {
4270 			x_ratio = y_ratio;
4271 		} else {
4272 			y_ratio = x_ratio;
4273 		}
4274 		dest_width = (int) (org_width * x_ratio);
4275 		dest_height = (int) (org_height * y_ratio);
4276 	}
4277 
4278 	im_tmp = gdImageCreate (dest_width, dest_height);
4279 	if (im_tmp == NULL ) {
4280 		php_error_docref(NULL, E_WARNING, "Unable to allocate temporary buffer");
4281         fclose(dest);
4282         gdImageDestroy(im_org);
4283 		RETURN_FALSE;
4284 	}
4285 
4286 	gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, dest_height, org_width, org_height);
4287 
4288 	gdImageDestroy(im_org);
4289 
4290 	im_dest = gdImageCreate(dest_width, dest_height);
4291 	if (im_dest == NULL) {
4292 		php_error_docref(NULL, E_WARNING, "Unable to allocate destination buffer");
4293         fclose(dest);
4294         gdImageDestroy(im_tmp);
4295 		RETURN_FALSE;
4296 	}
4297 
4298 	white = gdImageColorAllocate(im_dest, 255, 255, 255);
4299 	if (white == -1) {
4300 		php_error_docref(NULL, E_WARNING, "Unable to allocate the colors for the destination buffer");
4301         fclose(dest);
4302         gdImageDestroy(im_tmp);
4303         gdImageDestroy(im_dest);
4304 		RETURN_FALSE;
4305 	}
4306 
4307 	black = gdImageColorAllocate(im_dest, 0, 0, 0);
4308 	if (black == -1) {
4309 		php_error_docref(NULL, E_WARNING, "Unable to allocate the colors for the destination buffer");
4310         fclose(dest);
4311         gdImageDestroy(im_tmp);
4312         gdImageDestroy(im_dest);
4313 		RETURN_FALSE;
4314 	}
4315 
4316 	int_threshold = int_threshold * 32;
4317 
4318 	for (y = 0; y < dest_height; y++) {
4319 		for (x = 0; x < dest_width; x++) {
4320 			color_org = gdImageGetPixel (im_tmp, x, y);
4321 			median = (im_tmp->red[color_org] + im_tmp->green[color_org] + im_tmp->blue[color_org]) / 3;
4322 			if (median < int_threshold) {
4323 				color = black;
4324 			} else {
4325 				color = white;
4326 			}
4327 			gdImageSetPixel (im_dest, x, y, color);
4328 		}
4329 	}
4330 
4331 	gdImageDestroy (im_tmp );
4332 
4333 	gdImageWBMP(im_dest, black , dest);
4334 
4335 	fflush(dest);
4336 	fclose(dest);
4337 
4338 	gdImageDestroy(im_dest);
4339 
4340 	RETURN_TRUE;
4341 }
4342 /* }}} */
4343 
4344 /* Section Filters */
4345 #define PHP_GD_SINGLE_RES	\
4346 	zval *SIM;	\
4347 	gdImagePtr im_src;	\
4348 	if (zend_parse_parameters(1, "r", &SIM) == FAILURE) {	\
4349 		RETURN_FALSE;	\
4350 	}	\
4351 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {	\
4352 		RETURN_FALSE;	\
4353 	}
4354 
php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)4355 static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
4356 {
4357 	PHP_GD_SINGLE_RES
4358 
4359 	if (gdImageNegate(im_src) == 1) {
4360 		RETURN_TRUE;
4361 	}
4362 
4363 	RETURN_FALSE;
4364 }
4365 
php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)4366 static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
4367 {
4368 	PHP_GD_SINGLE_RES
4369 
4370 	if (gdImageGrayScale(im_src) == 1) {
4371 		RETURN_TRUE;
4372 	}
4373 
4374 	RETURN_FALSE;
4375 }
4376 
php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)4377 static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
4378 {
4379 	zval *SIM;
4380 	gdImagePtr im_src;
4381 	zend_long brightness, tmp;
4382 
4383 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zll", &SIM, &tmp, &brightness) == FAILURE) {
4384 		RETURN_FALSE;
4385 	}
4386 
4387 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
4388 		RETURN_FALSE;
4389 	}
4390 
4391 	if (im_src == NULL) {
4392 		RETURN_FALSE;
4393 	}
4394 
4395 	if (gdImageBrightness(im_src, (int)brightness) == 1) {
4396 		RETURN_TRUE;
4397 	}
4398 
4399 	RETURN_FALSE;
4400 }
4401 
php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)4402 static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
4403 {
4404 	zval *SIM;
4405 	gdImagePtr im_src;
4406 	zend_long contrast, tmp;
4407 
4408 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &SIM, &tmp, &contrast) == FAILURE) {
4409 		RETURN_FALSE;
4410 	}
4411 
4412 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
4413 		RETURN_FALSE;
4414 	}
4415 
4416 	if (im_src == NULL) {
4417 		RETURN_FALSE;
4418 	}
4419 
4420 	if (gdImageContrast(im_src, (int)contrast) == 1) {
4421 		RETURN_TRUE;
4422 	}
4423 
4424 	RETURN_FALSE;
4425 }
4426 
php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)4427 static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
4428 {
4429 	zval *SIM;
4430 	gdImagePtr im_src;
4431 	zend_long r,g,b,tmp;
4432 	zend_long a = 0;
4433 
4434 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllll|l", &SIM, &tmp, &r, &g, &b, &a) == FAILURE) {
4435 		RETURN_FALSE;
4436 	}
4437 
4438 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
4439 		RETURN_FALSE;
4440 	}
4441 
4442 	if (im_src == NULL) {
4443 		RETURN_FALSE;
4444 	}
4445 
4446 	if (gdImageColor(im_src, (int) r, (int) g, (int) b, (int) a) == 1) {
4447 		RETURN_TRUE;
4448 	}
4449 
4450 	RETURN_FALSE;
4451 }
4452 
php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)4453 static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
4454 {
4455 	PHP_GD_SINGLE_RES
4456 
4457 	if (gdImageEdgeDetectQuick(im_src) == 1) {
4458 		RETURN_TRUE;
4459 	}
4460 
4461 	RETURN_FALSE;
4462 }
4463 
php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)4464 static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
4465 {
4466 	PHP_GD_SINGLE_RES
4467 
4468 	if (gdImageEmboss(im_src) == 1) {
4469 		RETURN_TRUE;
4470 	}
4471 
4472 	RETURN_FALSE;
4473 }
4474 
php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)4475 static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
4476 {
4477 	PHP_GD_SINGLE_RES
4478 
4479 	if (gdImageGaussianBlur(im_src) == 1) {
4480 		RETURN_TRUE;
4481 	}
4482 
4483 	RETURN_FALSE;
4484 }
4485 
php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)4486 static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
4487 {
4488 	PHP_GD_SINGLE_RES
4489 
4490 	if (gdImageSelectiveBlur(im_src) == 1) {
4491 		RETURN_TRUE;
4492 	}
4493 
4494 	RETURN_FALSE;
4495 }
4496 
php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)4497 static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
4498 {
4499 	PHP_GD_SINGLE_RES
4500 
4501 	if (gdImageMeanRemoval(im_src) == 1) {
4502 		RETURN_TRUE;
4503 	}
4504 
4505 	RETURN_FALSE;
4506 }
4507 
php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)4508 static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
4509 {
4510 	zval *SIM;
4511 	zend_long tmp;
4512 	gdImagePtr im_src;
4513 	double weight;
4514 
4515 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rld", &SIM, &tmp, &weight) == FAILURE) {
4516 		RETURN_FALSE;
4517 	}
4518 
4519 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
4520 		RETURN_FALSE;
4521 	}
4522 
4523 	if (im_src == NULL) {
4524 		RETURN_FALSE;
4525 	}
4526 
4527 	if (gdImageSmooth(im_src, (float)weight)==1) {
4528 		RETURN_TRUE;
4529 	}
4530 
4531 	RETURN_FALSE;
4532 }
4533 
php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)4534 static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)
4535 {
4536 	zval *IM;
4537 	gdImagePtr im;
4538 	zend_long tmp, blocksize;
4539 	zend_bool mode = 0;
4540 
4541 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll|b", &IM, &tmp, &blocksize, &mode) == FAILURE) {
4542 		RETURN_FALSE;
4543 	}
4544 
4545 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4546 		RETURN_FALSE;
4547 	}
4548 
4549 	if (im == NULL) {
4550 		RETURN_FALSE;
4551 	}
4552 
4553 	if (gdImagePixelate(im, (int) blocksize, (const unsigned int) mode)) {
4554 		RETURN_TRUE;
4555 	}
4556 
4557 	RETURN_FALSE;
4558 }
4559 
4560 /* {{{ proto bool imagefilter(resource src_im, int filtertype[, int arg1 [, int arg2 [, int arg3 [, int arg4 ]]]] )
4561    Applies Filter an image using a custom angle */
PHP_FUNCTION(imagefilter)4562 PHP_FUNCTION(imagefilter)
4563 {
4564 	zval *tmp;
4565 
4566 	typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
4567 	zend_long filtertype;
4568 	image_filter filters[] =
4569 	{
4570 		php_image_filter_negate ,
4571 		php_image_filter_grayscale,
4572 		php_image_filter_brightness,
4573 		php_image_filter_contrast,
4574 		php_image_filter_colorize,
4575 		php_image_filter_edgedetect,
4576 		php_image_filter_emboss,
4577 		php_image_filter_gaussian_blur,
4578 		php_image_filter_selective_blur,
4579 		php_image_filter_mean_removal,
4580 		php_image_filter_smooth,
4581 		php_image_filter_pixelate
4582 	};
4583 
4584 	if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > IMAGE_FILTER_MAX_ARGS) {
4585 		WRONG_PARAM_COUNT;
4586 	} else if (zend_parse_parameters(2, "rl", &tmp, &filtertype) == FAILURE) {
4587 		return;
4588 	}
4589 
4590 	if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
4591 		filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
4592 	}
4593 }
4594 /* }}} */
4595 
4596 /* {{{ proto resource imageconvolution(resource src_im, array matrix3x3, double div, double offset)
4597    Apply a 3x3 convolution matrix, using coefficient div and offset */
PHP_FUNCTION(imageconvolution)4598 PHP_FUNCTION(imageconvolution)
4599 {
4600 	zval *SIM, *hash_matrix;
4601 	zval *var = NULL, *var2 = NULL;
4602 	gdImagePtr im_src = NULL;
4603 	double div, offset;
4604 	int nelem, i, j, res;
4605 	float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
4606 
4607 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "radd", &SIM, &hash_matrix, &div, &offset) == FAILURE) {
4608 		RETURN_FALSE;
4609 	}
4610 
4611 	if ((im_src = (gdImagePtr)zend_fetch_resource(Z_RES_P(SIM), "Image", le_gd)) == NULL) {
4612 		RETURN_FALSE;
4613 	}
4614 
4615 	nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
4616 	if (nelem != 3) {
4617 		php_error_docref(NULL, E_WARNING, "You must have 3x3 array");
4618 		RETURN_FALSE;
4619 	}
4620 
4621 	for (i=0; i<3; i++) {
4622 		if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) {
4623 			if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) {
4624 				php_error_docref(NULL, E_WARNING, "You must have 3x3 array");
4625 				RETURN_FALSE;
4626 			}
4627 
4628 			for (j=0; j<3; j++) {
4629 				if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) {
4630 					matrix[i][j] = (float) zval_get_double(var2);
4631 				} else {
4632 					php_error_docref(NULL, E_WARNING, "You must have a 3x3 matrix");
4633 					RETURN_FALSE;
4634 				}
4635 			}
4636 		}
4637 	}
4638 	res = gdImageConvolution(im_src, matrix, (float)div, (float)offset);
4639 
4640 	if (res) {
4641 		RETURN_TRUE;
4642 	} else {
4643 		RETURN_FALSE;
4644 	}
4645 }
4646 /* }}} */
4647 /* End section: Filters */
4648 
4649 /* {{{ proto void imageflip(resource im, int mode)
4650    Flip an image (in place) horizontally, vertically or both directions. */
PHP_FUNCTION(imageflip)4651 PHP_FUNCTION(imageflip)
4652 {
4653 	zval *IM;
4654 	zend_long mode;
4655 	gdImagePtr im;
4656 
4657 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &IM, &mode) == FAILURE)  {
4658 		return;
4659 	}
4660 
4661 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4662 		RETURN_FALSE;
4663 	}
4664 
4665 	switch (mode) {
4666 		case GD_FLIP_VERTICAL:
4667 			gdImageFlipVertical(im);
4668 			break;
4669 
4670 		case GD_FLIP_HORINZONTAL:
4671 			gdImageFlipHorizontal(im);
4672 			break;
4673 
4674 		case GD_FLIP_BOTH:
4675 			gdImageFlipBoth(im);
4676 			break;
4677 
4678 		default:
4679 			php_error_docref(NULL, E_WARNING, "Unknown flip mode");
4680 			RETURN_FALSE;
4681 	}
4682 
4683 	RETURN_TRUE;
4684 }
4685 /* }}} */
4686 
4687 /* {{{ proto bool imageantialias(resource im, bool on)
4688    Should antialiased functions used or not*/
PHP_FUNCTION(imageantialias)4689 PHP_FUNCTION(imageantialias)
4690 {
4691 	zval *IM;
4692 	zend_bool alias;
4693 	gdImagePtr im;
4694 
4695 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rb", &IM, &alias) == FAILURE) {
4696 		return;
4697 	}
4698 
4699 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4700 		RETURN_FALSE;
4701 	}
4702 
4703 	if (im->trueColor) {
4704 		im->AA = alias;
4705 	}
4706 
4707 	RETURN_TRUE;
4708 }
4709 /* }}} */
4710 
4711 /* {{{ proto void imagecrop(resource im, array rect)
4712    Crop an image using the given coordinates and size, x, y, width and height. */
PHP_FUNCTION(imagecrop)4713 PHP_FUNCTION(imagecrop)
4714 {
4715 	zval *IM;
4716 	gdImagePtr im;
4717 	gdImagePtr im_crop;
4718 	gdRect rect;
4719 	zval *z_rect;
4720 	zval *tmp;
4721 
4722 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra", &IM, &z_rect) == FAILURE)  {
4723 		return;
4724 	}
4725 
4726 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4727 		RETURN_FALSE;
4728 	}
4729 
4730 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) {
4731 		rect.x = zval_get_long(tmp);
4732 	} else {
4733 		php_error_docref(NULL, E_WARNING, "Missing x position");
4734 		RETURN_FALSE;
4735 	}
4736 
4737 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
4738 		rect.y = zval_get_long(tmp);
4739 	} else {
4740 		php_error_docref(NULL, E_WARNING, "Missing y position");
4741 		RETURN_FALSE;
4742 	}
4743 
4744 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
4745 		rect.width = zval_get_long(tmp);
4746 	} else {
4747 		php_error_docref(NULL, E_WARNING, "Missing width");
4748 		RETURN_FALSE;
4749 	}
4750 
4751 	if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
4752 		rect.height = zval_get_long(tmp);
4753 	} else {
4754 		php_error_docref(NULL, E_WARNING, "Missing height");
4755 		RETURN_FALSE;
4756 	}
4757 
4758 	im_crop = gdImageCrop(im, &rect);
4759 
4760 	if (im_crop == NULL) {
4761 		RETURN_FALSE;
4762 	} else {
4763 		RETURN_RES(zend_register_resource(im_crop, le_gd));
4764 	}
4765 }
4766 /* }}} */
4767 
4768 /* {{{ proto void imagecropauto(resource im [, int mode [, float threshold [, int color]]])
4769    Crop an image automatically using one of the available modes. */
PHP_FUNCTION(imagecropauto)4770 PHP_FUNCTION(imagecropauto)
4771 {
4772 	zval *IM;
4773 	zend_long mode = -1;
4774 	zend_long color = -1;
4775 	double threshold = 0.5f;
4776 	gdImagePtr im;
4777 	gdImagePtr im_crop;
4778 
4779 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|ldl", &IM, &mode, &threshold, &color) == FAILURE)  {
4780 		return;
4781 	}
4782 
4783 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4784 		RETURN_FALSE;
4785 	}
4786 
4787 	switch (mode) {
4788 		case -1:
4789 			mode = GD_CROP_DEFAULT;
4790 		case GD_CROP_DEFAULT:
4791 		case GD_CROP_TRANSPARENT:
4792 		case GD_CROP_BLACK:
4793 		case GD_CROP_WHITE:
4794 		case GD_CROP_SIDES:
4795 			im_crop = gdImageCropAuto(im, mode);
4796 			break;
4797 
4798 		case GD_CROP_THRESHOLD:
4799 			if (color < 0 || (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im))) {
4800 				php_error_docref(NULL, E_WARNING, "Color argument missing with threshold mode");
4801 				RETURN_FALSE;
4802 			}
4803 			im_crop = gdImageCropThreshold(im, color, (float) threshold);
4804 			break;
4805 
4806 		default:
4807 			php_error_docref(NULL, E_WARNING, "Unknown crop mode");
4808 			RETURN_FALSE;
4809 	}
4810 	if (im_crop == NULL) {
4811 		RETURN_FALSE;
4812 	} else {
4813 		RETURN_RES(zend_register_resource(im_crop, le_gd));
4814 	}
4815 }
4816 /* }}} */
4817 
4818 /* {{{ proto resource imagescale(resource im, int new_width[, int new_height[, int method]])
4819    Scale an image using the given new width and height. */
PHP_FUNCTION(imagescale)4820 PHP_FUNCTION(imagescale)
4821 {
4822 	zval *IM;
4823 	gdImagePtr im;
4824 	gdImagePtr im_scaled = NULL;
4825 	int new_width, new_height;
4826 	zend_long tmp_w, tmp_h=-1, tmp_m = GD_BILINEAR_FIXED;
4827 	gdInterpolationMethod method, old_method;
4828 
4829 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|ll", &IM, &tmp_w, &tmp_h, &tmp_m) == FAILURE)  {
4830 		return;
4831 	}
4832 	method = tmp_m;
4833 
4834 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4835 		RETURN_FALSE;
4836 	}
4837 
4838 	if (tmp_h < 0) {
4839 		/* preserve ratio */
4840 		long src_x, src_y;
4841 
4842 		src_x = gdImageSX(im);
4843 		src_y = gdImageSY(im);
4844 		if (src_x) {
4845 			tmp_h = tmp_w * src_y / src_x;
4846 		}
4847 	}
4848 
4849 	if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) {
4850 		RETURN_FALSE;
4851 	}
4852 
4853 	new_width = tmp_w;
4854 	new_height = tmp_h;
4855 
4856 	/* gdImageGetInterpolationMethod() is only available as of GD 2.1.1 */
4857 	old_method = im->interpolation_id;
4858 	if (gdImageSetInterpolationMethod(im, method)) {
4859 		im_scaled = gdImageScale(im, new_width, new_height);
4860 	}
4861 	gdImageSetInterpolationMethod(im, old_method);
4862 
4863 	if (im_scaled == NULL) {
4864 		RETURN_FALSE;
4865 	} else {
4866 		RETURN_RES(zend_register_resource(im_scaled, le_gd));
4867 	}
4868 }
4869 /* }}} */
4870 
4871 /* {{{ proto resource imageaffine(resource src, array affine[, array clip])
4872    Return an image containing the affine tramsformed src image, using an optional clipping area */
PHP_FUNCTION(imageaffine)4873 PHP_FUNCTION(imageaffine)
4874 {
4875 	zval *IM;
4876 	gdImagePtr src;
4877 	gdImagePtr dst;
4878 	gdRect rect;
4879 	gdRectPtr pRect = NULL;
4880 	zval *z_rect = NULL;
4881 	zval *z_affine;
4882 	zval *tmp;
4883 	double affine[6];
4884 	int i, nelems;
4885 	zval *zval_affine_elem = NULL;
4886 
4887 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra|a", &IM, &z_affine, &z_rect) == FAILURE)  {
4888 		return;
4889 	}
4890 
4891 	if ((src = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
4892 		RETURN_FALSE;
4893 	}
4894 
4895 	if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
4896 		php_error_docref(NULL, E_WARNING, "Affine array must have six elements");
4897 		RETURN_FALSE;
4898 	}
4899 
4900 	for (i = 0; i < nelems; i++) {
4901 		if ((zval_affine_elem = zend_hash_index_find(Z_ARRVAL_P(z_affine), i)) != NULL) {
4902 			switch (Z_TYPE_P(zval_affine_elem)) {
4903 				case IS_LONG:
4904 					affine[i]  = Z_LVAL_P(zval_affine_elem);
4905 					break;
4906 				case IS_DOUBLE:
4907 					affine[i] = Z_DVAL_P(zval_affine_elem);
4908 					break;
4909 				case IS_STRING:
4910 					affine[i] = zval_get_double(zval_affine_elem);
4911 					break;
4912 				default:
4913 					php_error_docref(NULL, E_WARNING, "Invalid type for element %i", i);
4914 					RETURN_FALSE;
4915 			}
4916 		}
4917 	}
4918 
4919 	if (z_rect != NULL) {
4920 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") - 1)) != NULL) {
4921 			rect.x = zval_get_long(tmp);
4922 		} else {
4923 			php_error_docref(NULL, E_WARNING, "Missing x position");
4924 			RETURN_FALSE;
4925 		}
4926 
4927 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
4928 			rect.y = zval_get_long(tmp);
4929 		} else {
4930 			php_error_docref(NULL, E_WARNING, "Missing y position");
4931 			RETURN_FALSE;
4932 		}
4933 
4934 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
4935 			rect.width = zval_get_long(tmp);
4936 		} else {
4937 			php_error_docref(NULL, E_WARNING, "Missing width");
4938 			RETURN_FALSE;
4939 		}
4940 
4941 		if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
4942 			rect.height = zval_get_long(tmp);
4943 		} else {
4944 			php_error_docref(NULL, E_WARNING, "Missing height");
4945 			RETURN_FALSE;
4946 		}
4947 		pRect = &rect;
4948 	} else {
4949 		rect.x = -1;
4950 		rect.y = -1;
4951 		rect.width = gdImageSX(src);
4952 		rect.height = gdImageSY(src);
4953 		pRect = NULL;
4954 	}
4955 
4956 	if (gdTransformAffineGetImage(&dst, src, pRect, affine) != GD_TRUE) {
4957 		RETURN_FALSE;
4958 	}
4959 
4960 	if (dst == NULL) {
4961 		RETURN_FALSE;
4962 	} else {
4963 		RETURN_RES(zend_register_resource(dst, le_gd));
4964 	}
4965 }
4966 /* }}} */
4967 
4968 /* {{{ proto array imageaffinematrixget(int type[, array options])
4969    Return an image containing the affine tramsformed src image, using an optional clipping area */
PHP_FUNCTION(imageaffinematrixget)4970 PHP_FUNCTION(imageaffinematrixget)
4971 {
4972 	double affine[6];
4973 	zend_long type;
4974 	zval *options = NULL;
4975 	zval *tmp;
4976 	int res = GD_FALSE, i;
4977 
4978 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z", &type, &options) == FAILURE)  {
4979 		return;
4980 	}
4981 
4982 	switch((gdAffineStandardMatrix)type) {
4983 		case GD_AFFINE_TRANSLATE:
4984 		case GD_AFFINE_SCALE: {
4985 			double x, y;
4986 			if (!options || Z_TYPE_P(options) != IS_ARRAY) {
4987 				php_error_docref(NULL, E_WARNING, "Array expected as options");
4988 				RETURN_FALSE;
4989 			}
4990 			if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) {
4991 				x = zval_get_double(tmp);
4992 			} else {
4993 				php_error_docref(NULL, E_WARNING, "Missing x position");
4994 				RETURN_FALSE;
4995 			}
4996 
4997 			if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) {
4998 				y = zval_get_double(tmp);
4999 			} else {
5000 				php_error_docref(NULL, E_WARNING, "Missing y position");
5001 				RETURN_FALSE;
5002 			}
5003 
5004 			if (type == GD_AFFINE_TRANSLATE) {
5005 				res = gdAffineTranslate(affine, x, y);
5006 			} else {
5007 				res = gdAffineScale(affine, x, y);
5008 			}
5009 			break;
5010 		}
5011 
5012 		case GD_AFFINE_ROTATE:
5013 		case GD_AFFINE_SHEAR_HORIZONTAL:
5014 		case GD_AFFINE_SHEAR_VERTICAL: {
5015 			double angle;
5016 
5017 			if (!options) {
5018 				php_error_docref(NULL, E_WARNING, "Number is expected as option");
5019 				RETURN_FALSE;
5020 			}
5021 
5022 			angle = zval_get_double(options);
5023 
5024 			if (type == GD_AFFINE_SHEAR_HORIZONTAL) {
5025 				res = gdAffineShearHorizontal(affine, angle);
5026 			} else if (type == GD_AFFINE_SHEAR_VERTICAL) {
5027 				res = gdAffineShearVertical(affine, angle);
5028 			} else {
5029 				res = gdAffineRotate(affine, angle);
5030 			}
5031 			break;
5032 		}
5033 
5034 		default:
5035 			php_error_docref(NULL, E_WARNING, "Invalid type for element " ZEND_LONG_FMT, type);
5036 			RETURN_FALSE;
5037 	}
5038 
5039 	if (res == GD_FALSE) {
5040 		RETURN_FALSE;
5041 	} else {
5042 		array_init(return_value);
5043 		for (i = 0; i < 6; i++) {
5044 			add_index_double(return_value, i, affine[i]);
5045 		}
5046 	}
5047 } /* }}} */
5048 
5049 /* {{{ proto array imageaffineconcat(array m1, array m2)
5050    Concat two matrices (as in doing many ops in one go) */
PHP_FUNCTION(imageaffinematrixconcat)5051 PHP_FUNCTION(imageaffinematrixconcat)
5052 {
5053 	double m1[6];
5054 	double m2[6];
5055 	double mr[6];
5056 
5057 	zval *tmp;
5058 	zval *z_m1;
5059 	zval *z_m2;
5060 	int i, nelems;
5061 
5062 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &z_m1, &z_m2) == FAILURE)  {
5063 		return;
5064 	}
5065 
5066 	if (((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m1))) != 6) || (nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m2))) != 6) {
5067 		php_error_docref(NULL, E_WARNING, "Affine arrays must have six elements");
5068 		RETURN_FALSE;
5069 	}
5070 
5071 	for (i = 0; i < 6; i++) {
5072 		if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m1), i)) != NULL) {
5073 			switch (Z_TYPE_P(tmp)) {
5074 				case IS_LONG:
5075 					m1[i]  = Z_LVAL_P(tmp);
5076 					break;
5077 				case IS_DOUBLE:
5078 					m1[i] = Z_DVAL_P(tmp);
5079 					break;
5080 				case IS_STRING:
5081 					m1[i] = zval_get_double(tmp);
5082 					break;
5083 				default:
5084 					php_error_docref(NULL, E_WARNING, "Invalid type for element %i", i);
5085 					RETURN_FALSE;
5086 			}
5087 		}
5088 		if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m2), i)) != NULL) {
5089 			switch (Z_TYPE_P(tmp)) {
5090 				case IS_LONG:
5091 					m2[i]  = Z_LVAL_P(tmp);
5092 					break;
5093 				case IS_DOUBLE:
5094 					m2[i] = Z_DVAL_P(tmp);
5095 					break;
5096 				case IS_STRING:
5097 					m2[i] = zval_get_double(tmp);
5098 					break;
5099 				default:
5100 					php_error_docref(NULL, E_WARNING, "Invalid type for element %i", i);
5101 					RETURN_FALSE;
5102 			}
5103 		}
5104 	}
5105 
5106 	if (gdAffineConcat (mr, m1, m2) != GD_TRUE) {
5107 		RETURN_FALSE;
5108 	}
5109 
5110 	array_init(return_value);
5111 	for (i = 0; i < 6; i++) {
5112 		add_index_double(return_value, i, mr[i]);
5113 	}
5114 } /* }}} */
5115 
5116 /* {{{ proto resource imagesetinterpolation(resource im [, int method]])
5117    Set the default interpolation method, passing -1 or 0 sets it to the libgd default (bilinear). */
PHP_FUNCTION(imagesetinterpolation)5118 PHP_FUNCTION(imagesetinterpolation)
5119 {
5120 	zval *IM;
5121 	gdImagePtr im;
5122 	zend_long method = GD_BILINEAR_FIXED;
5123 
5124 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &IM, &method) == FAILURE)  {
5125 		return;
5126 	}
5127 
5128 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
5129 		RETURN_FALSE;
5130 	}
5131 
5132 	if (method == -1) {
5133 		 method = GD_BILINEAR_FIXED;
5134 	}
5135 	RETURN_BOOL(gdImageSetInterpolationMethod(im, (gdInterpolationMethod) method));
5136 }
5137 /* }}} */
5138 
5139 /* {{{ proto array imageresolution(resource im [, res_x, [res_y]])
5140    Get or set the resolution of the image in DPI. */
PHP_FUNCTION(imageresolution)5141 PHP_FUNCTION(imageresolution)
5142 {
5143 	zval *IM;
5144 	gdImagePtr im;
5145 	zend_long res_x = GD_RESOLUTION, res_y = GD_RESOLUTION;
5146 
5147 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &IM, &res_x, &res_y) == FAILURE)  {
5148 		return;
5149 	}
5150 
5151 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) {
5152 		RETURN_FALSE;
5153 	}
5154 
5155 	switch (ZEND_NUM_ARGS()) {
5156 		case 3:
5157 			gdImageSetResolution(im, res_x, res_y);
5158 			RETURN_TRUE;
5159 		case 2:
5160 			gdImageSetResolution(im, res_x, res_x);
5161 			RETURN_TRUE;
5162 		default:
5163 			array_init(return_value);
5164 			add_next_index_long(return_value, gdImageResolutionX(im));
5165 			add_next_index_long(return_value, gdImageResolutionY(im));
5166 	}
5167 }
5168 /* }}} */
5169 
5170 /*
5171  * Local variables:
5172  * tab-width: 4
5173  * c-basic-offset: 4
5174  * End:
5175  * vim600: sw=4 ts=4 fdm=marker
5176  * vim<600: sw=4 ts=4
5177  */
5178