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