From 85e5635a8e89e529a2c8dcda234232b1c668da07 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 27 Dec 2023 14:12:32 +0100 Subject: [PATCH] Fix detection of image formats in system gd library - Use gdFontCacheShutdown() to detect freetype Currently we look for gdImageStringFT() to determine whether or not gd has freetype support... but that function always exists. This leads PHP to believe that gd has freetype support when it does not, and can lead to build failures. The gdFontCacheShutdown() function, on the other hand, is only present when gd was built with freetype support. Let's use that instead. - Fix GD image format detection We currently check for, say, AVIF support by attempting to link a program that calls libgd's gdImageCreateFromAvif() function. But perversely, that function always exists in libgd; moreover when AVIF support is missing it emits a warning and returns normally. Thus our straightforward link test becomes not so straightforward. This commit adds a new macro PHP_GD_CHECK_FORMAT that compiles, links, and runs a test program instead. The test program overrides that "emit a warning" handler so that the program actually fails if the format we're looking for is not supported. This fixes detection of AVIF and the other formats we check for in an external libgd. - ext/gd/tests/bug77391.phpt: skip if gd lacks BMP support I don't actually know how to remove BMP support from libgd, but PHP has a ./configure test for it, so we should probably treat it as optional. Closes GH-12019 --- NEWS | 3 ++ ext/gd/config.m4 | 61 +++++++++++++++++++++++++++++++++----- ext/gd/tests/bug77391.phpt | 1 + 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 087d98c2c68..5a30310bdbf 100644 --- a/NEWS +++ b/NEWS @@ -26,6 +26,9 @@ FPM: FTP: . Removed the deprecated inet_ntoa call support. (David Carlier) +GD: + . Fixed detection of image formats in system gd library. (Michael Orlitzky) + Intl: . Added IntlDateFormatter::PATTERN constant. (David Carlier) . Fixed Numberformatter::__construct when the locale is invalid, now diff --git a/ext/gd/config.m4 b/ext/gd/config.m4 index 826d7c7c826..6ca36fae1dc 100644 --- a/ext/gd/config.m4 +++ b/ext/gd/config.m4 @@ -138,15 +138,60 @@ AC_DEFUN([PHP_GD_JISX0208],[ fi ]) +dnl Build and run a program to determine if GD has support for the given +dnl format. The first argument is the proper-noun-capitalized name of the +dnl format -- basically the word Foo in gdImageCreateFromFoo -- such as +dnl Png. If support for format Foo exists, the second argument (the name +dnl of a constant) will be defined to 1. The reason for this charade is +dnl that gd defines "junk" versions of each gdImageCreateFromFoo function +dnl even when it does not support the Foo format. Those junk functions +dnl display a warning but eventually return normally, making a simple link +dnl or run test insufficient. +AC_DEFUN([PHP_GD_CHECK_FORMAT],[ + old_LIBS="${LIBS}" + LIBS="${LIBS} ${GD_SHARED_LIBADD}" + AC_MSG_CHECKING([for working gdImageCreateFrom$1 in libgd]) + AC_LANG_PUSH([C]) + AC_RUN_IFELSE([AC_LANG_SOURCE([ +#include +#include +#include + +/* A custom gdErrorMethod */ +void exit1(int priority, const char *format, va_list args) { + _exit(1); +} + +/* Override the default gd_error_method with one that + actually causes the program to return an error. */ +int main(int argc, char** argv) { + m4_if([$1],[Xpm], + [char* f = "test.xpm"], + [FILE* f = NULL]); + gdSetErrorMethod(exit1); + gdImagePtr p = gdImageCreateFrom$1(f); + return 0; +}])],[ + AC_MSG_RESULT([yes]) + AC_DEFINE($2, 1, [Does gdImageCreateFrom$1 work?]) + ],[ + AC_MSG_RESULT([no]) + ],[ + AC_MSG_RESULT([no]) + ]) + AC_LANG_POP([C]) + LIBS="${old_LIBS}" +]) + AC_DEFUN([PHP_GD_CHECK_VERSION],[ - PHP_CHECK_LIBRARY(gd, gdImageCreateFromPng, [AC_DEFINE(HAVE_GD_PNG, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageCreateFromAvif, [AC_DEFINE(HAVE_GD_AVIF, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageCreateFromWebp, [AC_DEFINE(HAVE_GD_WEBP, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageCreateFromJpeg, [AC_DEFINE(HAVE_GD_JPG, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageCreateFromXpm, [AC_DEFINE(HAVE_GD_XPM, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageCreateFromBmp, [AC_DEFINE(HAVE_GD_BMP, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageCreateFromTga, [AC_DEFINE(HAVE_GD_TGA, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) - PHP_CHECK_LIBRARY(gd, gdImageStringFT, [AC_DEFINE(HAVE_GD_FREETYPE, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) + PHP_GD_CHECK_FORMAT([Png], [HAVE_GD_PNG]) + PHP_GD_CHECK_FORMAT([Avif], [HAVE_GD_AVIF]) + PHP_GD_CHECK_FORMAT([Webp], [HAVE_GD_WEBP]) + PHP_GD_CHECK_FORMAT([Jpeg], [HAVE_GD_JPG]) + PHP_GD_CHECK_FORMAT([Xpm], [HAVE_GD_XPM]) + PHP_GD_CHECK_FORMAT([Bmp], [HAVE_GD_BMP]) + PHP_GD_CHECK_FORMAT([Tga], [HAVE_GD_TGA]) + PHP_CHECK_LIBRARY(gd, gdFontCacheShutdown, [AC_DEFINE(HAVE_GD_FREETYPE, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) PHP_CHECK_LIBRARY(gd, gdVersionString, [AC_DEFINE(HAVE_GD_LIBVERSION, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) PHP_CHECK_LIBRARY(gd, gdImageGetInterpolationMethod, [AC_DEFINE(HAVE_GD_GET_INTERPOLATION, 1, [ ])], [], [ $GD_SHARED_LIBADD ]) ]) diff --git a/ext/gd/tests/bug77391.phpt b/ext/gd/tests/bug77391.phpt index 6f07ce57da1..6f41d164ddc 100644 --- a/ext/gd/tests/bug77391.phpt +++ b/ext/gd/tests/bug77391.phpt @@ -5,6 +5,7 @@ gd --SKIPIF-- --FILE--