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: 1062 1585 67.0 %
Date: 2015-03-20 Functions: 119 128 93.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 20 Mar 2015 23:40:26 +0000 (6 days ago)

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