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