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: 1041 1510 68.9 %
Date: 2015-01-18 Functions: 119 128 93.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Jan 2015 09:00:39 +0000 (7 days ago)

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