Skip to content

Commit 6aedadc

Browse files
oerdnjremicollet
authored andcommitted
Allow to use new GD-2.1.0 function from system library
1 parent 9155d17 commit 6aedadc

File tree

4 files changed

+95
-52
lines changed

4 files changed

+95
-52
lines changed

ext/gd/config.m4

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ AC_DEFUN([PHP_GD_CHECK_VERSION],[
288288
PHP_CHECK_LIBRARY(gd, gdImagePixelate, [AC_DEFINE(HAVE_GD_IMAGE_PIXELATE, 1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
289289
PHP_CHECK_LIBRARY(gd, gdImageFlipBoth, [AC_DEFINE(HAVE_GD_IMAGE_FLIP, 1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
290290
PHP_CHECK_LIBRARY(gd, gdImageCreateFromJpegEx,[AC_DEFINE(HAVE_GD_JPGEX, 1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
291+
PHP_CHECK_LIBRARY(gd, gdSetErrorMethod, [AC_DEFINE(HAVE_LIBGD21, 1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
291292
])
292293

293294
dnl
@@ -328,6 +329,7 @@ dnl These are always available with bundled library
328329
AC_DEFINE(HAVE_LIBGD15, 1, [ ])
329330
AC_DEFINE(HAVE_LIBGD20, 1, [ ])
330331
AC_DEFINE(HAVE_LIBGD204, 1, [ ])
332+
AC_DEFINE(HAVE_LIBGD21, 1, [ ])
331333
AC_DEFINE(HAVE_GD_IMAGESETTILE, 1, [ ])
332334
AC_DEFINE(HAVE_GD_IMAGESETBRUSH, 1, [ ])
333335
AC_DEFINE(HAVE_GDIMAGECOLORRESOLVE, 1, [ ])
@@ -384,8 +386,12 @@ else
384386

385387
if test "$PHP_GD" != "no"; then
386388
GD_MODULE_TYPE=external
387-
extra_sources="gdcache.c libgd/gd_compat.c libgd/gd_filter.c libgd/gd_pixelate.c libgd/gd_arc.c \
388-
libgd/gd_rotate.c libgd/gd_color.c"
389+
if test -n "$HAVE_LIBGD21"; then
390+
extra_sources="libgd/gd_compat.c"
391+
else
392+
extra_sources="gdcache.c libgd/gd_compat.c libgd/gd_filter.c libgd/gd_pixelate.c libgd/gd_arc.c \
393+
libgd/gd_rotate.c libgd/gd_color.c"
394+
fi
389395

390396
dnl Various checks for GD features
391397
PHP_GD_ZLIB
@@ -419,22 +425,26 @@ dnl Library path
419425

420426
PHP_EXPAND_PATH($GD_INCLUDE, GD_INCLUDE)
421427

422-
dnl
423-
dnl Check for gd 2.0.4 greater availability
424-
dnl
425-
old_CPPFLAGS=$CPPFLAGS
426-
CPPFLAGS=-I$GD_INCLUDE
427-
AC_TRY_COMPILE([
428+
if test -n "$HAVE_LIBGD21"; then
429+
AC_DEFINE(HAVE_LIBGD204, 1, [ ])
430+
else
431+
dnl
432+
dnl Check for gd 2.0.4 greater availability
433+
dnl
434+
old_CPPFLAGS=$CPPFLAGS
435+
CPPFLAGS=-I$GD_INCLUDE
436+
AC_TRY_COMPILE([
428437
#include <gd.h>
429438
#include <stdlib.h>
430439
], [
431440
gdIOCtx *ctx;
432441
ctx = malloc(sizeof(gdIOCtx));
433442
ctx->gd_free = 1;
434-
], [
435-
AC_DEFINE(HAVE_LIBGD204, 1, [ ])
436-
])
437-
CPPFLAGS=$old_CPPFLAGS
443+
], [
444+
AC_DEFINE(HAVE_LIBGD204, 1, [ ])
445+
])
446+
CPPFLAGS=$old_CPPFLAGS
447+
fi
438448

439449
fi
440450
fi
@@ -445,16 +455,18 @@ dnl
445455
if test "$PHP_GD" != "no"; then
446456
PHP_NEW_EXTENSION(gd, gd.c $extra_sources, $ext_shared,, \\$(GDLIB_CFLAGS))
447457

448-
PHP_ADD_BUILD_DIR($ext_builddir/libgd)
449-
450-
if test "$GD_MODULE_TYPE" = "builtin"; then
458+
if test "$GD_MODULE_TYPE" = "builtin"; then
459+
PHP_ADD_BUILD_DIR($ext_builddir/libgd)
451460
GDLIB_CFLAGS="-I$ext_srcdir/libgd $GDLIB_CFLAGS"
452461
GD_HEADER_DIRS="ext/gd/ ext/gd/libgd/"
453462

454463
PHP_TEST_BUILD(foobar, [], [
455464
AC_MSG_ERROR([GD build test failed. Please check the config.log for details.])
456465
], [ $GD_SHARED_LIBADD ], [char foobar () {}])
457466
else
467+
if test "$HAVE_LIBGD21" = "no"; then
468+
PHP_ADD_BUILD_DIR($ext_builddir/libgd)
469+
fi
458470
GD_HEADER_DIRS="ext/gd/"
459471
GDLIB_CFLAGS="-I$GD_INCLUDE $GDLIB_CFLAGS"
460472
PHP_ADD_INCLUDE($GD_INCLUDE)

ext/gd/gd.c

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ static void php_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC);
7575
#include <gdfontl.h> /* 4 Large font */
7676
#include <gdfontg.h> /* 5 Giant font */
7777

78-
#ifdef HAVE_GD_WBMP
79-
#include "libgd/wbmp.h"
80-
#endif
8178
#ifdef ENABLE_GD_TTF
8279
# ifdef HAVE_LIBFREETYPE
8380
# include <ft2build.h>
@@ -241,7 +238,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_imagesavealpha, 0)
241238
ZEND_ARG_INFO(0, save)
242239
ZEND_END_ARG_INFO()
243240

244-
#if HAVE_GD_BUNDLED
241+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
245242
ZEND_BEGIN_ARG_INFO(arginfo_imagelayereffect, 0)
246243
ZEND_ARG_INFO(0, im)
247244
ZEND_ARG_INFO(0, effect)
@@ -368,7 +365,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromxbm, 0)
368365
ZEND_END_ARG_INFO()
369366
#endif
370367

371-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
368+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
372369
ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromxpm, 0)
373370
ZEND_ARG_INFO(0, filename)
374371
ZEND_END_ARG_INFO()
@@ -398,7 +395,7 @@ ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd2part, 0)
398395
ZEND_END_ARG_INFO()
399396
#endif
400397

401-
#if HAVE_GD_BUNDLED
398+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
402399
ZEND_BEGIN_ARG_INFO_EX(arginfo_imagexbm, 0, 0, 2)
403400
ZEND_ARG_INFO(0, im)
404401
ZEND_ARG_INFO(0, filename)
@@ -897,7 +894,9 @@ ZEND_BEGIN_ARG_INFO(arginfo_imageantialias, 0)
897894
ZEND_ARG_INFO(0, im)
898895
ZEND_ARG_INFO(0, on)
899896
ZEND_END_ARG_INFO()
897+
#endif
900898

899+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
901900
ZEND_BEGIN_ARG_INFO(arginfo_imagecrop, 0)
902901
ZEND_ARG_INFO(0, im)
903902
ZEND_ARG_INFO(0, rect)
@@ -1002,6 +1001,8 @@ const zend_function_entry gd_functions[] = {
10021001

10031002
#ifdef HAVE_GD_BUNDLED
10041003
PHP_FE(imageantialias, arginfo_imageantialias)
1004+
#endif
1005+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
10051006
PHP_FE(imagecrop, arginfo_imagecrop)
10061007
PHP_FE(imagecropauto, arginfo_imagecropauto)
10071008
PHP_FE(imagescale, arginfo_imagescale)
@@ -1039,7 +1040,7 @@ const zend_function_entry gd_functions[] = {
10391040
#ifdef HAVE_GD_XBM
10401041
PHP_FE(imagecreatefromxbm, arginfo_imagecreatefromxbm)
10411042
#endif
1042-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
1043+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
10431044
PHP_FE(imagecreatefromxpm, arginfo_imagecreatefromxpm)
10441045
#endif
10451046
PHP_FE(imagecreatefromgd, arginfo_imagecreatefromgd)
@@ -1119,7 +1120,7 @@ const zend_function_entry gd_functions[] = {
11191120
#ifdef HAVE_GD_WBMP
11201121
PHP_FE(image2wbmp, arginfo_image2wbmp)
11211122
#endif
1122-
#if HAVE_GD_BUNDLED
1123+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
11231124
PHP_FE(imagelayereffect, arginfo_imagelayereffect)
11241125
PHP_FE(imagexbm, arginfo_imagexbm)
11251126
#endif
@@ -1187,6 +1188,18 @@ static void php_free_gd_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
11871188
}
11881189
/* }}} */
11891190

1191+
#ifdef HAVE_LIBGD21
1192+
/* {{{ php_gd_error_method
1193+
*/
1194+
void php_gd_error_method(int type, const char *format, va_list args)
1195+
{
1196+
TSRMLS_FETCH();
1197+
1198+
php_verror(NULL, "", type, format, args TSRMLS_CC);
1199+
}
1200+
/* }}} */
1201+
#endif
1202+
11901203
/* {{{ PHP_MSHUTDOWN_FUNCTION
11911204
*/
11921205
#if HAVE_LIBT1 || HAVE_GD_FONTMUTEX
@@ -1222,7 +1235,9 @@ PHP_MINIT_FUNCTION(gd)
12221235
le_ps_font = zend_register_list_destructors_ex(php_free_ps_font, NULL, "gd PS font", module_number);
12231236
le_ps_enc = zend_register_list_destructors_ex(php_free_ps_enc, NULL, "gd PS encoding", module_number);
12241237
#endif
1225-
1238+
#ifdef HAVE_LIBGD21
1239+
gdSetErrorMethod(php_gd_error_method);
1240+
#endif
12261241
REGISTER_INI_ENTRIES();
12271242

12281243
REGISTER_LONG_CONSTANT("IMG_GIF", 1, CONST_CS | CONST_PERSISTENT);
@@ -1258,7 +1273,7 @@ PHP_MINIT_FUNCTION(gd)
12581273
REGISTER_LONG_CONSTANT("IMG_FLIP_VERTICAL", GD_FLIP_VERTICAL, CONST_CS | CONST_PERSISTENT);
12591274
REGISTER_LONG_CONSTANT("IMG_FLIP_BOTH", GD_FLIP_BOTH, CONST_CS | CONST_PERSISTENT);
12601275
#endif
1261-
#if HAVE_GD_BUNDLED
1276+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
12621277
REGISTER_LONG_CONSTANT("IMG_EFFECT_REPLACE", gdEffectReplace, CONST_CS | CONST_PERSISTENT);
12631278
REGISTER_LONG_CONSTANT("IMG_EFFECT_ALPHABLEND", gdEffectAlphaBlend, CONST_CS | CONST_PERSISTENT);
12641279
REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
@@ -1367,7 +1382,7 @@ PHP_RSHUTDOWN_FUNCTION(gd)
13671382
#endif
13681383
/* }}} */
13691384

1370-
#if HAVE_GD_BUNDLED
1385+
#if defined(HAVE_GD_BUNDLED)
13711386
#define PHP_GD_VERSION_STRING "bundled (2.1.0 compatible)"
13721387
#else
13731388
# ifdef GD_VERSION_STRING
@@ -1413,8 +1428,6 @@ PHP_MINFO_FUNCTION(gd)
14131428
php_info_print_table_row(2, "T1Lib Support", "enabled");
14141429
#endif
14151430

1416-
/* this next part is stupid ... if I knew better, I'd put them all on one row (cmv) */
1417-
14181431
#ifdef HAVE_GD_GIF_READ
14191432
php_info_print_table_row(2, "GIF Read Support", "enabled");
14201433
#endif
@@ -1423,21 +1436,18 @@ PHP_MINFO_FUNCTION(gd)
14231436
#endif
14241437
#ifdef HAVE_GD_JPG
14251438
{
1426-
char tmp[12];
1427-
snprintf(tmp, sizeof(tmp), "%s", gdJpegGetVersionString());
14281439
php_info_print_table_row(2, "JPEG Support", "enabled");
1429-
php_info_print_table_row(2, "libJPEG Version", tmp);
1440+
php_info_print_table_row(2, "libJPEG Version", gdJpegGetVersionString());
14301441
}
14311442
#endif
1432-
14331443
#ifdef HAVE_GD_PNG
14341444
php_info_print_table_row(2, "PNG Support", "enabled");
14351445
php_info_print_table_row(2, "libPNG Version", gdPngGetVersionString());
14361446
#endif
14371447
#ifdef HAVE_GD_WBMP
14381448
php_info_print_table_row(2, "WBMP Support", "enabled");
14391449
#endif
1440-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
1450+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
14411451
php_info_print_table_row(2, "XPM Support", "enabled");
14421452
{
14431453
char tmp[12];
@@ -1448,7 +1458,7 @@ PHP_MINFO_FUNCTION(gd)
14481458
#ifdef HAVE_GD_XBM
14491459
php_info_print_table_row(2, "XBM Support", "enabled");
14501460
#endif
1451-
#if defined(USE_GD_JISX0208) && defined(HAVE_GD_BUNDLED)
1461+
#if defined(USE_GD_JISX0208) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
14521462
php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
14531463
#endif
14541464
#ifdef HAVE_GD_WEBP
@@ -1512,7 +1522,7 @@ PHP_FUNCTION(gd_info)
15121522
#else
15131523
add_assoc_bool(return_value, "WBMP Support", 0);
15141524
#endif
1515-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
1525+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
15161526
add_assoc_bool(return_value, "XPM Support", 1);
15171527
#else
15181528
add_assoc_bool(return_value, "XPM Support", 0);
@@ -1522,7 +1532,7 @@ PHP_FUNCTION(gd_info)
15221532
#else
15231533
add_assoc_bool(return_value, "XBM Support", 0);
15241534
#endif
1525-
#if defined(USE_GD_JISX0208) && defined(HAVE_GD_BUNDLED)
1535+
#if defined(USE_GD_JISX0208) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
15261536
add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
15271537
#else
15281538
add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
@@ -1655,13 +1665,9 @@ PHP_FUNCTION(imageloadfont)
16551665
body_size = font->w * font->h * font->nchars;
16561666
}
16571667

1658-
if (overflow2(font->nchars, font->h)) {
1659-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font, invalid font header");
1660-
efree(font);
1661-
php_stream_close(stream);
1662-
RETURN_FALSE;
1663-
}
1664-
if (overflow2(font->nchars * font->h, font->w )) {
1668+
if ((font->nchars <= 0 || font->h <= 0 || font->w <= 0 ) || \
1669+
(font->nchars > INT_MAX / font->h) || \
1670+
(font->nchars * font->h > INT_MAX / font->w)) {
16651671
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font, invalid font header");
16661672
efree(font);
16671673
php_stream_close(stream);
@@ -1984,7 +1990,7 @@ PHP_FUNCTION(imagesavealpha)
19841990
}
19851991
/* }}} */
19861992

1987-
#if HAVE_GD_BUNDLED
1993+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
19881994
/* {{{ proto bool imagelayereffect(resource im, int effect)
19891995
Set the alpha blending flag to use the bundled libgd layering effects */
19901996
PHP_FUNCTION(imagelayereffect)
@@ -2375,7 +2381,7 @@ PHP_FUNCTION(imagetypes)
23752381
#ifdef HAVE_GD_WBMP
23762382
ret |= 8;
23772383
#endif
2378-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
2384+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
23792385
ret |= 16;
23802386
#endif
23812387

@@ -2387,6 +2393,23 @@ PHP_FUNCTION(imagetypes)
23872393
}
23882394
/* }}} */
23892395

2396+
/* {{{ _php_ctx_getmbi
2397+
*/
2398+
2399+
static _php_ctx_getmbi(gdIOCtx *ctx)
2400+
{
2401+
int i, mbi = 0;
2402+
2403+
do {
2404+
i = (ctx->getC)(ctx);
2405+
if (i < 0) {
2406+
break;
2407+
}
2408+
mbi = (mbi << 7) | (i & 0x7f);
2409+
} while (i & 0x80);
2410+
}
2411+
/* }}} */
2412+
23902413
/* {{{ _php_image_type
23912414
*/
23922415
static const char php_sig_gd2[3] = {'g', 'd', '2'};
@@ -2416,7 +2439,7 @@ static int _php_image_type (char data[8])
24162439
gdIOCtx *io_ctx;
24172440
io_ctx = gdNewDynamicCtxEx(8, data, 0);
24182441
if (io_ctx) {
2419-
if (getmbi((int(*)(void *)) io_ctx->getC, io_ctx) == 0 && skipheader((int(*)(void *)) io_ctx->getC, io_ctx) == 0 ) {
2442+
if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
24202443
#if HAVE_LIBGD204
24212444
io_ctx->gd_free(io_ctx);
24222445
#else
@@ -2662,7 +2685,7 @@ static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type,
26622685
case PHP_GDIMG_TYPE_GD2PART:
26632686
im = (*func_p)(fp, srcx, srcy, width, height);
26642687
break;
2665-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
2688+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
26662689
case PHP_GDIMG_TYPE_XPM:
26672690
im = gdImageCreateFromXpm(file);
26682691
break;
@@ -2752,7 +2775,7 @@ PHP_FUNCTION(imagecreatefromxbm)
27522775
/* }}} */
27532776
#endif /* HAVE_GD_XBM */
27542777

2755-
#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
2778+
#if defined(HAVE_GD_XPM) && (defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21))
27562779
/* {{{ proto resource imagecreatefromxpm(string filename)
27572780
Create a new image from XPM file or URL */
27582781
PHP_FUNCTION(imagecreatefromxpm)
@@ -2960,7 +2983,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
29602983

29612984
/* {{{ proto int imagexbm(int im, string filename [, int foreground])
29622985
Output XBM image to browser or file */
2963-
#if HAVE_GD_BUNDLED
2986+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
29642987
PHP_FUNCTION(imagexbm)
29652988
{
29662989
_php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageXbmCtx);
@@ -5253,8 +5276,10 @@ PHP_FUNCTION(imageantialias)
52535276
RETURN_TRUE;
52545277
}
52555278
/* }}} */
5279+
#endif
52565280

52575281

5282+
#if defined(HAVE_GD_BUNDLED) || defined(HAVE_LIBGD21)
52585283
/* {{{ proto void imagecrop(resource im, array rect)
52595284
Crop an image using the given coordinates and size, x, y, width and height. */
52605285
PHP_FUNCTION(imagecrop)

ext/gd/libgd/gd_compat.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
const char * gdPngGetVersionString();
1111
const char * gdJpegGetVersionString();
1212
int gdJpegGetVersionInt();
13+
14+
#if !defined(HAVE_LIBGD21)
1315
int overflow2(int a, int b);
1416
int getmbi (int (*getin) (void *in), void *in);
1517
int skipheader (int (*getin) (void *in), void *in);
@@ -65,5 +67,6 @@ gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack, int ignore
6567

6668
int gdImageColorMatch (gdImagePtr im1, gdImagePtr im2);
6769

68-
#endif
70+
#endif /* !HAVE_LIBGD21 */
6971

72+
#endif /* GD_COMPAT_H */

0 commit comments

Comments
 (0)