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: 1246 1766 70.6 %
Date: 2022-01-21 Functions: 130 135 96.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 21 Jan 2022 14:15:10 +0000 (29 hours ago)

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