Fixed bug #75124 (gdImageGrayScale() may produce colors)

We have to make sure to avoid alpha-blending issues by explicitly
switching to `gdEffectReplace` and to restore the old value afterwards.

This is a port of <https://github.com/libgd/libgd/commit/a7a7ece>.
This commit is contained in:
Christoph M. Becker 2017-08-27 13:53:39 +02:00
parent be9edd83c2
commit 499f5480f1
4 changed files with 41 additions and 0 deletions

3
NEWS
View File

@ -8,6 +8,9 @@ PHP NEWS
- CURL:
. Fixed bug #75093 (OpenSSL support not detected). (Remi)
- GD:
. Fixed bug #75124 (gdImageGrayScale() may produce colors). (cmb)
- Intl:
. Fixed bug #75090 (IntlGregorianCalendar doesn't have constants from parent
class). (tpunt)

View File

@ -53,12 +53,17 @@ int gdImageGrayScale(gdImagePtr src)
int new_pxl, pxl;
typedef int (*FuncPtr)(gdImagePtr, int, int);
FuncPtr f;
int alpha_blending;
f = GET_PIXEL_FUNCTION(src);
if (src==NULL) {
return 0;
}
alpha_blending = src->alphaBlendingFlag;
gdImageAlphaBlending(src, gdEffectReplace);
for (y=0; y<src->sy; ++y) {
for (x=0; x<src->sx; ++x) {
pxl = f (src, x, y);
@ -75,6 +80,8 @@ int gdImageGrayScale(gdImagePtr src)
gdImageSetPixel (src, x, y, new_pxl);
}
}
gdImageAlphaBlending(src, alpha_blending);
return 1;
}

View File

@ -0,0 +1,31 @@
--TEST--
Bug #75124 (gdImageGrayScale() may produce colors)
--SKIPIF--
<?php
if (!extension_loaded('gd')) die('gd extension not available');
if (!GD_BUNDLED && version_compare(GD_VERSION, '2.2.5', '<')) {
die('skip only for bundled libgd or external libgd >= 2.2.5');
}
?>
--FILE--
<?php
$im = imagecreatefrompng(__DIR__ . '/bug75124.png');
var_dump(imageistruecolor($im));
imagefilter($im, IMG_FILTER_GRAYSCALE);
for ($i = 0, $width = imagesx($im); $i < $width; $i ++) {
for ($j = 0, $height = imagesy($im); $j < $height; $j++) {
$color = imagecolorat($im, $i, $j);
$red = ($color >> 16) & 0xff;
$green = ($color >> 8) & 0xff;
$blue = $color & 0xff;
if ($red != $green || $green != $blue) {
echo "non grayscale pixel detected\n";
break 2;
}
}
}
?>
===DONE===
--EXPECT--
bool(true)
===DONE===

BIN
ext/gd/tests/bug75124.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB