root/ext/gd/gd.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ZEND_GET_MODULE
  2. php_free_gd_font
  3. php_gd_error_method
  4. PHP_MSHUTDOWN_FUNCTION
  5. PHP_MINIT_FUNCTION
  6. PHP_RSHUTDOWN_FUNCTION
  7. PHP_MINFO_FUNCTION
  8. PHP_FUNCTION
  9. phpi_get_le_gd
  10. PHP_FUNCTION
  11. PHP_FUNCTION
  12. PHP_FUNCTION
  13. PHP_FUNCTION
  14. PHP_FUNCTION
  15. PHP_FUNCTION
  16. PHP_FUNCTION
  17. PHP_FUNCTION
  18. PHP_FUNCTION
  19. PHP_FUNCTION
  20. PHP_FUNCTION
  21. PHP_FUNCTION
  22. PHP_FUNCTION
  23. PHP_FUNCTION
  24. PHP_FUNCTION
  25. PHP_FUNCTION
  26. PHP_FUNCTION
  27. PHP_FUNCTION
  28. PHP_FUNCTION
  29. PHP_FUNCTION
  30. PHP_FUNCTION
  31. PHP_FUNCTION
  32. PHP_FUNCTION
  33. PHP_FUNCTION
  34. PHP_FUNCTION
  35. _php_ctx_getmbi
  36. _php_image_type
  37. _php_image_create_from_string
  38. PHP_FUNCTION
  39. _php_image_create_from
  40. PHP_FUNCTION
  41. PHP_FUNCTION
  42. PHP_FUNCTION
  43. PHP_FUNCTION
  44. PHP_FUNCTION
  45. PHP_FUNCTION
  46. PHP_FUNCTION
  47. PHP_FUNCTION
  48. PHP_FUNCTION
  49. PHP_FUNCTION
  50. _php_image_output
  51. PHP_FUNCTION
  52. PHP_FUNCTION
  53. PHP_FUNCTION
  54. PHP_FUNCTION
  55. PHP_FUNCTION
  56. PHP_FUNCTION
  57. PHP_FUNCTION
  58. PHP_FUNCTION
  59. PHP_FUNCTION
  60. PHP_FUNCTION
  61. PHP_FUNCTION
  62. PHP_FUNCTION
  63. PHP_FUNCTION
  64. PHP_FUNCTION
  65. PHP_FUNCTION
  66. PHP_FUNCTION
  67. PHP_FUNCTION
  68. PHP_FUNCTION
  69. PHP_FUNCTION
  70. PHP_FUNCTION
  71. PHP_FUNCTION
  72. PHP_FUNCTION
  73. PHP_FUNCTION
  74. PHP_FUNCTION
  75. PHP_FUNCTION
  76. PHP_FUNCTION
  77. PHP_FUNCTION
  78. PHP_FUNCTION
  79. PHP_FUNCTION
  80. PHP_FUNCTION
  81. PHP_FUNCTION
  82. PHP_FUNCTION
  83. php_imagepolygon
  84. PHP_FUNCTION
  85. PHP_FUNCTION
  86. php_find_gd_font
  87. php_imagefontsize
  88. PHP_FUNCTION
  89. PHP_FUNCTION
  90. php_gdimagecharup
  91. php_imagechar
  92. PHP_FUNCTION
  93. PHP_FUNCTION
  94. PHP_FUNCTION
  95. PHP_FUNCTION
  96. PHP_FUNCTION
  97. PHP_FUNCTION
  98. PHP_FUNCTION
  99. PHP_FUNCTION
  100. PHP_FUNCTION
  101. PHP_FUNCTION
  102. PHP_FUNCTION
  103. PHP_FUNCTION
  104. PHP_FUNCTION
  105. PHP_FUNCTION
  106. php_imagettftext_common
  107. php_free_ps_font
  108. php_free_ps_enc
  109. PHP_FUNCTION
  110. PHP_FUNCTION
  111. PHP_FUNCTION
  112. PHP_FUNCTION
  113. PHP_FUNCTION
  114. PHP_FUNCTION
  115. PHP_FUNCTION
  116. PHP_FUNCTION
  117. PHP_FUNCTION
  118. PHP_FUNCTION
  119. _php_image_bw_convert
  120. _php_image_convert
  121. php_image_filter_negate
  122. php_image_filter_grayscale
  123. php_image_filter_brightness
  124. php_image_filter_contrast
  125. php_image_filter_colorize
  126. php_image_filter_edgedetect
  127. php_image_filter_emboss
  128. php_image_filter_gaussian_blur
  129. php_image_filter_selective_blur
  130. php_image_filter_mean_removal
  131. php_image_filter_smooth
  132. php_image_filter_pixelate
  133. PHP_FUNCTION
  134. PHP_FUNCTION
  135. PHP_FUNCTION
  136. PHP_FUNCTION
  137. PHP_FUNCTION
  138. PHP_FUNCTION
  139. PHP_FUNCTION
  140. PHP_FUNCTION
  141. PHP_FUNCTION
  142. PHP_FUNCTION
  143. PHP_FUNCTION

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

/* [<][>][^][v][top][bottom][index][help] */