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

Generated by: LCOV version 1.10

Generated at Sat, 30 Jul 2016 22:07:22 +0000 (9 hours ago)

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