PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/gd - gd.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1053 1572 67.0 %
Date: 2014-07-21 Functions: 118 128 92.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:09 +0000 (13 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.