From 51d68c113cd0c5133d95198dd61f63173abcc328 Mon Sep 17 00:00:00 2001 From: arshidkv12 Date: Wed, 21 Oct 2026 00:01:58 +0530 Subject: [PATCH 1/3] ext/gd: Fix imagewebp() return value for palette images ext/gd: Fix imagewebp() return value for palette images ext/gd: Fix imagewebp() return value for palette images ext/gd: Fix imagewebp() return value for palette images --- NEWS | 1 + ext/gd/gd.c | 6 ++++++ ext/gd/tests/gh24457.phpt | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 ext/gd/tests/gh24457.phpt diff --git a/NEWS b/NEWS index 8771d8eae544..b62b94099e6e 100644 --- a/NEWS +++ b/NEWS @@ -202,6 +202,7 @@ PHP NEWS (DanielEScherzer, cmb) . Fixed undefined behavior with GIFs with problematic LZW compression data. (DanielEScherzer, vapier) + . Fixed the return value of imagewebp() for palette images. (arshidkv12) - Intl: . Fixed IntlDateFormatter::parse(), localtime() and parseToCalendar() diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 92001c4f9967..114ccec55aa0 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -2003,6 +2003,12 @@ PHP_FUNCTION(imagewebp) RETURN_FALSE; } + if (!gdImageTrueColor(im)) { + php_error_docref(NULL, E_WARNING, "Palette image not supported by webp"); + ctx->gd_free(ctx); + RETURN_FALSE; + } + if (quality < -1) { zend_argument_value_error(3, "must be greater than or equal to -1"); ctx->gd_free(ctx); diff --git a/ext/gd/tests/gh24457.phpt b/ext/gd/tests/gh24457.phpt new file mode 100644 index 000000000000..9497b42489a1 --- /dev/null +++ b/ext/gd/tests/gh24457.phpt @@ -0,0 +1,22 @@ +--TEST-- +imagewebp() with a palette image +--EXTENSIONS-- +gd +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECTF-- +Warning: imagewebp(): Palette image not supported by webp in %s on line %d +bool(false) From 636115cfaa8192a9fd443d71788a95af3d9661e2 Mon Sep 17 00:00:00 2001 From: arshidkv12 Date: Wed, 21 Oct 2026 00:59:56 +0530 Subject: [PATCH 2/3] ext/gd: Fix imagewebp() return value for palette images --- ext/gd/gd.c | 11 ++++------- ext/gd/libgd/gd.h | 2 +- ext/gd/libgd/gd_webp.c | 15 +++++++++------ ext/gd/tests/gh24457.phpt | 7 +++++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 114ccec55aa0..96a9f3f35e89 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -2003,19 +2003,16 @@ PHP_FUNCTION(imagewebp) RETURN_FALSE; } - if (!gdImageTrueColor(im)) { - php_error_docref(NULL, E_WARNING, "Palette image not supported by webp"); - ctx->gd_free(ctx); - RETURN_FALSE; - } - if (quality < -1) { zend_argument_value_error(3, "must be greater than or equal to -1"); ctx->gd_free(ctx); RETURN_THROWS(); } - gdImageWebpCtx(im, ctx, (int) quality); + if(!gdImageWebpCtx(im, ctx, (int) quality)){ + ctx->gd_free(ctx); + RETURN_FALSE; + } ctx->gd_free(ctx); diff --git a/ext/gd/libgd/gd.h b/ext/gd/libgd/gd.h index 672ddf94837c..c0046e3be779 100644 --- a/ext/gd/libgd/gd.h +++ b/ext/gd/libgd/gd.h @@ -632,7 +632,7 @@ void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality); */ #define gdWebpLossless 101 -void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality); +bool gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality); /* Best to free this memory with gdFree(), not free() */ void *gdImageJpegPtr(gdImagePtr im, int *size, int quality); diff --git a/ext/gd/libgd/gd_webp.c b/ext/gd/libgd/gd_webp.c index 6277e09710f9..f2f65e77ee75 100644 --- a/ext/gd/libgd/gd_webp.c +++ b/ext/gd/libgd/gd_webp.c @@ -101,7 +101,7 @@ gdImagePtr gdImageCreateFromWebpCtx (gdIOCtx * infile) return im; } -void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality) +bool gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality) { uint8_t *argb; int x, y; @@ -110,12 +110,12 @@ void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality) size_t out_size; if (im == NULL) { - return; + return false; } if (!gdImageTrueColor(im)) { gd_error("Palette image not supported by webp"); - return; + return false; } if (quality == -1) { @@ -123,16 +123,16 @@ void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality) } if (overflow2(gdImageSX(im), 4)) { - return; + return false; } if (overflow2(gdImageSX(im) * 4, gdImageSY(im))) { - return; + return false; } argb = (uint8_t *)gdMalloc(gdImageSX(im) * 4 * gdImageSY(im)); if (!argb) { - return; + return false; } p = argb; for (y = 0; y < gdImageSY(im); y++) { @@ -165,9 +165,12 @@ void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality) } gdPutBuf(out, out_size, outfile); free(out); + gdFree(argb); + return true; freeargb: gdFree(argb); + return false; } void gdImageWebpEx (gdImagePtr im, FILE * outFile, int quality) diff --git a/ext/gd/tests/gh24457.phpt b/ext/gd/tests/gh24457.phpt index 9497b42489a1..8575d350481e 100644 --- a/ext/gd/tests/gh24457.phpt +++ b/ext/gd/tests/gh24457.phpt @@ -12,11 +12,18 @@ if (!function_exists('imagewebp')) { --CLEAN-- --EXPECTF-- Warning: imagewebp(): Palette image not supported by webp in %s on line %d bool(false) + +Warning: imagewebp(): gd-webp encoding failed in %s on line %d +bool(false) \ No newline at end of file From 3e85e263f007fe87431a9c239088da00f52da4d3 Mon Sep 17 00:00:00 2001 From: arshidkv12 Date: Wed, 21 Oct 2026 01:59:34 +0530 Subject: [PATCH 3/3] ext/gd: Fix imagewebp() return value for palette images --- ext/gd/gd.c | 2 +- ext/gd/libgd/gd.h | 4 +++- ext/gd/libgd/gd_webp.c | 6 +++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 96a9f3f35e89..c789600c37e9 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -2009,7 +2009,7 @@ PHP_FUNCTION(imagewebp) RETURN_THROWS(); } - if(!gdImageWebpCtx(im, ctx, (int) quality)){ + if (!gdImageWebpCtxEx(im, ctx, (int) quality)) { ctx->gd_free(ctx); RETURN_FALSE; } diff --git a/ext/gd/libgd/gd.h b/ext/gd/libgd/gd.h index c0046e3be779..83dfe796c7ea 100644 --- a/ext/gd/libgd/gd.h +++ b/ext/gd/libgd/gd.h @@ -48,6 +48,7 @@ extern "C" { /* va_list needed in gdErrorMethod */ #include +#include /* The maximum number of palette entries in palette-based images. In the wonderful new world of gd 2.0, you can of course have @@ -632,7 +633,8 @@ void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality); */ #define gdWebpLossless 101 -bool gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality); +void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality); +bool gdImageWebpCtxEx (gdImagePtr im, gdIOCtx * outfile, int quality); /* Best to free this memory with gdFree(), not free() */ void *gdImageJpegPtr(gdImagePtr im, int *size, int quality); diff --git a/ext/gd/libgd/gd_webp.c b/ext/gd/libgd/gd_webp.c index f2f65e77ee75..423be70d4997 100644 --- a/ext/gd/libgd/gd_webp.c +++ b/ext/gd/libgd/gd_webp.c @@ -101,7 +101,11 @@ gdImagePtr gdImageCreateFromWebpCtx (gdIOCtx * infile) return im; } -bool gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality) +void gdImageWebpCtx(gdImagePtr im, gdIOCtx *outfile, int quality){ + gdImageWebpCtxEx(im, outfile, quality); +} + +bool gdImageWebpCtxEx (gdImagePtr im, gdIOCtx * outfile, int quality) { uint8_t *argb; int x, y;