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: 1094 1592 68.7 %
Date: 2016-08-24 Functions: 121 128 94.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:20 +0000 (3 days ago)

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