2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 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_0.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
+----------------------------------------------------------------------+
21
/* $Id: gd.c,v 1.294.2.12 2005/05/06 16:49:04 tony2001 Exp $ */
23
/* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center,
24
Cold Spring Harbor Labs. */
26
/* Note that there is no code from the gd package in this file */
33
#include "ext/standard/head.h"
37
#include "ext/standard/info.h"
38
#include "php_open_temporary_file.h"
41
# include <sys/wait.h>
53
static int le_gd, le_gd_font;
56
static int le_ps_font, le_ps_enc;
57
static void php_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC);
58
static void php_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC);
62
#include <gdfontt.h> /* 1 Tiny font */
63
#include <gdfonts.h> /* 2 Small font */
64
#include <gdfontmb.h> /* 3 Medium bold font */
65
#include <gdfontl.h> /* 4 Large font */
66
#include <gdfontg.h> /* 5 Giant font */
68
#include "libgd/wbmp.h"
71
# ifdef HAVE_LIBFREETYPE
72
# include <ft2build.h>
73
# include FT_FREETYPE_H
76
# include <freetype.h>
83
#define M_PI 3.14159265358979323846
87
static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int, int);
91
/* it's >= 1.5, i.e. has IOCtx */
92
#define USE_GD_IOCTX 1
100
#define gdImageCreateFromGdCtx NULL
101
#define gdImageCreateFromGd2Ctx NULL
102
#define gdImageCreateFromGd2partCtx NULL
103
#define gdImageCreateFromGifCtx NULL
104
#define gdImageCreateFromJpegCtx NULL
105
#define gdImageCreateFromPngCtx NULL
106
#define gdImageCreateFromWBMPCtx NULL
107
typedef FILE gdIOCtx;
108
#define CTX_PUTC(c, fp) fputc(c, fp)
111
#ifndef HAVE_GDIMAGECOLORRESOLVE
112
extern int gdImageColorResolve(gdImagePtr, int, int, int);
115
#if HAVE_COLORCLOSESTHWB
116
int gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b);
119
#ifndef HAVE_GD_DYNAMIC_CTX_EX
120
#define gdNewDynamicCtxEx(len, data, val) gdNewDynamicCtx(len, data)
123
/* Section Filters Declarations */
124
/* IMPORTANT NOTE FOR NEW FILTER
125
* Do not forget to update:
126
* IMAGE_FILTER_MAX: define the last filter index
127
* IMAGE_FILTER_MAX_ARGS: define the biggest amout of arguments
128
* image_filter array in PHP_FUNCTION(imagefilter)
131
#define IMAGE_FILTER_NEGATE 0
132
#define IMAGE_FILTER_GRAYSCALE 1
133
#define IMAGE_FILTER_BRIGHTNESS 2
134
#define IMAGE_FILTER_CONTRAST 3
135
#define IMAGE_FILTER_COLORIZE 4
136
#define IMAGE_FILTER_EDGEDETECT 5
137
#define IMAGE_FILTER_EMBOSS 6
138
#define IMAGE_FILTER_GAUSSIAN_BLUR 7
139
#define IMAGE_FILTER_SELECTIVE_BLUR 8
140
#define IMAGE_FILTER_MEAN_REMOVAL 9
141
#define IMAGE_FILTER_SMOOTH 10
142
#define IMAGE_FILTER_MAX 10
143
#define IMAGE_FILTER_MAX_ARGS 5
144
static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
145
static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
146
static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
147
static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
148
static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
149
static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
150
static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
151
static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
152
static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
153
static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
154
static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
156
/* End Section filters declarations */
157
static gdImagePtr _php_image_create_from_string (zval **Data, char *tn, gdImagePtr (*ioctx_func_p)() TSRMLS_DC);
158
static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)());
159
static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)());
160
static int _php_image_type(char data[8]);
161
static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type);
162
static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold);
164
/* {{{ gd_functions[]
166
function_entry gd_functions[] = {
167
PHP_FE(gd_info, NULL)
168
PHP_FE(imagearc, NULL)
169
PHP_FE(imageellipse, NULL)
170
PHP_FE(imagechar, NULL)
171
PHP_FE(imagecharup, NULL)
172
PHP_FE(imagecolorat, NULL)
173
PHP_FE(imagecolorallocate, NULL)
175
PHP_FE(imagepalettecopy, NULL)
176
PHP_FE(imagecreatefromstring, NULL)
178
PHP_FE(imagecolorclosest, NULL)
179
#if HAVE_COLORCLOSESTHWB
180
PHP_FE(imagecolorclosesthwb, NULL)
182
PHP_FE(imagecolordeallocate, NULL)
183
PHP_FE(imagecolorresolve, NULL)
184
PHP_FE(imagecolorexact, NULL)
185
PHP_FE(imagecolorset, NULL)
186
PHP_FE(imagecolortransparent, NULL)
187
PHP_FE(imagecolorstotal, NULL)
188
PHP_FE(imagecolorsforindex, NULL)
189
PHP_FE(imagecopy, NULL)
191
PHP_FE(imagecopymerge, NULL)
192
PHP_FE(imagecopymergegray, NULL)
194
PHP_FE(imagecopyresized, NULL)
195
PHP_FE(imagecreate, NULL)
197
PHP_FE(imagecreatetruecolor, NULL)
198
PHP_FE(imageistruecolor, NULL)
199
PHP_FE(imagetruecolortopalette, NULL)
200
PHP_FE(imagesetthickness, NULL)
201
PHP_FE(imagefilledarc, NULL)
202
PHP_FE(imagefilledellipse, NULL)
203
PHP_FE(imagealphablending, NULL)
204
PHP_FE(imagesavealpha, NULL)
205
PHP_FE(imagecolorallocatealpha, NULL)
206
PHP_FE(imagecolorresolvealpha, NULL)
207
PHP_FE(imagecolorclosestalpha, NULL)
208
PHP_FE(imagecolorexactalpha, NULL)
209
PHP_FE(imagecopyresampled, NULL)
212
#ifdef HAVE_GD_BUNDLED
213
PHP_FE(imagerotate, NULL)
214
PHP_FE(imageantialias, NULL)
217
#if HAVE_GD_IMAGESETTILE
218
PHP_FE(imagesettile, NULL)
221
#if HAVE_GD_IMAGESETBRUSH
222
PHP_FE(imagesetbrush, NULL)
225
PHP_FE(imagesetstyle, NULL)
228
PHP_FE(imagecreatefrompng, NULL)
230
#ifdef HAVE_GD_GIF_READ
231
PHP_FE(imagecreatefromgif, NULL)
234
PHP_FE(imagecreatefromjpeg, NULL)
237
PHP_FE(imagecreatefromwbmp, NULL)
240
PHP_FE(imagecreatefromxbm, NULL)
242
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
243
PHP_FE(imagecreatefromxpm, NULL)
245
PHP_FE(imagecreatefromgd, NULL)
247
PHP_FE(imagecreatefromgd2, NULL)
248
PHP_FE(imagecreatefromgd2part, NULL)
251
PHP_FE(imagepng, NULL)
253
#ifdef HAVE_GD_GIF_CREATE
254
PHP_FE(imagegif, NULL)
257
PHP_FE(imagejpeg, NULL)
260
PHP_FE(imagewbmp, NULL)
262
PHP_FE(imagegd, NULL)
264
PHP_FE(imagegd2, NULL)
267
PHP_FE(imagedestroy, NULL)
268
PHP_FE(imagegammacorrect, NULL)
269
PHP_FE(imagefill, NULL)
270
PHP_FE(imagefilledpolygon, NULL)
271
PHP_FE(imagefilledrectangle, NULL)
272
PHP_FE(imagefilltoborder, NULL)
273
PHP_FE(imagefontwidth, NULL)
274
PHP_FE(imagefontheight, NULL)
275
PHP_FE(imageinterlace, NULL)
276
PHP_FE(imageline, NULL)
277
PHP_FE(imageloadfont, NULL)
278
PHP_FE(imagepolygon, NULL)
279
PHP_FE(imagerectangle, NULL)
280
PHP_FE(imagesetpixel, NULL)
281
PHP_FE(imagestring, NULL)
282
PHP_FE(imagestringup, NULL)
283
PHP_FE(imagesx, NULL)
284
PHP_FE(imagesy, NULL)
285
PHP_FE(imagedashedline, NULL)
288
PHP_FE(imagettfbbox, NULL)
289
PHP_FE(imagettftext, NULL)
290
#if HAVE_LIBGD20 && HAVE_LIBFREETYPE && HAVE_GD_STRINGFTEX
291
PHP_FE(imageftbbox, NULL)
292
PHP_FE(imagefttext, NULL)
297
PHP_FE(imagepsloadfont, NULL)
299
PHP_FE(imagepscopyfont, NULL)
301
PHP_FE(imagepsfreefont, NULL)
302
PHP_FE(imagepsencodefont, NULL)
303
PHP_FE(imagepsextendfont, NULL)
304
PHP_FE(imagepsslantfont, NULL)
305
PHP_FE(imagepstext, NULL)
306
PHP_FE(imagepsbbox, NULL)
308
PHP_FE(imagetypes, NULL)
310
#if defined(HAVE_GD_JPG) && defined(HAVE_GD_WBMP)
311
PHP_FE(jpeg2wbmp, NULL)
313
#if defined(HAVE_GD_PNG) && defined(HAVE_GD_WBMP)
314
PHP_FE(png2wbmp, NULL)
317
PHP_FE(image2wbmp, NULL)
320
PHP_FE(imagelayereffect, NULL)
321
PHP_FE(imagecolormatch, NULL)
322
PHP_FE(imagexbm, NULL)
325
#ifdef HAVE_GD_BUNDLED
326
PHP_FE(imagefilter, NULL)
333
zend_module_entry gd_module_entry = {
334
STANDARD_MODULE_HEADER,
340
#if HAVE_LIBGD20 && HAVE_GD_STRINGFT && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE)
347
STANDARD_MODULE_PROPERTIES
354
/* {{{ php_free_gd_image
356
static void php_free_gd_image(zend_rsrc_list_entry *rsrc TSRMLS_DC)
358
gdImageDestroy((gdImagePtr) rsrc->ptr);
362
/* {{{ php_free_gd_font
364
static void php_free_gd_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
366
gdFontPtr fp = (gdFontPtr) rsrc->ptr;
376
/* {{{ PHP_MSHUTDOWN_FUNCTION
378
PHP_MSHUTDOWN_FUNCTION(gd)
388
/* {{{ PHP_MINIT_FUNCTION
390
PHP_MINIT_FUNCTION(gd)
392
le_gd = zend_register_list_destructors_ex(php_free_gd_image, NULL, "gd", module_number);
393
le_gd_font = zend_register_list_destructors_ex(php_free_gd_font, NULL, "gd font", module_number);
396
T1_InitLib(NO_LOGFILE | IGNORE_CONFIGFILE | IGNORE_FONTDATABASE);
397
T1_SetLogLevel(T1LOG_DEBUG);
398
le_ps_font = zend_register_list_destructors_ex(php_free_ps_font, NULL, "gd PS font", module_number);
399
le_ps_enc = zend_register_list_destructors_ex(php_free_ps_enc, NULL, "gd PS encoding", module_number);
402
REGISTER_LONG_CONSTANT("IMG_GIF", 1, CONST_CS | CONST_PERSISTENT);
403
REGISTER_LONG_CONSTANT("IMG_JPG", 2, CONST_CS | CONST_PERSISTENT);
404
REGISTER_LONG_CONSTANT("IMG_JPEG", 2, CONST_CS | CONST_PERSISTENT);
405
REGISTER_LONG_CONSTANT("IMG_PNG", 4, CONST_CS | CONST_PERSISTENT);
406
REGISTER_LONG_CONSTANT("IMG_WBMP", 8, CONST_CS | CONST_PERSISTENT);
407
REGISTER_LONG_CONSTANT("IMG_XPM", 16, CONST_CS | CONST_PERSISTENT);
409
/* special colours for gd */
410
REGISTER_LONG_CONSTANT("IMG_COLOR_TILED", gdTiled, CONST_CS | CONST_PERSISTENT);
411
REGISTER_LONG_CONSTANT("IMG_COLOR_STYLED", gdStyled, CONST_CS | CONST_PERSISTENT);
412
REGISTER_LONG_CONSTANT("IMG_COLOR_BRUSHED", gdBrushed, CONST_CS | CONST_PERSISTENT);
413
REGISTER_LONG_CONSTANT("IMG_COLOR_STYLEDBRUSHED", gdStyledBrushed, CONST_CS | CONST_PERSISTENT);
414
REGISTER_LONG_CONSTANT("IMG_COLOR_TRANSPARENT", gdTransparent, CONST_CS | CONST_PERSISTENT);
417
/* for imagefilledarc */
418
REGISTER_LONG_CONSTANT("IMG_ARC_ROUNDED", gdArc, CONST_CS | CONST_PERSISTENT);
419
REGISTER_LONG_CONSTANT("IMG_ARC_PIE", gdPie, CONST_CS | CONST_PERSISTENT);
420
REGISTER_LONG_CONSTANT("IMG_ARC_CHORD", gdChord, CONST_CS | CONST_PERSISTENT);
421
REGISTER_LONG_CONSTANT("IMG_ARC_NOFILL", gdNoFill, CONST_CS | CONST_PERSISTENT);
422
REGISTER_LONG_CONSTANT("IMG_ARC_EDGED", gdEdged, CONST_CS | CONST_PERSISTENT);
424
/* GD2 image format types */
426
REGISTER_LONG_CONSTANT("IMG_GD2_RAW", GD2_FMT_RAW, CONST_CS | CONST_PERSISTENT);
428
#ifdef GD2_FMT_COMPRESSED
429
REGISTER_LONG_CONSTANT("IMG_GD2_COMPRESSED", GD2_FMT_COMPRESSED, CONST_CS | CONST_PERSISTENT);
432
REGISTER_LONG_CONSTANT("IMG_EFFECT_REPLACE", gdEffectReplace, CONST_CS | CONST_PERSISTENT);
433
REGISTER_LONG_CONSTANT("IMG_EFFECT_ALPHABLEND", gdEffectAlphaBlend, CONST_CS | CONST_PERSISTENT);
434
REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
435
REGISTER_LONG_CONSTANT("IMG_EFFECT_OVERLAY", gdEffectOverlay, CONST_CS | CONST_PERSISTENT);
436
REGISTER_LONG_CONSTANT("GD_BUNDLED", 1, CONST_CS | CONST_PERSISTENT);
437
/* Section Filters */
438
REGISTER_LONG_CONSTANT("IMG_FILTER_NEGATE", IMAGE_FILTER_NEGATE, CONST_CS | CONST_PERSISTENT);
439
REGISTER_LONG_CONSTANT("IMG_FILTER_GRAYSCALE", IMAGE_FILTER_GRAYSCALE, CONST_CS | CONST_PERSISTENT);
440
REGISTER_LONG_CONSTANT("IMG_FILTER_BRIGHTNESS", IMAGE_FILTER_BRIGHTNESS, CONST_CS | CONST_PERSISTENT);
441
REGISTER_LONG_CONSTANT("IMG_FILTER_CONTRAST", IMAGE_FILTER_CONTRAST, CONST_CS | CONST_PERSISTENT);
442
REGISTER_LONG_CONSTANT("IMG_FILTER_COLORIZE", IMAGE_FILTER_COLORIZE, CONST_CS | CONST_PERSISTENT);
443
REGISTER_LONG_CONSTANT("IMG_FILTER_EDGEDETECT", IMAGE_FILTER_EDGEDETECT, CONST_CS | CONST_PERSISTENT);
444
REGISTER_LONG_CONSTANT("IMG_FILTER_GAUSSIAN_BLUR", IMAGE_FILTER_GAUSSIAN_BLUR, CONST_CS | CONST_PERSISTENT);
445
REGISTER_LONG_CONSTANT("IMG_FILTER_SELECTIVE_BLUR", IMAGE_FILTER_SELECTIVE_BLUR, CONST_CS | CONST_PERSISTENT);
446
REGISTER_LONG_CONSTANT("IMG_FILTER_EMBOSS", IMAGE_FILTER_EMBOSS, CONST_CS | CONST_PERSISTENT);
447
REGISTER_LONG_CONSTANT("IMG_FILTER_MEAN_REMOVAL", IMAGE_FILTER_MEAN_REMOVAL, CONST_CS | CONST_PERSISTENT);
448
REGISTER_LONG_CONSTANT("IMG_FILTER_SMOOTH", IMAGE_FILTER_SMOOTH, CONST_CS | CONST_PERSISTENT);
449
/* End Section Filters */
451
REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
457
/* {{{ PHP_RSHUTDOWN_FUNCTION
459
#if HAVE_LIBGD20 && HAVE_GD_STRINGFT && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE)
460
PHP_RSHUTDOWN_FUNCTION(gd)
462
#if HAVE_GD_FONTCACHESHUTDOWN
463
gdFontCacheShutdown();
473
#define PHP_GD_VERSION_STRING "bundled (2.0.28 compatible)"
475
#define PHP_GD_VERSION_STRING "2.0 or higher"
476
#elif HAVE_GDIMAGECOLORRESOLVE
477
#define PHP_GD_VERSION_STRING "1.6.2 or higher"
479
#define PHP_GD_VERSION_STRING "between 1.3 and 1.6.1"
481
#define PHP_GD_VERSION_STRING "1.2"
484
/* {{{ PHP_MINFO_FUNCTION
486
PHP_MINFO_FUNCTION(gd)
488
php_info_print_table_start();
489
php_info_print_table_row(2, "GD Support", "enabled");
491
/* need to use a PHPAPI function here because it is external module in windows */
493
php_info_print_table_row(2, "GD Version", PHP_GD_VERSION_STRING);
496
php_info_print_table_row(2, "FreeType Support", "enabled");
498
php_info_print_table_row(2, "FreeType Linkage", "with freetype");
501
#ifdef FREETYPE_PATCH
502
snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
504
snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
506
php_info_print_table_row(2, "FreeType Version", tmp);
509
php_info_print_table_row(2, "FreeType Linkage", "with TTF library");
512
snprintf(tmp, sizeof(tmp), "%d.%d", TT_FREETYPE_MAJOR, TT_FREETYPE_MINOR);
513
php_info_print_table_row(2, "FreeType Version", tmp);
516
php_info_print_table_row(2, "FreeType Linkage", "with unknown library");
521
php_info_print_table_row(2, "T1Lib Support", "enabled");
524
/* this next part is stupid ... if I knew better, I'd put them all on one row (cmv) */
526
#ifdef HAVE_GD_GIF_READ
527
php_info_print_table_row(2, "GIF Read Support", "enabled");
529
#ifdef HAVE_GD_GIF_CREATE
530
php_info_print_table_row(2, "GIF Create Support", "enabled");
533
php_info_print_table_row(2, "JPG Support", "enabled");
536
php_info_print_table_row(2, "PNG Support", "enabled");
539
php_info_print_table_row(2, "WBMP Support", "enabled");
541
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
542
php_info_print_table_row(2, "XPM Support", "enabled");
545
php_info_print_table_row(2, "XBM Support", "enabled");
547
#if defined(USE_GD_JISX0208) && defined(HAVE_GD_BUNDLED)
548
php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
550
php_info_print_table_end();
554
/* {{{ proto array gd_info()
556
PHP_FUNCTION(gd_info)
558
if (ZEND_NUM_ARGS() != 0) {
559
ZEND_WRONG_PARAM_COUNT();
563
array_init(return_value);
565
add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING, 1);
568
add_assoc_bool(return_value, "FreeType Support", 1);
570
add_assoc_string(return_value, "FreeType Linkage", "with freetype", 1);
572
add_assoc_string(return_value, "FreeType Linkage", "with TTF library", 1);
574
add_assoc_string(return_value, "FreeType Linkage", "with unknown library", 1);
577
add_assoc_bool(return_value, "FreeType Support", 0);
581
add_assoc_bool(return_value, "T1Lib Support", 1);
583
add_assoc_bool(return_value, "T1Lib Support", 0);
585
#ifdef HAVE_GD_GIF_READ
586
add_assoc_bool(return_value, "GIF Read Support", 1);
588
add_assoc_bool(return_value, "GIF Read Support", 0);
590
#ifdef HAVE_GD_GIF_CREATE
591
add_assoc_bool(return_value, "GIF Create Support", 1);
593
add_assoc_bool(return_value, "GIF Create Support", 0);
596
add_assoc_bool(return_value, "JPG Support", 1);
598
add_assoc_bool(return_value, "JPG Support", 0);
601
add_assoc_bool(return_value, "PNG Support", 1);
603
add_assoc_bool(return_value, "PNG Support", 0);
606
add_assoc_bool(return_value, "WBMP Support", 1);
608
add_assoc_bool(return_value, "WBMP Support", 0);
610
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
611
add_assoc_bool(return_value, "XPM Support", 1);
613
add_assoc_bool(return_value, "XPM Support", 0);
616
add_assoc_bool(return_value, "XBM Support", 1);
618
add_assoc_bool(return_value, "XBM Support", 0);
620
#if defined(USE_GD_JISX0208) && defined(HAVE_GD_BUNDLED)
621
add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
623
add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
628
/* Need this for cpdf. See also comment in file.c php3i_get_le_fp() */
629
PHP_GD_API int phpi_get_le_gd(void)
634
#ifndef HAVE_GDIMAGECOLORRESOLVE
636
/* {{{ gdImageColorResolve
638
/********************************************************************/
639
/* gdImageColorResolve is a replacement for the old fragment: */
641
/* if ((color=gdImageColorExact(im,R,G,B)) < 0) */
642
/* if ((color=gdImageColorAllocate(im,R,G,B)) < 0) */
643
/* color=gdImageColorClosest(im,R,G,B); */
645
/* in a single function */
647
int gdImageColorResolve(gdImagePtr im, int r, int g, int b)
652
long rd, gd, bd, dist;
653
long mindist = 3*255*255; /* init to max poss dist */
655
for (c = 0; c < im->colorsTotal; c++) {
657
op = c; /* Save open slot */
658
continue; /* Color not in use */
660
rd = (long) (im->red [c] - r);
661
gd = (long) (im->green[c] - g);
662
bd = (long) (im->blue [c] - b);
663
dist = rd * rd + gd * gd + bd * bd;
664
if (dist < mindist) {
666
return c; /* Return exact match color */
672
/* no exact match. We now know closest, but first try to allocate exact */
674
op = im->colorsTotal;
675
if (op == gdMaxColors) { /* No room for more colors */
676
return ct; /* Return closest available color */
684
return op; /* Return newly allocated color */
690
#define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
692
/* {{{ proto int imageloadfont(string filename)
694
PHP_FUNCTION(imageloadfont)
697
int hdr_size = sizeof(gdFont) - sizeof(char *);
698
int ind, body_size, n = 0, b, i, body_size_check;
702
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
703
ZEND_WRONG_PARAM_COUNT();
706
convert_to_string_ex(file);
708
stream = php_stream_open_wrapper(Z_STRVAL_PP(file), "rb", IGNORE_PATH | IGNORE_URL_WIN | REPORT_ERRORS, NULL);
709
if (stream == NULL) {
713
/* Only supports a architecture-dependent binary dump format
715
* The file format is like this on machines with 32-byte integers:
717
* byte 0-3: (int) number of characters in the font
718
* byte 4-7: (int) value of first character in the font (often 32, space)
719
* byte 8-11: (int) pixel width of each character
720
* byte 12-15: (int) pixel height of each character
721
* bytes 16-: (char) array with character data, one byte per pixel
722
* in each character, for a total of
723
* (nchars*width*height) bytes.
725
font = (gdFontPtr) emalloc(sizeof(gdFont));
727
while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b))) {
733
if (php_stream_eof(stream)) {
734
php_error_docref(NULL TSRMLS_CC, E_WARNING, "End of file while reading header");
736
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading header");
738
php_stream_close(stream);
741
i = php_stream_tell(stream);
742
php_stream_seek(stream, 0, SEEK_END);
743
body_size_check = php_stream_tell(stream) - hdr_size;
744
php_stream_seek(stream, i, SEEK_SET);
746
body_size = font->w * font->h * font->nchars;
747
if (body_size != body_size_check) {
748
font->w = FLIPWORD(font->w);
749
font->h = FLIPWORD(font->h);
750
font->nchars = FLIPWORD(font->nchars);
751
body_size = font->w * font->h * font->nchars;
754
if (body_size != body_size_check) {
755
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font");
757
php_stream_close(stream);
761
font->data = emalloc(body_size);
763
while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b))) {
770
if (php_stream_eof(stream)) {
771
php_error_docref(NULL TSRMLS_CC, E_WARNING, "End of file while reading body");
773
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading body");
775
php_stream_close(stream);
778
php_stream_close(stream);
780
/* Adding 5 to the font index so we will never have font indices
781
* that overlap with the old fonts (with indices 1-5). The first
782
* list index given out is always 1.
784
ind = 5 + zend_list_insert(font, le_gd_font);
790
/* {{{ proto bool imagesetstyle(resource im, array styles)
791
Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
792
PHP_FUNCTION(imagesetstyle)
800
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &styles) == FAILURE) {
801
ZEND_WRONG_PARAM_COUNT();
804
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
806
convert_to_array_ex(styles);
808
/* copy the style values in the stylearr */
809
stylearr = safe_emalloc(sizeof(int), zend_hash_num_elements(HASH_OF(*styles)), 0);
811
zend_hash_internal_pointer_reset_ex(HASH_OF(*styles), &pos);
813
for (index = 0;; zend_hash_move_forward_ex(HASH_OF(*styles), &pos)) {
816
if (zend_hash_get_current_data_ex(HASH_OF(*styles), (void **) &item, &pos) == FAILURE) {
820
convert_to_long_ex(item);
822
stylearr[index++] = Z_LVAL_PP(item);
825
gdImageSetStyle(im, stylearr, index);
834
/* {{{ proto resource imagecreatetruecolor(int x_size, int y_size)
835
Create a new true color image */
836
PHP_FUNCTION(imagecreatetruecolor)
838
zval **x_size, **y_size;
841
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &x_size, &y_size) == FAILURE) {
842
ZEND_WRONG_PARAM_COUNT();
845
convert_to_long_ex(x_size);
846
convert_to_long_ex(y_size);
848
if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0) {
849
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
853
im = gdImageCreateTrueColor(Z_LVAL_PP(x_size), Z_LVAL_PP(y_size));
855
ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
859
/* {{{ proto bool imageistruecolor(resource im)
860
return true if the image uses truecolor */
861
PHP_FUNCTION(imageistruecolor)
866
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &IM) == FAILURE) {
867
ZEND_WRONG_PARAM_COUNT();
870
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
872
RETURN_BOOL(im->trueColor);
876
/* {{{ proto void imagetruecolortopalette(resource im, bool ditherFlag, int colorsWanted)
877
Convert a true colour image to a palette based image with a number of colours, optionally using dithering. */
878
PHP_FUNCTION(imagetruecolortopalette)
880
zval **IM, **dither, **ncolors;
883
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &IM, &dither, &ncolors) == FAILURE) {
884
ZEND_WRONG_PARAM_COUNT();
887
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
889
convert_to_boolean_ex(dither);
890
convert_to_long_ex(ncolors);
892
gdImageTrueColorToPalette(im, Z_LVAL_PP(dither), Z_LVAL_PP(ncolors));
899
/* {{{ proto bool imagecolormatch(resource im1, resource im2)
900
Makes the colors of the palette version of an image more closely match the true color version */
901
PHP_FUNCTION(imagecolormatch)
907
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM1, &IM2 ) == FAILURE) {
908
ZEND_WRONG_PARAM_COUNT();
911
ZEND_FETCH_RESOURCE(im1, gdImagePtr, IM1, -1, "Image", le_gd);
912
ZEND_FETCH_RESOURCE(im2, gdImagePtr, IM2, -1, "Image", le_gd);
914
result = gdImageColorMatch(im1, im2);
917
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image1 must be TrueColor" );
921
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image2 must be Palette" );
925
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image1 and Image2 must be the same size" );
935
/* {{{ proto bool imagesetthickness(resource im, int thickness)
936
Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
937
PHP_FUNCTION(imagesetthickness)
942
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &thick) == FAILURE) {
943
ZEND_WRONG_PARAM_COUNT();
946
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
947
convert_to_long_ex(thick);
949
gdImageSetThickness(im, Z_LVAL_PP(thick));
955
/* {{{ proto bool imagefilledellipse(resource im, int cx, int cy, int w, int h, int color)
957
PHP_FUNCTION(imagefilledellipse)
959
zval **IM, **cx, **cy, **w, **h, **color;
962
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &cx, &cy, &w, &h, &color) == FAILURE) {
963
ZEND_WRONG_PARAM_COUNT();
966
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
968
convert_to_long_ex(cx);
969
convert_to_long_ex(cy);
970
convert_to_long_ex(w);
971
convert_to_long_ex(h);
972
convert_to_long_ex(color);
974
gdImageFilledEllipse(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), Z_LVAL_PP(color));
980
/* {{{ proto bool imagefilledarc(resource im, int cx, int cy, int w, int h, int s, int e, int col, int style)
981
Draw a filled partial ellipse */
982
PHP_FUNCTION(imagefilledarc)
984
zval **IM, **cx, **cy, **w, **h, **ST, **E, **col, **style;
988
if (ZEND_NUM_ARGS() != 9 || zend_get_parameters_ex(9, &IM, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
989
ZEND_WRONG_PARAM_COUNT();
992
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
994
convert_to_long_ex(cx);
995
convert_to_long_ex(cy);
996
convert_to_long_ex(w);
997
convert_to_long_ex(h);
998
convert_to_long_ex(ST);
999
convert_to_long_ex(E);
1000
convert_to_long_ex(col);
1001
convert_to_long_ex(style);
1013
gdImageFilledArc(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), st, e, Z_LVAL_PP(col), Z_LVAL_PP(style));
1019
/* {{{ proto bool imagealphablending(resource im, bool on)
1020
Turn alpha blending mode on or off for the given image */
1021
PHP_FUNCTION(imagealphablending)
1026
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &blend) == FAILURE) {
1027
ZEND_WRONG_PARAM_COUNT();
1030
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1031
convert_to_boolean_ex(blend);
1033
gdImageAlphaBlending(im, Z_LVAL_PP(blend));
1040
/* {{{ proto bool imagesavealpha(resource im, bool on)
1041
Include alpha channel to a saved image */
1042
PHP_FUNCTION(imagesavealpha)
1047
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &save) == FAILURE) {
1048
ZEND_WRONG_PARAM_COUNT();
1051
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1052
convert_to_boolean_ex(save);
1054
gdImageSaveAlpha(im, Z_LVAL_PP(save));
1061
/* {{{ proto bool imagelayereffect(resource im, int effect)
1062
Set the alpha blending flag to use the bundled libgd layering effects */
1063
PHP_FUNCTION(imagelayereffect)
1065
zval **IM, **effect;
1068
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &effect) == FAILURE) {
1069
ZEND_WRONG_PARAM_COUNT();
1072
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1073
convert_to_long_ex(effect);
1075
gdImageAlphaBlending(im, Z_LVAL_PP(effect) );
1082
/* {{{ proto int imagecolorallocatealpha(resource im, int red, int green, int blue, int alpha)
1083
Allocate a color with an alpha level. Works for true color and palette based images */
1084
PHP_FUNCTION(imagecolorallocatealpha)
1087
long red, green, blue, alpha;
1090
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
1094
ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
1096
RETURN_LONG(gdImageColorAllocateAlpha(im, red, green, blue, alpha));
1100
/* {{{ proto int imagecolorresolvealpha(resource im, int red, int green, int blue, int alpha)
1101
Resolve/Allocate a colour with an alpha level. Works for true colour and palette based images */
1102
PHP_FUNCTION(imagecolorresolvealpha)
1104
zval **IM, ** red, **green, **blue, **alpha;
1107
if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &IM, &red, &green, &blue, &alpha) == FAILURE) {
1108
ZEND_WRONG_PARAM_COUNT();
1111
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1113
convert_to_long_ex(red);
1114
convert_to_long_ex(green);
1115
convert_to_long_ex(blue);
1116
convert_to_long_ex(alpha);
1118
RETURN_LONG(gdImageColorResolveAlpha(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue), Z_LVAL_PP(alpha)));
1122
/* {{{ proto int imagecolorclosestalpha(resource im, int red, int green, int blue, int alpha)
1123
Find the closest matching colour with alpha transparency */
1124
PHP_FUNCTION(imagecolorclosestalpha)
1126
zval **IM, ** red, **green, **blue, **alpha;
1129
if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &IM, &red, &green, &blue, &alpha) == FAILURE) {
1130
ZEND_WRONG_PARAM_COUNT();
1133
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1135
convert_to_long_ex(red);
1136
convert_to_long_ex(green);
1137
convert_to_long_ex(blue);
1138
convert_to_long_ex(alpha);
1140
RETURN_LONG(gdImageColorClosestAlpha(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue), Z_LVAL_PP(alpha)));
1144
/* {{{ proto int imagecolorexactalpha(resource im, int red, int green, int blue, int alpha)
1145
Find exact match for colour with transparency */
1146
PHP_FUNCTION(imagecolorexactalpha)
1148
zval **IM, **red, **green, **blue, **alpha;
1151
if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &IM, &red, &green, &blue, &alpha) == FAILURE) {
1152
ZEND_WRONG_PARAM_COUNT();
1155
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1157
convert_to_long_ex(red);
1158
convert_to_long_ex(green);
1159
convert_to_long_ex(blue);
1160
convert_to_long_ex(alpha);
1162
RETURN_LONG(gdImageColorExactAlpha(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue), Z_LVAL_PP(alpha)));
1166
/* {{{ 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)
1167
Copy and resize part of an image using resampling to help ensure clarity */
1168
PHP_FUNCTION(imagecopyresampled)
1170
zval **SIM, **DIM, **SX, **SY, **SW, **SH, **DX, **DY, **DW, **DH;
1171
gdImagePtr im_dst, im_src;
1172
int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
1174
if (ZEND_NUM_ARGS() != 10 || zend_get_parameters_ex(10, &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
1175
ZEND_WRONG_PARAM_COUNT();
1178
ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, DIM, -1, "Image", le_gd);
1179
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
1181
convert_to_long_ex(SX);
1182
convert_to_long_ex(SY);
1183
convert_to_long_ex(SW);
1184
convert_to_long_ex(SH);
1185
convert_to_long_ex(DX);
1186
convert_to_long_ex(DY);
1187
convert_to_long_ex(DW);
1188
convert_to_long_ex(DH);
1190
srcX = Z_LVAL_PP(SX);
1191
srcY = Z_LVAL_PP(SY);
1192
srcH = Z_LVAL_PP(SH);
1193
srcW = Z_LVAL_PP(SW);
1194
dstX = Z_LVAL_PP(DX);
1195
dstY = Z_LVAL_PP(DY);
1196
dstH = Z_LVAL_PP(DH);
1197
dstW = Z_LVAL_PP(DW);
1199
gdImageCopyResampled(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
1206
#ifdef HAVE_GD_BUNDLED
1207
/* {{{ proto resource imagerotate(resource src_im, float angle, int bgdcolor)
1208
Rotate an image using a custom angle */
1209
PHP_FUNCTION(imagerotate)
1211
zval **SIM, **ANGLE, **BGDCOLOR;
1212
gdImagePtr im_dst, im_src;
1216
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &SIM, &ANGLE, &BGDCOLOR) == FAILURE) {
1217
ZEND_WRONG_PARAM_COUNT();
1220
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
1222
convert_to_long_ex(BGDCOLOR);
1223
color = Z_LVAL_PP(BGDCOLOR);
1225
convert_to_double_ex(ANGLE);
1226
degrees = Z_DVAL_PP(ANGLE);
1227
im_dst = gdImageRotate(im_src, degrees, color);
1229
if (im_dst != NULL) {
1230
ZEND_REGISTER_RESOURCE(return_value, im_dst, le_gd);
1238
#if HAVE_GD_IMAGESETTILE
1239
/* {{{ proto bool imagesettile(resource image, resource tile)
1240
Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color */
1241
PHP_FUNCTION(imagesettile)
1244
gdImagePtr im, tile;
1246
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &TILE) == FAILURE) {
1247
ZEND_WRONG_PARAM_COUNT();
1250
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1251
ZEND_FETCH_RESOURCE(tile, gdImagePtr, TILE, -1, "Image", le_gd);
1253
gdImageSetTile(im, tile);
1260
#if HAVE_GD_IMAGESETBRUSH
1261
/* {{{ proto bool imagesetbrush(resource image, resource brush)
1262
Set the brush image to $brush when filling $image with the "IMG_COLOR_BRUSHED" color */
1263
PHP_FUNCTION(imagesetbrush)
1266
gdImagePtr im, tile;
1268
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &TILE) == FAILURE) {
1269
ZEND_WRONG_PARAM_COUNT();
1272
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1273
ZEND_FETCH_RESOURCE(tile, gdImagePtr, TILE, -1, "Image", le_gd);
1275
gdImageSetBrush(im, tile);
1282
/* {{{ proto resource imagecreate(int x_size, int y_size)
1283
Create a new image */
1284
PHP_FUNCTION(imagecreate)
1286
zval **x_size, **y_size;
1289
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &x_size, &y_size) == FAILURE) {
1290
ZEND_WRONG_PARAM_COUNT();
1293
convert_to_long_ex(x_size);
1294
convert_to_long_ex(y_size);
1296
if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0) {
1297
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
1301
im = gdImageCreate(Z_LVAL_PP(x_size), Z_LVAL_PP(y_size));
1303
ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
1307
/* {{{ proto int imagetypes(void)
1308
Return the types of images supported in a bitfield - 1=GIF, 2=JPEG, 4=PNG, 8=WBMP, 16=XPM */
1309
PHP_FUNCTION(imagetypes)
1312
#ifdef HAVE_GD_GIF_CREATE
1324
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
1328
if (ZEND_NUM_ARGS() != 0) {
1336
/* {{{ _php_image_type
1338
static const char php_sig_gd2[3] = {'g', 'd', '2'};
1340
static int _php_image_type (char data[8])
1343
/* Based on ext/standard/image.c */
1349
if (!memcmp(data, php_sig_gd2, 3)) {
1350
return PHP_GDIMG_TYPE_GD2;
1351
} else if (!memcmp(data, php_sig_jpg, 3)) {
1352
return PHP_GDIMG_TYPE_JPG;
1353
} else if (!memcmp(data, php_sig_png, 3)) {
1354
if (!memcmp(data, php_sig_png, 8)) {
1355
return PHP_GDIMG_TYPE_PNG;
1357
} else if (!memcmp(data, php_sig_gif, 3)) {
1358
return PHP_GDIMG_TYPE_GIF;
1363
io_ctx = gdNewDynamicCtxEx(8, data, 0);
1365
if (getmbi((int(*)(void *)) gdGetC, io_ctx) == 0 && skipheader((int(*)(void *)) gdGetC, io_ctx) == 0 ) {
1367
io_ctx->gd_free(io_ctx);
1369
io_ctx->free(io_ctx);
1371
return PHP_GDIMG_TYPE_WBM;
1374
io_ctx->gd_free(io_ctx);
1376
io_ctx->free(io_ctx);
1388
/* {{{ _php_image_create_from_string
1390
gdImagePtr _php_image_create_from_string(zval **data, char *tn, gdImagePtr (*ioctx_func_p)() TSRMLS_DC)
1395
io_ctx = gdNewDynamicCtxEx(Z_STRLEN_PP(data), Z_STRVAL_PP(data), 0);
1401
im = (*ioctx_func_p)(io_ctx);
1403
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Passed data is not in '%s' format", tn);
1408
io_ctx->gd_free(io_ctx);
1410
io_ctx->free(io_ctx);
1417
/* {{{ proto resource imagecreatefromstring(string image)
1418
Create a new image from the image stream in the string */
1419
PHP_FUNCTION(imagecreatefromstring)
1426
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &data) == FAILURE) {
1427
ZEND_WRONG_PARAM_COUNT();
1430
convert_to_string_ex(data);
1431
memcpy(sig, Z_STRVAL_PP(data), 8);
1433
imtype = _php_image_type(sig);
1436
case PHP_GDIMG_TYPE_JPG:
1438
im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx TSRMLS_CC);
1440
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No JPEG support in this PHP build");
1445
case PHP_GDIMG_TYPE_PNG:
1447
im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx TSRMLS_CC);
1449
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No PNG support in this PHP build");
1454
case PHP_GDIMG_TYPE_GIF:
1455
#ifdef HAVE_GD_GIF_READ
1456
im = _php_image_create_from_string(data, "GIF", gdImageCreateFromGifCtx TSRMLS_CC);
1458
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No GIF support in this PHP build");
1463
case PHP_GDIMG_TYPE_WBM:
1465
im = _php_image_create_from_string(data, "WBMP", gdImageCreateFromWBMPCtx TSRMLS_CC);
1467
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No WBMP support in this PHP build");
1472
case PHP_GDIMG_TYPE_GD2:
1474
im = _php_image_create_from_string(data, "GD2", gdImageCreateFromGd2Ctx TSRMLS_CC);
1476
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No GD2 support in this PHP build");
1482
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Data is not in a recognized format.");
1487
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't create GD Image Stream out of Data");
1491
ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
1496
/* {{{ _php_image_create_from
1498
static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)())
1500
zval **file, **srcx, **srcy, **width, **height;
1501
gdImagePtr im = NULL;
1505
int argc=ZEND_NUM_ARGS();
1507
if ((image_type == PHP_GDIMG_TYPE_GD2PART && argc != 5) ||
1508
(image_type != PHP_GDIMG_TYPE_GD2PART && argc != 1) ||
1509
zend_get_parameters_ex(argc, &file, &srcx, &srcy, &width, &height) == FAILURE) {
1510
ZEND_WRONG_PARAM_COUNT();
1513
convert_to_string_ex(file);
1515
if (argc == 5 && image_type == PHP_GDIMG_TYPE_GD2PART) {
1516
multi_convert_to_long_ex(4, srcx, srcy, width, height);
1519
fn = Z_STRVAL_PP(file);
1521
stream = php_stream_open_wrapper(fn, "rb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
1522
if (stream == NULL) {
1526
#ifndef USE_GD_IOCTX
1527
ioctx_func_p = NULL; /* don't allow sockets without IOCtx */
1530
/* try and avoid allocating a FILE* if the stream is not naturally a FILE* */
1531
if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
1532
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
1535
} else if (ioctx_func_p) {
1537
/* we can create an io context */
1542
/* needs to be malloc (persistent) - GD will free() it later */
1543
buff_size = php_stream_copy_to_mem(stream, &buff, PHP_STREAM_COPY_ALL, 1);
1546
php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot read image data");
1550
io_ctx = gdNewDynamicCtxEx(buff_size, buff, 0);
1552
php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot allocate GD IO context");
1556
if (image_type == PHP_GDIMG_TYPE_GD2PART) {
1557
im = (*ioctx_func_p)(io_ctx, Z_LVAL_PP(srcx), Z_LVAL_PP(srcy), Z_LVAL_PP(width), Z_LVAL_PP(height));
1559
im = (*ioctx_func_p)(io_ctx);
1562
io_ctx->gd_free(io_ctx);
1564
io_ctx->free(io_ctx);
1570
/* try and force the stream to be FILE* */
1571
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {
1577
switch (image_type) {
1578
case PHP_GDIMG_TYPE_GD2PART:
1579
im = (*func_p)(fp, Z_LVAL_PP(srcx), Z_LVAL_PP(srcy), Z_LVAL_PP(width), Z_LVAL_PP(height));
1581
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
1582
case PHP_GDIMG_TYPE_XPM:
1583
im = gdImageCreateFromXpm(fn);
1595
ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
1596
php_stream_close(stream);
1600
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid %s file", fn, tn);
1602
php_stream_close(stream);
1608
#ifdef HAVE_GD_GIF_READ
1609
/* {{{ proto resource imagecreatefromgif(string filename)
1610
Create a new image from GIF file or URL */
1611
PHP_FUNCTION(imagecreatefromgif)
1613
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
1616
#endif /* HAVE_GD_GIF_READ */
1619
/* {{{ proto resource imagecreatefromjpeg(string filename)
1620
Create a new image from JPEG file or URL */
1621
PHP_FUNCTION(imagecreatefromjpeg)
1623
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
1626
#endif /* HAVE_GD_JPG */
1629
/* {{{ proto resource imagecreatefrompng(string filename)
1630
Create a new image from PNG file or URL */
1631
PHP_FUNCTION(imagecreatefrompng)
1633
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
1636
#endif /* HAVE_GD_PNG */
1639
/* {{{ proto resource imagecreatefromxbm(string filename)
1640
Create a new image from XBM file or URL */
1641
PHP_FUNCTION(imagecreatefromxbm)
1643
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);
1646
#endif /* HAVE_GD_XBM */
1648
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
1649
/* {{{ proto resource imagecreatefromxpm(string filename)
1650
Create a new image from XPM file or URL */
1651
PHP_FUNCTION(imagecreatefromxpm)
1653
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", gdImageCreateFromXpm, NULL);
1659
/* {{{ proto resource imagecreatefromwbmp(string filename)
1660
Create a new image from WBMP file or URL */
1661
PHP_FUNCTION(imagecreatefromwbmp)
1663
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);
1666
#endif /* HAVE_GD_WBMP */
1668
/* {{{ proto resource imagecreatefromgd(string filename)
1669
Create a new image from GD file or URL */
1670
PHP_FUNCTION(imagecreatefromgd)
1672
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);
1677
/* {{{ proto resource imagecreatefromgd2(string filename)
1678
Create a new image from GD2 file or URL */
1679
PHP_FUNCTION(imagecreatefromgd2)
1681
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);
1685
/* {{{ proto resource imagecreatefromgd2part(string filename, int srcX, int srcY, int width, int height)
1686
Create a new image from a given part of GD2 file or URL */
1687
PHP_FUNCTION(imagecreatefromgd2part)
1689
_php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", gdImageCreateFromGd2Part, gdImageCreateFromGd2PartCtx);
1692
#endif /* HAVE_GD_GD2 */
1694
/* {{{ _php_image_output
1696
static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
1698
zval **imgind, **file, **quality, **type;
1702
int argc = ZEND_NUM_ARGS();
1703
int q = -1, i, t = 1;
1705
/* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */
1706
/* When called from imagewbmp() the quality parameter stands for the foreground color. Default: black. */
1707
/* The quality parameter for gd2 stands for chunk size */
1709
if (argc < 1 || argc > 4 || zend_get_parameters_ex(argc, &imgind, &file, &quality, &type) == FAILURE) {
1710
ZEND_WRONG_PARAM_COUNT();
1713
ZEND_FETCH_RESOURCE(im, gdImagePtr, imgind, -1, "Image", le_gd);
1716
convert_to_string_ex(file);
1717
fn = Z_STRVAL_PP(file);
1719
convert_to_long_ex(quality);
1720
q = Z_LVAL_PP(quality);
1723
convert_to_long_ex(type);
1724
t = Z_LVAL_PP(type);
1728
if ((argc == 2) || (argc > 2 && Z_STRLEN_PP(file))) {
1729
if (!fn || fn == empty_string || php_check_open_basedir(fn TSRMLS_CC)) {
1730
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filename '%s'", fn);
1734
fp = VCWD_FOPEN(fn, "wb");
1736
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn);
1740
switch (image_type) {
1742
case PHP_GDIMG_CONVERT_WBM:
1745
} else if (q < 0 || q > 255) {
1746
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
1749
gdImageWBMP(im, q, fp);
1752
case PHP_GDIMG_TYPE_JPG:
1753
(*func_p)(im, fp, q);
1755
case PHP_GDIMG_TYPE_WBM:
1756
for (i = 0; i < gdImageColorsTotal(im); i++) {
1757
if (gdImageRed(im, i) == 0) break;
1759
(*func_p)(im, i, fp);
1762
case PHP_GDIMG_TYPE_GD:
1764
gdImageTrueColorToPalette(im,1,256);
1773
(*func_p)(im, fp, q, t);
1784
tmp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);
1786
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file");
1790
switch (image_type) {
1792
case PHP_GDIMG_CONVERT_WBM:
1795
} else if (q < 0 || q > 255) {
1796
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
1799
gdImageWBMP(im, q, tmp);
1802
case PHP_GDIMG_TYPE_JPG:
1803
(*func_p)(im, tmp, q);
1805
case PHP_GDIMG_TYPE_WBM:
1806
for (i = 0; i < gdImageColorsTotal(im); i++) {
1807
if (gdImageRed(im, i) == 0) {
1811
(*func_p)(im, q, tmp);
1814
case PHP_GDIMG_TYPE_GD:
1815
if (im->trueColor) {
1816
gdImageTrueColorToPalette(im,1,256);
1826
fseek(tmp, 0, SEEK_SET);
1828
#if APACHE && defined(CHARSET_EBCDIC)
1829
/* XXX this is unlikely to work any more thies@thieso.net */
1831
/* This is a binary file already: avoid EBCDIC->ASCII conversion */
1832
ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
1834
while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
1835
php_write(buf, b TSRMLS_CC);
1839
VCWD_UNLINK((const char *)path); /* make sure that the temporary file is removed */
1846
/* {{{ proto int imagexbm(int im, string filename [, int foreground])
1847
Output XBM image to browser or file */
1849
PHP_FUNCTION(imagexbm)
1851
_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageXbmCtx);
1856
#ifdef HAVE_GD_GIF_CREATE
1857
/* {{{ proto bool imagegif(resource im [, string filename])
1858
Output GIF image to browser or file */
1859
PHP_FUNCTION(imagegif)
1861
#ifdef HAVE_GD_GIF_CTX
1862
_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageGifCtx);
1864
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageGif);
1868
#endif /* HAVE_GD_GIF_CREATE */
1871
/* {{{ proto bool imagepng(resource im [, string filename])
1872
Output PNG image to browser or file */
1873
PHP_FUNCTION(imagepng)
1876
_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePngCtx);
1878
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePng);
1882
#endif /* HAVE_GD_PNG */
1885
/* {{{ proto bool imagejpeg(resource im [, string filename [, int quality]])
1886
Output JPEG image to browser or file */
1887
PHP_FUNCTION(imagejpeg)
1890
_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageJpegCtx);
1892
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageJpeg);
1896
#endif /* HAVE_GD_JPG */
1899
/* {{{ proto bool imagewbmp(resource im [, string filename, [, int foreground]])
1900
Output WBMP image to browser or file */
1901
PHP_FUNCTION(imagewbmp)
1904
_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageWBMPCtx);
1906
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageWBMP);
1910
#endif /* HAVE_GD_WBMP */
1912
/* {{{ proto bool imagegd(resource im [, string filename])
1913
Output GD image to browser or file */
1914
PHP_FUNCTION(imagegd)
1916
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageGd);
1921
/* {{{ proto bool imagegd2(resource im [, string filename, [, int chunk_size, [, int type]]])
1922
Output GD2 image to browser or file */
1923
PHP_FUNCTION(imagegd2)
1925
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageGd2);
1928
#endif /* HAVE_GD_GD2 */
1930
/* {{{ proto bool imagedestroy(resource im)
1932
PHP_FUNCTION(imagedestroy)
1937
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &IM) == FAILURE) {
1938
ZEND_WRONG_PARAM_COUNT();
1941
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1943
zend_list_delete(Z_LVAL_PP(IM));
1950
/* {{{ proto int imagecolorallocate(resource im, int red, int green, int blue)
1951
Allocate a color for an image */
1952
PHP_FUNCTION(imagecolorallocate)
1954
zval **IM, **red, **green, **blue;
1957
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {
1958
ZEND_WRONG_PARAM_COUNT();
1961
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
1963
convert_to_long_ex(red);
1964
convert_to_long_ex(green);
1965
convert_to_long_ex(blue);
1967
RETURN_LONG(gdImageColorAllocate(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));
1972
/* {{{ proto void imagepalettecopy(resource dst, resource src)
1973
Copy the palette from the src image onto the dst image */
1974
PHP_FUNCTION(imagepalettecopy)
1976
zval **dstim, **srcim;
1977
gdImagePtr dst, src;
1979
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &dstim, &srcim) == FAILURE) {
1980
ZEND_WRONG_PARAM_COUNT();
1983
ZEND_FETCH_RESOURCE(dst, gdImagePtr, dstim, -1, "Image", le_gd);
1984
ZEND_FETCH_RESOURCE(src, gdImagePtr, srcim, -1, "Image", le_gd);
1986
gdImagePaletteCopy(dst, src);
1991
/* {{{ proto int imagecolorat(resource im, int x, int y)
1992
Get the index of the color of a pixel */
1993
PHP_FUNCTION(imagecolorat)
1995
zval **IM, **x, **y;
1998
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &IM, &x, &y) == FAILURE) {
1999
ZEND_WRONG_PARAM_COUNT();
2002
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2004
convert_to_long_ex(x);
2005
convert_to_long_ex(y);
2008
if (gdImageTrueColor(im)) {
2009
if (im->tpixels && gdImageBoundsSafe(im, Z_LVAL_PP(x), Z_LVAL_PP(y))) {
2010
RETURN_LONG(gdImageTrueColorPixel(im, Z_LVAL_PP(x), Z_LVAL_PP(y)));
2012
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%ld,%ld is out of bounds", Z_LVAL_PP(x), Z_LVAL_PP(y));
2017
if (im->pixels && gdImageBoundsSafe(im, Z_LVAL_PP(x), Z_LVAL_PP(y))) {
2019
RETURN_LONG(im->pixels[Z_LVAL_PP(y)][Z_LVAL_PP(x)]);
2021
RETURN_LONG(im->pixels[Z_LVAL_PP(x)][Z_LVAL_PP(y)]);
2024
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%ld,%ld is out of bounds", Z_LVAL_PP(x), Z_LVAL_PP(y));
2033
/* {{{ proto int imagecolorclosest(resource im, int red, int green, int blue)
2034
Get the index of the closest color to the specified color */
2035
PHP_FUNCTION(imagecolorclosest)
2037
zval **IM, **red, **green, **blue;
2040
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {
2041
ZEND_WRONG_PARAM_COUNT();
2044
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2046
convert_to_long_ex(red);
2047
convert_to_long_ex(green);
2048
convert_to_long_ex(blue);
2050
RETURN_LONG(gdImageColorClosest(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));
2054
#if HAVE_COLORCLOSESTHWB
2055
/* {{{ proto int imagecolorclosesthwb(resource im, int red, int green, int blue)
2056
Get the index of the color which has the hue, white and blackness nearest to the given color */
2057
PHP_FUNCTION(imagecolorclosesthwb)
2059
zval **IM, **red, **green, **blue;
2062
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {
2063
ZEND_WRONG_PARAM_COUNT();
2066
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2068
convert_to_long_ex(red);
2069
convert_to_long_ex(green);
2070
convert_to_long_ex(blue);
2072
RETURN_LONG(gdImageColorClosestHWB(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));
2077
/* {{{ proto bool imagecolordeallocate(resource im, int index)
2078
De-allocate a color for an image */
2079
PHP_FUNCTION(imagecolordeallocate)
2085
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &index) == FAILURE) {
2086
ZEND_WRONG_PARAM_COUNT();
2089
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2092
/* We can return right away for a truecolor image as deallocating colours is meaningless here */
2093
if (gdImageTrueColor(im)) {
2098
convert_to_long_ex(index);
2099
col = Z_LVAL_PP(index);
2101
if (col >= 0 && col < gdImageColorsTotal(im)) {
2102
gdImageColorDeallocate(im, col);
2105
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color index %d out of range", col);
2111
/* {{{ proto int imagecolorresolve(resource im, int red, int green, int blue)
2112
Get the index of the specified color or its closest possible alternative */
2113
PHP_FUNCTION(imagecolorresolve)
2115
zval **IM, **red, **green, **blue;
2118
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {
2119
ZEND_WRONG_PARAM_COUNT();
2122
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2124
convert_to_long_ex(red);
2125
convert_to_long_ex(green);
2126
convert_to_long_ex(blue);
2128
RETURN_LONG(gdImageColorResolve(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));
2132
/* {{{ proto int imagecolorexact(resource im, int red, int green, int blue)
2133
Get the index of the specified color */
2134
PHP_FUNCTION(imagecolorexact)
2136
zval **IM, **red, **green, **blue;
2139
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {
2140
ZEND_WRONG_PARAM_COUNT();
2143
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2145
convert_to_long_ex(red);
2146
convert_to_long_ex(green);
2147
convert_to_long_ex(blue);
2149
RETURN_LONG(gdImageColorExact(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));
2153
/* {{{ proto void imagecolorset(resource im, int col, int red, int green, int blue)
2154
Set the color for the specified palette index */
2155
PHP_FUNCTION(imagecolorset)
2157
zval **IM, **color, **red, **green, **blue;
2161
if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &IM, &color, &red, &green, &blue) == FAILURE) {
2162
ZEND_WRONG_PARAM_COUNT();
2165
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2167
convert_to_long_ex(color);
2168
convert_to_long_ex(red);
2169
convert_to_long_ex(green);
2170
convert_to_long_ex(blue);
2172
col = Z_LVAL_PP(color);
2174
if (col >= 0 && col < gdImageColorsTotal(im)) {
2175
im->red[col] = Z_LVAL_PP(red);
2176
im->green[col] = Z_LVAL_PP(green);
2177
im->blue[col] = Z_LVAL_PP(blue);
2184
/* {{{ proto array imagecolorsforindex(resource im, int col)
2185
Get the colors for an index */
2186
PHP_FUNCTION(imagecolorsforindex)
2192
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &index) == FAILURE) {
2193
ZEND_WRONG_PARAM_COUNT();
2196
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2198
convert_to_long_ex(index);
2199
col = Z_LVAL_PP(index);
2201
if ((col >= 0 && gdImageTrueColor(im)) || (!gdImageTrueColor(im) && col >= 0 && col < gdImageColorsTotal(im))) {
2202
array_init(return_value);
2204
add_assoc_long(return_value,"red", gdImageRed(im,col));
2205
add_assoc_long(return_value,"green", gdImageGreen(im,col));
2206
add_assoc_long(return_value,"blue", gdImageBlue(im,col));
2207
add_assoc_long(return_value,"alpha", gdImageAlpha(im,col));
2210
if (col >= 0 && col < gdImageColorsTotal(im)) {
2211
array_init(return_value);
2213
add_assoc_long(return_value,"red", im->red[col]);
2214
add_assoc_long(return_value,"green", im->green[col]);
2215
add_assoc_long(return_value,"blue", im->blue[col]);
2219
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color index %d out of range", col);
2225
/* {{{ proto bool imagegammacorrect(resource im, float inputgamma, float outputgamma)
2226
Apply a gamma correction to a GD image */
2227
PHP_FUNCTION(imagegammacorrect)
2229
zval **IM, **inputgamma, **outputgamma;
2232
double input, output;
2234
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &IM, &inputgamma, &outputgamma) == FAILURE) {
2235
ZEND_WRONG_PARAM_COUNT();
2238
convert_to_double_ex(inputgamma);
2239
convert_to_double_ex(outputgamma);
2241
input = Z_DVAL_PP(inputgamma);
2242
output = Z_DVAL_PP(outputgamma);
2244
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2247
if (gdImageTrueColor(im)) {
2250
for (y = 0; y < gdImageSY(im); y++) {
2251
for (x = 0; x < gdImageSX(im); x++) {
2252
c = gdImageGetPixel(im, x, y);
2253
gdImageSetPixel(im, x, y,
2255
(int) ((pow((pow((gdTrueColorGetRed(c) / 255.0), input)), 1.0 / output) * 255) + .5),
2256
(int) ((pow((pow((gdTrueColorGetGreen(c) / 255.0), input)), 1.0 / output) * 255) + .5),
2257
(int) ((pow((pow((gdTrueColorGetBlue(c) / 255.0), input)), 1.0 / output) * 255) + .5)
2265
for (i = 0; i < gdImageColorsTotal(im); i++) {
2266
im->red[i] = (int)((pow((pow((im->red[i] / 255.0), input)), 1.0 / output) * 255) + .5);
2267
im->green[i] = (int)((pow((pow((im->green[i] / 255.0), input)), 1.0 / output) * 255) + .5);
2268
im->blue[i] = (int)((pow((pow((im->blue[i] / 255.0), input)), 1.0 / output) * 255) + .5);
2275
/* {{{ proto bool imagesetpixel(resource im, int x, int y, int col)
2276
Set a single pixel */
2277
PHP_FUNCTION(imagesetpixel)
2279
zval **IM, **x, **y, **col;
2282
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &x, &y, &col) == FAILURE) {
2283
ZEND_WRONG_PARAM_COUNT();
2286
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2288
convert_to_long_ex(x);
2289
convert_to_long_ex(y);
2290
convert_to_long_ex(col);
2292
gdImageSetPixel(im, Z_LVAL_PP(x), Z_LVAL_PP(y), Z_LVAL_PP(col));
2298
/* {{{ proto bool imageline(resource im, int x1, int y1, int x2, int y2, int col)
2300
PHP_FUNCTION(imageline)
2302
zval **IM, **x1, **y1, **x2, **y2, **col;
2305
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2306
ZEND_WRONG_PARAM_COUNT();
2309
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2311
convert_to_long_ex(x1);
2312
convert_to_long_ex(y1);
2313
convert_to_long_ex(x2);
2314
convert_to_long_ex(y2);
2315
convert_to_long_ex(col);
2317
#ifdef HAVE_GD_BUNDLED
2318
if (im->antialias) {
2319
gdImageAALine(im, Z_LVAL_PP(x1), Z_LVAL_PP(y1), Z_LVAL_PP(x2), Z_LVAL_PP(y2), Z_LVAL_PP(col));
2323
gdImageLine(im, Z_LVAL_PP(x1), Z_LVAL_PP(y1), Z_LVAL_PP(x2), Z_LVAL_PP(y2), Z_LVAL_PP(col));
2329
/* {{{ proto bool imagedashedline(resource im, int x1, int y1, int x2, int y2, int col)
2330
Draw a dashed line */
2331
PHP_FUNCTION(imagedashedline)
2333
zval **IM, **x1, **y1, **x2, **y2, **col;
2336
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2337
ZEND_WRONG_PARAM_COUNT();
2340
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2342
convert_to_long_ex(x1);
2343
convert_to_long_ex(y1);
2344
convert_to_long_ex(x2);
2345
convert_to_long_ex(y2);
2346
convert_to_long_ex(col);
2348
gdImageDashedLine(im, Z_LVAL_PP(x1), Z_LVAL_PP(y1), Z_LVAL_PP(x2), Z_LVAL_PP(y2), Z_LVAL_PP(col));
2353
/* {{{ proto bool imagerectangle(resource im, int x1, int y1, int x2, int y2, int col)
2355
PHP_FUNCTION(imagerectangle)
2357
zval **IM, **x1, **y1, **x2, **y2, **col;
2360
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2361
ZEND_WRONG_PARAM_COUNT();
2364
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2366
convert_to_long_ex(x1);
2367
convert_to_long_ex(y1);
2368
convert_to_long_ex(x2);
2369
convert_to_long_ex(y2);
2370
convert_to_long_ex(col);
2372
gdImageRectangle(im, Z_LVAL_PP(x1), Z_LVAL_PP(y1), Z_LVAL_PP(x2), Z_LVAL_PP(y2), Z_LVAL_PP(col));
2377
/* {{{ proto bool imagefilledrectangle(resource im, int x1, int y1, int x2, int y2, int col)
2378
Draw a filled rectangle */
2379
PHP_FUNCTION(imagefilledrectangle)
2381
zval **IM, **x1, **y1, **x2, **y2, **col;
2384
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
2385
ZEND_WRONG_PARAM_COUNT();
2388
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2390
convert_to_long_ex(x1);
2391
convert_to_long_ex(y1);
2392
convert_to_long_ex(x2);
2393
convert_to_long_ex(y2);
2394
convert_to_long_ex(col);
2396
gdImageFilledRectangle(im, Z_LVAL_PP(x1), Z_LVAL_PP(y1), Z_LVAL_PP(x2), Z_LVAL_PP(y2), Z_LVAL_PP(col));
2401
/* {{{ proto bool imagearc(resource im, int cx, int cy, int w, int h, int s, int e, int col)
2402
Draw a partial ellipse */
2403
PHP_FUNCTION(imagearc)
2405
zval **IM, **cx, **cy, **w, **h, **ST, **E, **col;
2409
if (ZEND_NUM_ARGS() != 8 || zend_get_parameters_ex(8, &IM, &cx, &cy, &w, &h, &ST, &E, &col) == FAILURE) {
2410
ZEND_WRONG_PARAM_COUNT();
2413
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2415
convert_to_long_ex(cx);
2416
convert_to_long_ex(cy);
2417
convert_to_long_ex(w);
2418
convert_to_long_ex(h);
2419
convert_to_long_ex(ST);
2420
convert_to_long_ex(E);
2421
convert_to_long_ex(col);
2433
gdImageArc(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), st, e, Z_LVAL_PP(col));
2438
/* {{{ proto bool imageellipse(resource im, int cx, int cy, int w, int h, int color)
2440
PHP_FUNCTION(imageellipse)
2442
zval **IM, **cx, **cy, **w, **h, **color;
2445
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &cx, &cy, &w, &h, &color) == FAILURE) {
2446
ZEND_WRONG_PARAM_COUNT();
2449
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2451
convert_to_long_ex(cx);
2452
convert_to_long_ex(cy);
2453
convert_to_long_ex(w);
2454
convert_to_long_ex(h);
2455
convert_to_long_ex(color);
2457
#ifdef HAVE_GD_IMAGEELLIPSE /* this function is missing from GD 2.0.1 */
2458
gdImageEllipse(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), Z_LVAL_PP(color));
2460
gdImageArc(im, Z_LVAL_PP(cx), Z_LVAL_PP(cy), Z_LVAL_PP(w), Z_LVAL_PP(h), 0, 360, Z_LVAL_PP(color));
2467
/* {{{ proto bool imagefilltoborder(resource im, int x, int y, int border, int col)
2468
Flood fill to specific color */
2469
PHP_FUNCTION(imagefilltoborder)
2471
zval **IM, **x, **y, **border, **col;
2474
if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &IM, &x, &y, &border, &col) == FAILURE) {
2475
ZEND_WRONG_PARAM_COUNT();
2478
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2480
convert_to_long_ex(x);
2481
convert_to_long_ex(y);
2482
convert_to_long_ex(border);
2483
convert_to_long_ex(col);
2485
gdImageFillToBorder(im, Z_LVAL_PP(x), Z_LVAL_PP(y), Z_LVAL_PP(border), Z_LVAL_PP(col));
2490
/* {{{ proto bool imagefill(resource im, int x, int y, int col)
2492
PHP_FUNCTION(imagefill)
2494
zval **IM, **x, **y, **col;
2497
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &x, &y, &col) == FAILURE) {
2498
ZEND_WRONG_PARAM_COUNT();
2501
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2503
convert_to_long_ex(x);
2504
convert_to_long_ex(y);
2505
convert_to_long_ex(col);
2507
gdImageFill(im, Z_LVAL_PP(x), Z_LVAL_PP(y), Z_LVAL_PP(col));
2512
/* {{{ proto int imagecolorstotal(resource im)
2513
Find out the number of colors in an image's palette */
2514
PHP_FUNCTION(imagecolorstotal)
2519
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &IM) == FAILURE) {
2520
ZEND_WRONG_PARAM_COUNT();
2523
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2525
RETURN_LONG(gdImageColorsTotal(im));
2529
/* {{{ proto int imagecolortransparent(resource im [, int col])
2530
Define a color as transparent */
2531
PHP_FUNCTION(imagecolortransparent)
2536
switch (ZEND_NUM_ARGS()) {
2538
if (zend_get_parameters_ex(1, &IM) == FAILURE) {
2539
ZEND_WRONG_PARAM_COUNT();
2543
if (zend_get_parameters_ex(2, &IM, &COL) == FAILURE) {
2544
ZEND_WRONG_PARAM_COUNT();
2546
convert_to_long_ex(COL);
2549
ZEND_WRONG_PARAM_COUNT();
2552
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2554
if (ZEND_NUM_ARGS() > 1) {
2555
gdImageColorTransparent(im, Z_LVAL_PP(COL));
2558
RETURN_LONG(gdImageGetTransparent(im));
2562
/* {{{ proto int imageinterlace(resource im [, int interlace])
2563
Enable or disable interlace */
2564
PHP_FUNCTION(imageinterlace)
2569
switch (ZEND_NUM_ARGS()) {
2571
if (zend_get_parameters_ex(1, &IM) == FAILURE) {
2572
ZEND_WRONG_PARAM_COUNT();
2576
if (zend_get_parameters_ex(2, &IM, &INT) == FAILURE) {
2577
ZEND_WRONG_PARAM_COUNT();
2579
convert_to_long_ex(INT);
2582
ZEND_WRONG_PARAM_COUNT();
2585
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2587
if (ZEND_NUM_ARGS() > 1) {
2588
gdImageInterlace(im, Z_LVAL_PP(INT));
2591
RETURN_LONG(gdImageGetInterlaced(im));
2595
/* {{{ php_imagepolygon
2596
arg = 0 normal polygon
2597
arg = 1 filled polygon */
2598
/* im, points, num_points, col */
2599
static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
2601
zval **IM, **POINTS, **NPOINTS, **COL;
2605
int npoints, col, nelem, i;
2607
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &POINTS, &NPOINTS, &COL) == FAILURE) {
2608
ZEND_WRONG_PARAM_COUNT();
2611
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2613
convert_to_long_ex(NPOINTS);
2614
convert_to_long_ex(COL);
2616
npoints = Z_LVAL_PP(NPOINTS);
2617
col = Z_LVAL_PP(COL);
2619
if (Z_TYPE_PP(POINTS) != IS_ARRAY) {
2620
php_error_docref(NULL TSRMLS_CC, E_WARNING, "2nd argument to imagepolygon not an array");
2624
nelem = zend_hash_num_elements(Z_ARRVAL_PP(POINTS));
2626
php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have at least 3 points in your array");
2630
if (nelem < npoints * 2) {
2631
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to use %d points in array with only %d points", npoints, nelem/2);
2635
points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
2637
for (i = 0; i < npoints; i++) {
2638
if (zend_hash_index_find(Z_ARRVAL_PP(POINTS), (i * 2), (void **) &var) == SUCCESS) {
2639
SEPARATE_ZVAL((var));
2640
convert_to_long(*var);
2641
points[i].x = Z_LVAL_PP(var);
2643
if (zend_hash_index_find(Z_ARRVAL_PP(POINTS), (i * 2) + 1, (void **) &var) == SUCCESS) {
2645
convert_to_long(*var);
2646
points[i].y = Z_LVAL_PP(var);
2651
gdImageFilledPolygon(im, points, npoints, col);
2653
gdImagePolygon(im, points, npoints, col);
2661
/* {{{ proto bool imagepolygon(resource im, array point, int num_points, int col)
2663
PHP_FUNCTION(imagepolygon)
2665
php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2669
/* {{{ proto bool imagefilledpolygon(resource im, array point, int num_points, int col)
2670
Draw a filled polygon */
2671
PHP_FUNCTION(imagefilledpolygon)
2673
php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2677
/* {{{ php_find_gd_font
2679
static gdFontPtr php_find_gd_font(int size TSRMLS_DC)
2692
font = gdFontMediumBold;
2701
font = zend_list_find(size - 5, &ind_type);
2702
if (!font || ind_type != le_gd_font) {
2716
/* {{{ php_imagefontsize
2717
* arg = 0 ImageFontWidth
2718
* arg = 1 ImageFontHeight
2720
static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg)
2725
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &SIZE) == FAILURE) {
2726
ZEND_WRONG_PARAM_COUNT();
2729
convert_to_long_ex(SIZE);
2731
font = php_find_gd_font(Z_LVAL_PP(SIZE) TSRMLS_CC);
2732
RETURN_LONG(arg ? font->h : font->w);
2736
/* {{{ proto int imagefontwidth(int font)
2738
PHP_FUNCTION(imagefontwidth)
2740
php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2744
/* {{{ proto int imagefontheight(int font)
2746
PHP_FUNCTION(imagefontheight)
2748
php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2752
/* {{{ php_gdimagecharup
2753
* workaround for a bug in gd 1.2 */
2754
static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color)
2756
int cx, cy, px, py, fline;
2760
if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
2764
fline = (c - f->offset) * f->h * f->w;
2765
for (py = y; (py > (y - f->w)); py--) {
2766
for (px = x; (px < (x + f->h)); px++) {
2767
if (f->data[fline + cy * f->w + cx]) {
2768
gdImageSetPixel(im, px, py, color);
2778
/* {{{ php_imagechar
2780
* arg = 1 ImageCharUp
2781
* arg = 2 ImageString
2782
* arg = 3 ImageStringUp
2784
static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode)
2786
zval **IM, **SIZE, **X, **Y, **C, **COL;
2788
int ch = 0, col, x, y, size, i, l = 0;
2789
unsigned char *str = NULL;
2792
if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &IM, &SIZE, &X, &Y, &C, &COL) == FAILURE) {
2793
ZEND_WRONG_PARAM_COUNT();
2796
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
2798
convert_to_long_ex(SIZE);
2799
convert_to_long_ex(X);
2800
convert_to_long_ex(Y);
2801
convert_to_string_ex(C);
2802
convert_to_long_ex(COL);
2804
col = Z_LVAL_PP(COL);
2807
ch = (int)((unsigned char)*(Z_STRVAL_PP(C)));
2809
str = (unsigned char *) estrndup(Z_STRVAL_PP(C), Z_STRLEN_PP(C));
2815
size = Z_LVAL_PP(SIZE);
2817
font = php_find_gd_font(size TSRMLS_CC);
2821
gdImageChar(im, font, x, y, ch, col);
2824
php_gdimagecharup(im, font, x, y, ch, col);
2827
for (i = 0; (i < l); i++) {
2828
gdImageChar(im, font, x, y, (int) ((unsigned char) str[i]), col);
2833
for (i = 0; (i < l); i++) {
2834
/* php_gdimagecharup(im, font, x, y, (int) str[i], col); */
2835
gdImageCharUp(im, font, x, y, (int) str[i], col);
2848
/* {{{ proto bool imagechar(resource im, int font, int x, int y, string c, int col)
2850
PHP_FUNCTION(imagechar)
2852
php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2856
/* {{{ proto bool imagecharup(resource im, int font, int x, int y, string c, int col)
2857
Draw a character rotated 90 degrees counter-clockwise */
2858
PHP_FUNCTION(imagecharup)
2860
php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2864
/* {{{ proto bool imagestring(resource im, int font, int x, int y, string str, int col)
2865
Draw a string horizontally */
2866
PHP_FUNCTION(imagestring)
2868
php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
2872
/* {{{ proto bool imagestringup(resource im, int font, int x, int y, string str, int col)
2873
Draw a string vertically - rotated 90 degrees counter-clockwise */
2874
PHP_FUNCTION(imagestringup)
2876
php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
2880
/* {{{ 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)
2881
Copy part of an image */
2882
PHP_FUNCTION(imagecopy)
2884
zval **SIM, **DIM, **SX, **SY, **SW, **SH, **DX, **DY;
2885
gdImagePtr im_dst, im_src;
2886
int srcH, srcW, srcY, srcX, dstY, dstX;
2888
if (ZEND_NUM_ARGS() != 8 || zend_get_parameters_ex(8, &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH) == FAILURE) {
2889
ZEND_WRONG_PARAM_COUNT();
2892
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
2893
ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, DIM, -1, "Image", le_gd);
2895
convert_to_long_ex(SX);
2896
convert_to_long_ex(SY);
2897
convert_to_long_ex(SW);
2898
convert_to_long_ex(SH);
2899
convert_to_long_ex(DX);
2900
convert_to_long_ex(DY);
2902
srcX = Z_LVAL_PP(SX);
2903
srcY = Z_LVAL_PP(SY);
2904
srcH = Z_LVAL_PP(SH);
2905
srcW = Z_LVAL_PP(SW);
2906
dstX = Z_LVAL_PP(DX);
2907
dstY = Z_LVAL_PP(DY);
2909
gdImageCopy(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH);
2915
/* {{{ 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)
2916
Merge one part of an image with another */
2917
PHP_FUNCTION(imagecopymerge)
2919
zval **SIM, **DIM, **SX, **SY, **SW, **SH, **DX, **DY, **PCT;
2920
gdImagePtr im_dst, im_src;
2921
int srcH, srcW, srcY, srcX, dstY, dstX, pct;
2923
if (ZEND_NUM_ARGS() != 9 || zend_get_parameters_ex(9, &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
2924
ZEND_WRONG_PARAM_COUNT();
2927
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
2928
ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, DIM, -1, "Image", le_gd);
2930
convert_to_long_ex(SX);
2931
convert_to_long_ex(SY);
2932
convert_to_long_ex(SW);
2933
convert_to_long_ex(SH);
2934
convert_to_long_ex(DX);
2935
convert_to_long_ex(DY);
2936
convert_to_long_ex(PCT);
2938
srcX = Z_LVAL_PP(SX);
2939
srcY = Z_LVAL_PP(SY);
2940
srcH = Z_LVAL_PP(SH);
2941
srcW = Z_LVAL_PP(SW);
2942
dstX = Z_LVAL_PP(DX);
2943
dstY = Z_LVAL_PP(DY);
2944
pct = Z_LVAL_PP(PCT);
2946
gdImageCopyMerge(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
2951
/* {{{ 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)
2952
Merge one part of an image with another */
2953
PHP_FUNCTION(imagecopymergegray)
2955
zval **SIM, **DIM, **SX, **SY, **SW, **SH, **DX, **DY, **PCT;
2956
gdImagePtr im_dst, im_src;
2957
int srcH, srcW, srcY, srcX, dstY, dstX, pct;
2959
if (ZEND_NUM_ARGS() != 9 || zend_get_parameters_ex(9, &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
2960
ZEND_WRONG_PARAM_COUNT();
2963
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
2964
ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, DIM, -1, "Image", le_gd);
2966
convert_to_long_ex(SX);
2967
convert_to_long_ex(SY);
2968
convert_to_long_ex(SW);
2969
convert_to_long_ex(SH);
2970
convert_to_long_ex(DX);
2971
convert_to_long_ex(DY);
2972
convert_to_long_ex(PCT);
2974
srcX = Z_LVAL_PP(SX);
2975
srcY = Z_LVAL_PP(SY);
2976
srcH = Z_LVAL_PP(SH);
2977
srcW = Z_LVAL_PP(SW);
2978
dstX = Z_LVAL_PP(DX);
2979
dstY = Z_LVAL_PP(DY);
2980
pct = Z_LVAL_PP(PCT);
2982
gdImageCopyMergeGray(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
2988
/* {{{ 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)
2989
Copy and resize part of an image */
2990
PHP_FUNCTION(imagecopyresized)
2992
zval **SIM, **DIM, **SX, **SY, **SW, **SH, **DX, **DY, **DW, **DH;
2993
gdImagePtr im_dst, im_src;
2994
int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
2996
if (ZEND_NUM_ARGS() != 10 || zend_get_parameters_ex(10, &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
2997
ZEND_WRONG_PARAM_COUNT();
3000
ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, DIM, -1, "Image", le_gd);
3001
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
3003
convert_to_long_ex(SX);
3004
convert_to_long_ex(SY);
3005
convert_to_long_ex(SW);
3006
convert_to_long_ex(SH);
3007
convert_to_long_ex(DX);
3008
convert_to_long_ex(DY);
3009
convert_to_long_ex(DW);
3010
convert_to_long_ex(DH);
3012
srcX = Z_LVAL_PP(SX);
3013
srcY = Z_LVAL_PP(SY);
3014
srcH = Z_LVAL_PP(SH);
3015
srcW = Z_LVAL_PP(SW);
3016
dstX = Z_LVAL_PP(DX);
3017
dstY = Z_LVAL_PP(DY);
3018
dstH = Z_LVAL_PP(DH);
3019
dstW = Z_LVAL_PP(DW);
3021
if (dstW <= 0 || dstH <= 0 || srcW <= 0 || srcH <= 0) {
3022
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
3026
gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
3031
/* {{{ proto int imagesx(resource im)
3033
PHP_FUNCTION(imagesx)
3038
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &IM) == FAILURE) {
3039
ZEND_WRONG_PARAM_COUNT();
3042
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
3044
RETURN_LONG(gdImageSX(im));
3048
/* {{{ proto int imagesy(resource im)
3050
PHP_FUNCTION(imagesy)
3055
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &IM) == FAILURE) {
3056
ZEND_WRONG_PARAM_COUNT();
3059
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
3061
RETURN_LONG(gdImageSY(im));
3065
#ifdef ENABLE_GD_TTF
3066
#define TTFTEXT_DRAW 0
3067
#define TTFTEXT_BBOX 1
3070
#ifdef ENABLE_GD_TTF
3072
#if HAVE_LIBGD20 && HAVE_LIBFREETYPE && HAVE_GD_STRINGFTEX
3073
/* {{{ proto array imageftbbox(float size, float angle, string font_file, string text [, array extrainfo])
3074
Give the bounding box of a text using fonts via freetype2 */
3075
PHP_FUNCTION(imageftbbox)
3077
php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX, 1);
3081
/* {{{ proto array imagefttext(resource im, float size, float angle, int x, int y, int col, string font_file, string text [, array extrainfo])
3082
Write text to the image using fonts via freetype2 */
3083
PHP_FUNCTION(imagefttext)
3085
php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW, 1);
3090
/* {{{ proto array imagettfbbox(float size, float angle, string font_file, string text)
3091
Give the bounding box of a text using TrueType fonts */
3092
PHP_FUNCTION(imagettfbbox)
3094
php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX, 0);
3098
/* {{{ proto array imagettftext(resource im, float size, float angle, int x, int y, int col, string font_file, string text)
3099
Write text to the image using a TrueType font */
3100
PHP_FUNCTION(imagettftext)
3102
php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW, 0);
3106
/* {{{ php_imagettftext_common
3108
static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int mode, int extended)
3110
zval *IM, *EXT = NULL;
3112
int col = -1, x = -1, y = -1, str_len, fontname_len, i, brect[8];
3113
double ptsize, angle;
3114
unsigned char *str = NULL, *fontname = NULL;
3116
int argc = ZEND_NUM_ARGS();
3117
#if HAVE_GD_STRINGFTEX
3118
gdFTStringExtra strex = {0};
3121
#if !HAVE_GD_STRINGFTEX
3125
if (mode == TTFTEXT_BBOX) {
3126
if (argc < 4 || argc > ((extended) ? 5 : 4)) {
3127
ZEND_WRONG_PARAM_COUNT();
3128
} else if (zend_parse_parameters(argc TSRMLS_CC, "ddss|a", &ptsize, &angle, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
3132
if (argc < 8 || argc > ((extended) ? 9 : 8)) {
3133
ZEND_WRONG_PARAM_COUNT();
3134
} else if (zend_parse_parameters(argc TSRMLS_CC, "rddlllss|a", &IM, &ptsize, &angle, &x, &y, &col, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
3137
ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
3140
/* convert angle to radians */
3141
angle = angle * (M_PI/180);
3143
#if HAVE_GD_STRINGFTEX
3144
if (extended && EXT) { /* parse extended info */
3147
/* walk the assoc array */
3148
zend_hash_internal_pointer_reset_ex(HASH_OF(EXT), &pos);
3154
if (zend_hash_get_current_key_ex(HASH_OF(EXT), &key, NULL, &num_key, 0, &pos) != HASH_KEY_IS_STRING) {
3158
if (zend_hash_get_current_data_ex(HASH_OF(EXT), (void **) &item, &pos) == FAILURE) {
3162
if (strcmp("linespacing", key) == 0) {
3163
convert_to_double_ex(item);
3164
strex.flags |= gdFTEX_LINESPACE;
3165
strex.linespacing = Z_DVAL_PP(item);
3168
} while (zend_hash_move_forward_ex(HASH_OF(EXT), &pos) == SUCCESS);
3174
char tmp_font_path[MAXPATHLEN];
3176
if (VCWD_REALPATH(fontname, tmp_font_path)) {
3177
fontname = (unsigned char *) fontname;
3183
fontname = (unsigned char *) fontname;
3186
#ifdef USE_GD_IMGSTRTTF
3187
# if HAVE_GD_STRINGFTEX
3189
error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
3194
# if HAVE_GD_STRINGFT
3195
error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
3196
# elif HAVE_GD_STRINGTTF
3197
error = gdImageStringTTF(im, brect, col, fontname, ptsize, angle, x, y, str);
3200
#else /* !USE_GD_IMGSTRTTF */
3201
error = gdttf(im, brect, col, fontname, ptsize, angle, x, y, str);
3205
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error);
3209
array_init(return_value);
3211
/* return array with the text's bounding box */
3212
for (i = 0; i < 8; i++) {
3213
add_next_index_long(return_value, brect[i]);
3217
#endif /* ENABLE_GD_TTF */
3221
/* {{{ php_free_ps_font
3223
static void php_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
3225
int *font = (int *) rsrc->ptr;
3227
T1_DeleteFont(*font);
3232
/* {{{ php_free_ps_enc
3234
static void php_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
3236
char **enc = (char **) rsrc->ptr;
3238
T1_DeleteEncoding(enc);
3242
/* {{{ proto resource imagepsloadfont(string pathname)
3243
Load a new font from specified file */
3244
PHP_FUNCTION(imagepsloadfont)
3249
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
3250
ZEND_WRONG_PARAM_COUNT();
3253
convert_to_string_ex(file);
3255
f_ind = T1_AddFont(Z_STRVAL_PP(file));
3258
php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(f_ind));
3261
if (T1_LoadFont(f_ind)) {
3262
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't load the font");
3266
font = (int *) emalloc(sizeof(int));
3268
ZEND_REGISTER_RESOURCE(return_value, font, le_ps_font);
3272
/* {{{ proto int imagepscopyfont(int font_index)
3273
Make a copy of a font for purposes like extending or reenconding */
3274
/* The function in t1lib which this function uses seem to be buggy...
3275
PHP_FUNCTION(imagepscopyfont)
3279
gd_ps_font *nf_ind, *of_ind;
3281
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &fnt) == FAILURE) {
3282
ZEND_WRONG_PARAM_COUNT();
3285
convert_to_long_ex(fnt);
3287
of_ind = zend_list_find(Z_LVAL_PP(fnt), &type);
3289
if (type != le_ps_font) {
3290
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld is not a Type 1 font index", Z_LVAL_PP(fnt));
3294
nf_ind = emalloc(sizeof(gd_ps_font));
3295
nf_ind->font_id = T1_CopyFont(of_ind->font_id);
3297
if (nf_ind->font_id < 0) {
3298
l_ind = nf_ind->font_id;
3302
php_error_docref(NULL TSRMLS_CC, E_WARNING, "FontID %d is not loaded in memory", l_ind);
3306
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Tried to copy a logical font");
3310
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation fault in t1lib");
3314
php_error_docref(NULL TSRMLS_CC, E_WARNING, "An unknown error occurred in t1lib");
3321
l_ind = zend_list_insert(nf_ind, le_ps_font);
3327
/* {{{ proto bool imagepsfreefont(resource font_index)
3328
Free memory used by a font */
3329
PHP_FUNCTION(imagepsfreefont)
3334
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &fnt) == FAILURE) {
3335
ZEND_WRONG_PARAM_COUNT();
3338
ZEND_FETCH_RESOURCE(f_ind, int *, fnt, -1, "Type 1 font", le_ps_font);
3340
zend_list_delete(Z_LVAL_PP(fnt));
3345
/* {{{ proto bool imagepsencodefont(resource font_index, string filename)
3346
To change a fonts character encoding vector */
3347
PHP_FUNCTION(imagepsencodefont)
3353
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &fnt, &enc) == FAILURE) {
3354
ZEND_WRONG_PARAM_COUNT();
3357
convert_to_string_ex(enc);
3359
ZEND_FETCH_RESOURCE(f_ind, int *, fnt, -1, "Type 1 font", le_ps_font);
3361
if ((enc_vector = T1_LoadEncoding(Z_STRVAL_PP(enc))) == NULL) {
3362
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't load encoding vector from %s", Z_STRVAL_PP(enc));
3366
T1_DeleteAllSizes(*f_ind);
3367
if (T1_ReencodeFont(*f_ind, enc_vector)) {
3368
T1_DeleteEncoding(enc_vector);
3369
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't reencode font");
3373
zend_list_insert(enc_vector, le_ps_enc);
3379
/* {{{ proto bool imagepsextendfont(resource font_index, float extend)
3380
Extend or or condense (if extend < 1) a font */
3381
PHP_FUNCTION(imagepsextendfont)
3386
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &fnt, &ext) == FAILURE) {
3387
ZEND_WRONG_PARAM_COUNT();
3390
convert_to_double_ex(ext);
3392
ZEND_FETCH_RESOURCE(f_ind, int *, fnt, -1, "Type 1 font", le_ps_font);
3394
T1_DeleteAllSizes(*f_ind);
3396
if (Z_DVAL_PP(ext) <= 0) {
3397
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Second parameter %f out of range (must be > 0)", Z_DVAL_PP(ext));
3401
if (T1_ExtendFont(*f_ind, Z_DVAL_PP(ext)) != 0) {
3409
/* {{{ proto bool imagepsslantfont(resource font_index, float slant)
3411
PHP_FUNCTION(imagepsslantfont)
3416
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &fnt, &slt) == FAILURE) {
3417
ZEND_WRONG_PARAM_COUNT();
3420
convert_to_double_ex(slt);
3422
ZEND_FETCH_RESOURCE(f_ind, int *, fnt, -1, "Type 1 font", le_ps_font);
3424
if (T1_SlantFont(*f_ind, Z_DVAL_PP(slt)) != 0) {
3432
/* {{{ proto array imagepstext(resource image, string text, resource font, int size, int xcoord, int ycoord [, int space, int tightness, float angle, int antialias])
3433
Rasterize a string over an image */
3434
PHP_FUNCTION(imagepstext)
3438
long _fg, _bg, x, y, size, space = 0, aa_steps = 4, width = 0;
3440
int h_lines, v_lines, c_ind;
3441
int rd, gr, bl, fg_rd, fg_gr, fg_bl, bg_rd, bg_gr, bg_bl;
3443
int fg_al, bg_al, al;
3446
int amount_kern, add_width;
3447
double angle = 0.0, extend;
3448
unsigned long aa_greys[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
3451
T1_OUTLINE *char_path, *str_path;
3452
T1_TMATRIX *transform = NULL;
3455
int argc = ZEND_NUM_ARGS();
3457
if (argc != 8 && argc != 12) {
3458
ZEND_WRONG_PARAM_COUNT();
3461
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) {
3465
ZEND_FETCH_RESOURCE(bg_img, gdImagePtr, &img, -1, "Image", le_gd);
3466
ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
3468
/* Ensure that the provided colors are valid */
3470
if (_fg < 0 || (!gdImageTrueColor(bg_img) && _fg > gdImageColorsTotal(bg_img))) {
3472
if (_fg < 0 || _fg > gdImageColorsTotal(bg_img)) {
3474
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Foreground color index %ld out of range", _fg);
3479
if (_bg < 0 || (!gdImageTrueColor(bg_img) && _fg > gdImageColorsTotal(bg_img))) {
3481
if (_bg < 0 || _bg > gdImageColorsTotal(bg_img)) {
3483
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Background color index %ld out of range", _bg);
3487
fg_rd = gdImageRed (bg_img, _fg);
3488
fg_gr = gdImageGreen(bg_img, _fg);
3489
fg_bl = gdImageBlue (bg_img, _fg);
3491
fg_al = gdImageAlpha(bg_img, _fg);
3494
bg_rd = gdImageRed (bg_img, _bg);
3495
bg_gr = gdImageGreen(bg_img, _bg);
3496
bg_bl = gdImageBlue (bg_img, _bg);
3498
bg_al = gdImageAlpha(bg_img, _bg);
3501
for (i = 0; i < aa_steps; i++) {
3502
rd = bg_rd + (double) (fg_rd - bg_rd) / aa_steps * (i + 1);
3503
gr = bg_gr + (double) (fg_gr - bg_gr) / aa_steps * (i + 1);
3504
bl = bg_bl + (double) (fg_bl - bg_bl) / aa_steps * (i + 1);
3506
al = bg_al + (double) (fg_al - bg_al) / aa_steps * (i + 1);
3507
aa[i] = gdImageColorResolveAlpha(bg_img, rd, gr, bl, al);
3509
aa[i] = gdImageColorResolve(bg_img, rd, gr, bl);
3513
T1_AASetBitsPerPixel(8);
3517
T1_AASetGrayValues(0, 1, 2, 3, 4);
3518
T1_AASetLevel(T1_AA_LOW);
3521
T1_AAHSetGrayValues(aa_greys);
3522
T1_AASetLevel(T1_AA_HIGH);
3525
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value %ld as number of steps for antialiasing", aa_steps);
3530
transform = T1_RotateMatrix(NULL, angle);
3534
extend = T1_GetExtend(*f_ind);
3535
str_path = T1_GetCharOutline(*f_ind, str[0], size, transform);
3539
php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(T1_errno));
3544
for (i = 1; i < str_len; i++) {
3545
amount_kern = (int) T1_GetKerning(*f_ind, str[i - 1], str[i]);
3546
amount_kern += str[i - 1] == ' ' ? space : 0;
3547
add_width = (int) (amount_kern + width) / extend;
3549
char_path = T1_GetMoveOutline(*f_ind, add_width, 0, 0, size, transform);
3550
str_path = T1_ConcatOutlines(str_path, char_path);
3552
char_path = T1_GetCharOutline(*f_ind, str[i], size, transform);
3553
str_path = T1_ConcatOutlines(str_path, char_path);
3555
str_img = T1_AAFillOutline(str_path, 0);
3557
str_img = T1_AASetString(*f_ind, str, str_len, space, T1_KERNING, size, transform);
3560
php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(T1_errno));
3564
h_lines = str_img->metrics.ascent - str_img->metrics.descent;
3565
v_lines = str_img->metrics.rightSideBearing - str_img->metrics.leftSideBearing;
3567
for (i = 0; i < v_lines; i++) {
3568
for (j = 0; j < h_lines; j++) {
3569
switch (str_img->bits[j * v_lines + i]) {
3573
c_ind = aa[str_img->bits[j * v_lines + i] - 1];
3574
gdImageSetPixel(bg_img, x + str_img->metrics.leftSideBearing + i, y - str_img->metrics.ascent + j, c_ind);
3580
array_init(return_value);
3582
add_next_index_long(return_value, str_img->metrics.leftSideBearing);
3583
add_next_index_long(return_value, str_img->metrics.descent);
3584
add_next_index_long(return_value, str_img->metrics.rightSideBearing);
3585
add_next_index_long(return_value, str_img->metrics.ascent);
3589
/* {{{ proto array imagepsbbox(string text, resource font, int size [, int space, int tightness, int angle])
3590
Return the bounding box needed by a string if rasterized */
3591
PHP_FUNCTION(imagepsbbox)
3593
zval **str, **fnt, **sz, **sp, **wd, **ang;
3594
int i, space, add_width = 0, char_width, amount_kern;
3595
int cur_x, cur_y, dx, dy;
3596
int x1, y1, x2, y2, x3, y3, x4, y4;
3599
double angle, sin_a = 0, cos_a = 0;
3600
BBox char_bbox, str_bbox = {0, 0, 0, 0};
3602
switch (ZEND_NUM_ARGS()) {
3604
if (zend_get_parameters_ex(3, &str, &fnt, &sz) == FAILURE) {
3610
if (zend_get_parameters_ex(6, &str, &fnt, &sz, &sp, &wd, &ang) == FAILURE) {
3613
convert_to_long_ex(sp);
3614
convert_to_long_ex(wd);
3615
convert_to_double_ex(ang);
3616
space = Z_LVAL_PP(sp);
3617
add_width = Z_LVAL_PP(wd);
3618
angle = Z_DVAL_PP(ang) * M_PI / 180;
3621
per_char = add_width || angle ? 1 : 0;
3624
ZEND_WRONG_PARAM_COUNT();
3627
ZEND_FETCH_RESOURCE(f_ind, int *, fnt, -1, "Type 1 font", le_ps_font);
3629
convert_to_string_ex(str);
3630
convert_to_long_ex(sz);
3632
#define max(a, b) (a > b ? a : b)
3633
#define min(a, b) (a < b ? a : b)
3634
#define new_x(a, b) (int) ((a) * cos_a - (b) * sin_a)
3635
#define new_y(a, b) (int) ((a) * sin_a + (b) * cos_a)
3638
space += T1_GetCharWidth(*f_ind, ' ');
3641
for (i = 0; i < Z_STRLEN_PP(str); i++) {
3642
if (Z_STRVAL_PP(str)[i] == ' ') {
3643
char_bbox.llx = char_bbox.lly = char_bbox.ury = 0;
3644
char_bbox.urx = char_width = space;
3646
char_bbox = T1_GetCharBBox(*f_ind, Z_STRVAL_PP(str)[i]);
3647
char_width = T1_GetCharWidth(*f_ind, Z_STRVAL_PP(str)[i]);
3649
amount_kern = i ? T1_GetKerning(*f_ind, Z_STRVAL_PP(str)[i - 1], Z_STRVAL_PP(str)[i]) : 0;
3651
/* Transfer character bounding box to right place */
3652
x1 = new_x(char_bbox.llx, char_bbox.lly) + cur_x;
3653
y1 = new_y(char_bbox.llx, char_bbox.lly) + cur_y;
3654
x2 = new_x(char_bbox.llx, char_bbox.ury) + cur_x;
3655
y2 = new_y(char_bbox.llx, char_bbox.ury) + cur_y;
3656
x3 = new_x(char_bbox.urx, char_bbox.ury) + cur_x;
3657
y3 = new_y(char_bbox.urx, char_bbox.ury) + cur_y;
3658
x4 = new_x(char_bbox.urx, char_bbox.lly) + cur_x;
3659
y4 = new_y(char_bbox.urx, char_bbox.lly) + cur_y;
3661
/* Find min & max values and compare them with current bounding box */
3662
str_bbox.llx = min(str_bbox.llx, min(x1, min(x2, min(x3, x4))));
3663
str_bbox.lly = min(str_bbox.lly, min(y1, min(y2, min(y3, y4))));
3664
str_bbox.urx = max(str_bbox.urx, max(x1, max(x2, max(x3, x4))));
3665
str_bbox.ury = max(str_bbox.ury, max(y1, max(y2, max(y3, y4))));
3667
/* Move to the next base point */
3668
dx = new_x(char_width + add_width + amount_kern, 0);
3669
dy = new_y(char_width + add_width + amount_kern, 0);
3673
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);
3678
str_bbox = T1_GetStringBBox(*f_ind, Z_STRVAL_PP(str), Z_STRLEN_PP(str), space, T1_KERNING);
3685
array_init(return_value);
3687
printf("%d %d %d %d\n", str_bbox.llx, str_bbox.lly, str_bbox.urx, str_bbox.ury);
3689
add_next_index_long(return_value, (int) ceil(((double) str_bbox.llx)*Z_LVAL_PP(sz)/1000));
3690
add_next_index_long(return_value, (int) ceil(((double) str_bbox.lly)*Z_LVAL_PP(sz)/1000));
3691
add_next_index_long(return_value, (int) ceil(((double) str_bbox.urx)*Z_LVAL_PP(sz)/1000));
3692
add_next_index_long(return_value, (int) ceil(((double) str_bbox.ury)*Z_LVAL_PP(sz)/1000));
3698
/* {{{ proto bool image2wbmp(resource im [, string filename [, int threshold]])
3699
Output WBMP image to browser or file */
3700
PHP_FUNCTION(image2wbmp)
3702
_php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_CONVERT_WBM, "WBMP", _php_image_bw_convert);
3705
#endif /* HAVE_GD_WBMP */
3707
#if defined(HAVE_GD_JPG) && defined(HAVE_GD_WBMP)
3708
/* {{{ proto bool jpeg2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold)
3709
Convert JPEG image to WBMP image */
3710
PHP_FUNCTION(jpeg2wbmp)
3712
_php_image_convert(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG);
3717
#if defined(HAVE_GD_PNG) && defined(HAVE_GD_WBMP)
3718
/* {{{ proto bool png2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold)
3719
Convert PNG image to WBMP image */
3720
PHP_FUNCTION(png2wbmp)
3722
_php_image_convert(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG);
3728
/* {{{ _php_image_bw_convert
3729
* It converts a gd Image to bw using a threshold value */
3730
static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold)
3734
int color, color_org, median;
3735
int dest_height = gdImageSY(im_org);
3736
int dest_width = gdImageSX(im_org);
3740
im_dest = gdImageCreate(dest_width, dest_height);
3741
if (im_dest == NULL) {
3742
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate temporary buffer");
3746
white = gdImageColorAllocate(im_dest, 255, 255, 255);
3748
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
3752
black = gdImageColorAllocate(im_dest, 0, 0, 0);
3754
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
3759
if (im_org->trueColor) {
3760
gdImageTrueColorToPalette(im_org, 1, 256);
3764
for (y = 0; y < dest_height; y++) {
3765
for (x = 0; x < dest_width; x++) {
3766
color_org = gdImageGetPixel(im_org, x, y);
3767
median = (im_org->red[color_org] + im_org->green[color_org] + im_org->blue[color_org]) / 3;
3768
if (median < threshold) {
3773
gdImageSetPixel (im_dest, x, y, color);
3777
gdImageWBMPCtx (im_dest, black, out);
3779
gdImageWBMP (im_dest, black, out);
3785
/* {{{ _php_image_convert
3786
* _php_image_convert converts jpeg/png images to wbmp and resizes them as needed */
3787
static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
3789
zval **f_org, **f_dest, **height, **width, **threshold;
3790
gdImagePtr im_org, im_dest, im_tmp;
3791
char *fn_org = NULL;
3792
char *fn_dest = NULL;
3794
int argc = ZEND_NUM_ARGS();
3795
int dest_height = -1;
3796
int dest_width = -1;
3797
int org_height, org_width;
3799
int color, color_org, median;
3802
float x_ratio, y_ratio;
3804
if (argc != 5 || zend_get_parameters_ex(argc, &f_org, &f_dest, &height, &width, &threshold) == FAILURE) {
3805
ZEND_WRONG_PARAM_COUNT();
3808
convert_to_string_ex(f_org);
3809
convert_to_string_ex(f_dest);
3810
convert_to_long_ex(height);
3811
convert_to_long_ex(width);
3812
convert_to_long_ex(threshold);
3814
fn_org = Z_STRVAL_PP(f_org);
3815
fn_dest = Z_STRVAL_PP(f_dest);
3816
dest_height = Z_LVAL_PP(height);
3817
dest_width = Z_LVAL_PP(width);
3818
int_threshold = Z_LVAL_PP(threshold);
3820
/* Check threshold value */
3821
if (int_threshold < 0 || int_threshold > 8) {
3822
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'", int_threshold);
3826
/* Check origin file */
3827
if (!fn_org || fn_org == empty_string || php_check_open_basedir(fn_org TSRMLS_CC)) {
3828
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid origin filename '%s'", fn_org);
3832
/* Check destination file */
3833
if (!fn_dest || fn_dest == empty_string || php_check_open_basedir(fn_dest TSRMLS_CC)) {
3834
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid destination filename '%s'", fn_dest);
3838
/* Open origin file */
3839
org = VCWD_FOPEN(fn_org, "rb");
3841
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for reading", fn_org);
3845
/* Open destination file */
3846
dest = VCWD_FOPEN(fn_dest, "wb");
3848
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn_dest);
3852
switch (image_type) {
3853
#ifdef HAVE_GD_GIF_READ
3854
case PHP_GDIMG_TYPE_GIF:
3855
im_org = gdImageCreateFromGif(org);
3856
if (im_org == NULL) {
3857
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid GIF file", fn_dest);
3861
#endif /* HAVE_GD_GIF_READ */
3864
case PHP_GDIMG_TYPE_JPG:
3865
im_org = gdImageCreateFromJpeg(org);
3866
if (im_org == NULL) {
3867
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid JPEG file", fn_dest);
3871
#endif /* HAVE_GD_JPG */
3875
case PHP_GDIMG_TYPE_PNG:
3876
im_org = gdImageCreateFromPng(org);
3877
if (im_org == NULL) {
3878
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid PNG file", fn_dest);
3882
#endif /* HAVE_GD_PNG */
3885
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Format not supported");
3890
org_width = gdImageSX (im_org);
3891
org_height = gdImageSY (im_org);
3893
x_ratio = (float) org_width / (float) dest_width;
3894
y_ratio = (float) org_height / (float) dest_height;
3896
if (x_ratio > 1 && y_ratio > 1) {
3897
if (y_ratio > x_ratio) {
3902
dest_width = (int) (org_width / x_ratio);
3903
dest_height = (int) (org_height / y_ratio);
3905
x_ratio = (float) dest_width / (float) org_width;
3906
y_ratio = (float) dest_height / (float) org_height;
3908
if (y_ratio < x_ratio) {
3913
dest_width = (int) (org_width * x_ratio);
3914
dest_height = (int) (org_height * y_ratio);
3917
im_tmp = gdImageCreate (dest_width, dest_height);
3918
if (im_tmp == NULL ) {
3919
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate temporary buffer");
3923
gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, dest_height, org_width, org_height);
3925
gdImageDestroy(im_org);
3929
im_dest = gdImageCreate(dest_width, dest_height);
3930
if (im_dest == NULL) {
3931
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate destination buffer");
3935
white = gdImageColorAllocate(im_dest, 255, 255, 255);
3937
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
3941
black = gdImageColorAllocate(im_dest, 0, 0, 0);
3943
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
3947
int_threshold = int_threshold * 32;
3949
for (y = 0; y < dest_height; y++) {
3950
for (x = 0; x < dest_width; x++) {
3951
color_org = gdImageGetPixel (im_tmp, x, y);
3952
median = (im_tmp->red[color_org] + im_tmp->green[color_org] + im_tmp->blue[color_org]) / 3;
3953
if (median < int_threshold) {
3958
gdImageSetPixel (im_dest, x, y, color);
3962
gdImageDestroy (im_tmp );
3964
gdImageWBMP(im_dest, black , dest);
3969
gdImageDestroy(im_dest);
3974
#endif /* HAVE_GD_WBMP */
3976
#endif /* HAVE_LIBGD */
3978
/* Section Filters */
3979
#ifdef HAVE_GD_BUNDLED
3981
#define PHP_GD_SINGLE_RES \
3983
gdImagePtr im_src; \
3984
if (zend_get_parameters_ex(1, &SIM) == FAILURE) { \
3987
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd); \
3988
if (im_src == NULL) { \
3992
static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
3996
if (gdImageNegate(im_src) == 1) {
4003
static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
4007
if (gdImageGrayScale(im_src) == 1) {
4014
static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
4018
long brightness, tmp;
4020
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zll", &SIM, &tmp, &brightness) == FAILURE) {
4024
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
4026
if (im_src == NULL) {
4030
if (gdImageBrightness(im_src, (int)brightness) == 1) {
4037
static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
4043
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &SIM, &tmp, &contrast) == FAILURE) {
4047
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
4049
if (im_src == NULL) {
4053
if (gdImageContrast(im_src, (int)contrast) == 1) {
4060
static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
4066
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &SIM, &tmp, &r, &g, &b) == FAILURE) {
4070
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
4072
if (im_src == NULL) {
4076
if (gdImageColor(im_src, (int) r, (int) g, (int) b) == 1) {
4083
static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
4087
if (gdImageEdgeDetectQuick(im_src) == 1) {
4094
static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
4098
if (gdImageEmboss(im_src) == 1) {
4105
static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
4109
if (gdImageGaussianBlur(im_src) == 1) {
4116
static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
4120
if (gdImageSelectiveBlur(im_src) == 1) {
4127
static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
4131
if (gdImageMeanRemoval(im_src) == 1) {
4138
static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
4145
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rld", &SIM, &tmp, &weight) == FAILURE) {
4149
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
4151
if (im_src == NULL) {
4155
if (gdImageSmooth(im_src, (float)weight)==1) {
4162
/* {{{ proto bool imagefilter(resource src_im, int filtertype, [args] )
4163
Applies Filter an image using a custom angle */
4164
PHP_FUNCTION(imagefilter)
4168
typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
4170
image_filter filters[] =
4172
php_image_filter_negate ,
4173
php_image_filter_grayscale,
4174
php_image_filter_brightness,
4175
php_image_filter_contrast,
4176
php_image_filter_colorize,
4177
php_image_filter_edgedetect,
4178
php_image_filter_emboss,
4179
php_image_filter_gaussian_blur,
4180
php_image_filter_selective_blur,
4181
php_image_filter_mean_removal,
4182
php_image_filter_smooth
4185
if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 5 || zend_parse_parameters(2 TSRMLS_CC, "rl", &tmp, &filtertype) == FAILURE) {
4186
ZEND_WRONG_PARAM_COUNT();
4189
if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
4190
filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
4194
/* End section: Filters */
4196
/* {{{ proto bool imageantialias(resource im, bool on)
4197
Should antialiased functions used or not*/
4198
PHP_FUNCTION(imageantialias)
4203
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &IM, &alias) == FAILURE) {
4204
ZEND_WRONG_PARAM_COUNT();
4207
ZEND_FETCH_RESOURCE(im, gdImagePtr, IM, -1, "Image", le_gd);
4209
convert_to_boolean_ex(alias);
4210
gdImageAntialias(im, Z_LVAL_PP(alias));
4222
* vim600: sw=4 ts=4 fdm=marker
4223
* vim<600: sw=4 ts=4