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