Skip to content

Commit d65adac

Browse files
committed
Fix #72913: imagecopy() loses single-color transparency on palette images
The proper code to handle true-color to palette copies is already contained in gdImageCopy(), so we can simply remove the buggy duplicated code.
1 parent 9eb5bbd commit d65adac

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616
- GD:
1717
. Fixed bug #66005 (imagecopy does not support 1bit transparency on truecolor
1818
images). (cmb)
19+
. Fixed bug #72913 (imagecopy() loses single-color transparency on palette
20+
images). (cmb)
1921

2022
- JSON:
2123
. Fixed bug #72787 (json_decode reads out of bounds). (Jakub Zelenka)

ext/gd/libgd/gd.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,26 +2266,6 @@ void gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX,
22662266
return;
22672267
}
22682268

2269-
/* Destination is palette based */
2270-
if (src->trueColor) { /* But source is truecolor (Ouch!) */
2271-
toy = dstY;
2272-
for (y = srcY; (y < (srcY + h)); y++) {
2273-
tox = dstX;
2274-
for (x = srcX; x < (srcX + w); x++) {
2275-
int nc;
2276-
c = gdImageGetPixel (src, x, y);
2277-
2278-
/* Get best match possible. */
2279-
nc = gdImageColorResolveAlpha(dst, gdTrueColorGetRed(c), gdTrueColorGetGreen(c), gdTrueColorGetBlue(c), gdTrueColorGetAlpha(c));
2280-
2281-
gdImageSetPixel(dst, tox, toy, nc);
2282-
tox++;
2283-
}
2284-
toy++;
2285-
}
2286-
return;
2287-
}
2288-
22892269
/* Palette based to palette based */
22902270
for (i = 0; i < gdMaxColors; i++) {
22912271
colorMap[i] = (-1);

ext/gd/tests/bug72913.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #72913 (imagecopy() loses single-color transparency on palette images)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$base64 = 'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAABnRSTlMAAAAAAABu'
10+
. 'pgeRAAAAVklEQVRYw+3UQQqAMBAEwf3/p9eTBxEPiWAmWMU8oGFJqgAAuOpzWTX3'
11+
. 'xQUti+uRJTZ9V5aY1bOTFZLV7yZr9zt6ibv/qPXfrMpsGipbIy7oqQ8AYJED1plD'
12+
. 'y5PCu2sAAAAASUVORK5CYII=';
13+
$src = imagecreatefromstring(base64_decode($base64));
14+
15+
$dst = imagecreate(50, 50);
16+
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
17+
imagealphablending($dst, false);
18+
imagesavealpha($dst, true);
19+
20+
imagecopy($dst, $src, 0,0, 0,0, 50,50);
21+
22+
ob_start();
23+
imagegd($dst);
24+
echo md5(ob_get_clean()), PHP_EOL;
25+
?>
26+
==DONE==
27+
--EXPECT--
28+
f03c27f20710e21debd7090c660f1a1e
29+
==DONE==

0 commit comments

Comments
 (0)