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