PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - gd - gd.c
Test: PHP Code Coverage
Date: 2009-11-23 Instrumented lines: 1200
Code covered: 74.6 % Executed lines: 895
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Mon, 23 Nov 2009 17:39:30 +0000 (33 hours ago)

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