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