]> granicus.if.org Git - php/commitdiff
Don't enforce palette conversion when writing GD images
authorChristoph M. Becker <cmbecker69@gmx.de>
Sat, 24 Sep 2016 12:44:57 +0000 (14:44 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Sat, 24 Sep 2016 12:46:37 +0000 (14:46 +0200)
The GD image format is able to handle truecolor images as of libgd 2.0.12
(<https://github.com/libgd/libgd/blob/gd-2.2.3/src/gd_gd.c#L31-L33>).
Therefore we don't need the potentially lossy and time consuming palette
conversion.

This way, imagegd() can also be used to export raw truecolor image data.

UPGRADING
ext/gd/gd.c
ext/gd/tests/imagegd_truecolor.phpt [new file with mode: 0644]
ext/gd/tests/imagegd_truecolor.png [new file with mode: 0644]

index feef5d61c8d80f42e0e696888fd344a8b03406ba..2c66c4263a37708eb24cbcd221de3ee011ad8aa8 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -95,6 +95,8 @@ PHP 7.2 UPGRADE NOTES
 - GD:
     . Removed --enable-gd-native-ttf configuration option which was unused as
       of PHP 5.5.0 anyway.
+    . imagegd() stores truecolor images as real truecolor images. Formerly, they
+      have been converted to palette.
 
 - Mbstring
   . mb_check_encoding() accepts array parameter. Both key and value
index 29e51752862b141bd9dedff78d9c9a2471969a4f..739c069dd130484c3b2e10910056651a4384606a 100644 (file)
@@ -2565,9 +2565,6 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
                                (*func_p)(im, i, fp);
                                break;
                        case PHP_GDIMG_TYPE_GD:
-                               if (im->trueColor){
-                                       gdImageTrueColorToPalette(im,1,256);
-                               }
                                (*func_p)(im, fp);
                                break;
                        case PHP_GDIMG_TYPE_GD2:
@@ -2619,9 +2616,6 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
                                (*func_p)(im, q, tmp);
                                break;
                        case PHP_GDIMG_TYPE_GD:
-                               if (im->trueColor) {
-                                       gdImageTrueColorToPalette(im,1,256);
-                               }
                                (*func_p)(im, tmp);
                                break;
                        case PHP_GDIMG_TYPE_GD2:
diff --git a/ext/gd/tests/imagegd_truecolor.phpt b/ext/gd/tests/imagegd_truecolor.phpt
new file mode 100644 (file)
index 0000000..1eedae4
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--\r
+imagegd() writes truecolor images without palette conversion\r
+--SKIPIF--\r
+<?php\r
+if (!extension_loaded('gd')) die('skip gd extension not available');\r
+?>\r
+--FILE--\r
+<?php\r
+require_once __DIR__ . DIRECTORY_SEPARATOR . 'func.inc';\r
+\r
+$im = imagecreatetruecolor(10, 10);\r
+$white = imagecolorallocate($im, 255, 255, 255);\r
+imagefilledrectangle($im, 0,0, 9,9, $white);\r
+$blue = imagecolorallocate($im, 0, 0, 255);\r
+imagefilledrectangle($im, 3,3, 6,6, $blue);\r
+\r
+ob_start();\r
+imagegd($im);\r
+$buffer = ob_get_clean();\r
+\r
+$header = unpack('nsignature/nwidth/nheight/Ctruecolor', $buffer);\r
+printf("signature: %d\n", $header['signature']);\r
+printf("truecolor: %d\n", $header['truecolor']);\r
+printf("size: %d\n", strlen($buffer));\r
+\r
+test_image_equals_file(__DIR__ . DIRECTORY_SEPARATOR . 'imagegd_truecolor.png', $im);\r
+?>\r
+===DONE===\r
+--EXPECT--\r
+signature: 65534\r
+truecolor: 1\r
+size: 411\r
+The images are equal.\r
+===DONE===\r
diff --git a/ext/gd/tests/imagegd_truecolor.png b/ext/gd/tests/imagegd_truecolor.png
new file mode 100644 (file)
index 0000000..8c77c9f
Binary files /dev/null and b/ext/gd/tests/imagegd_truecolor.png differ