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: 1123 1635 68.7 %
Date: 2016-09-27 Functions: 123 130 94.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:25:56 +0000 (2 days ago)

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