From c696bc8cfac64a66bb69dffe96d312a621fcdab7 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sun, 25 Sep 2016 10:30:48 +0200 Subject: [PATCH] Fix test_image_equals_file() wrt. palette images The recently introduced test_image_equals_file() doesn't properly work for palette images, because in this case only the palette indexes are compared, what can lead to false positives and negatives as shown in the added test. To fix that we convert palette images to truecolor, what is supposed to be faster than calling imagecolorsforindex() for each pixel. We furthermore rely on PHP's refcounting to free unused images; after all, this is not C. --- ext/gd/tests/func.inc | 23 +++++++++- .../tests/test_image_equals_file_palette.phpt | 42 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 ext/gd/tests/test_image_equals_file_palette.phpt diff --git a/ext/gd/tests/func.inc b/ext/gd/tests/func.inc index 01ba83102ec..708fccd3d11 100644 --- a/ext/gd/tests/func.inc +++ b/ext/gd/tests/func.inc @@ -82,7 +82,9 @@ function test_image_equals_file($filename, $actual) save_actual_image($actual); return; } + $actual = test_to_truecolor($actual); $expected = imagecreatefrompng($filename); + $expected = test_to_truecolor($expected); $exp_x = imagesx($expected); $exp_y = imagesy($expected); $act_x = imagesx($actual); @@ -90,7 +92,6 @@ function test_image_equals_file($filename, $actual) if ($exp_x != $act_x || $exp_y != $act_y) { echo "The image size differs: expected {$exp_x}x{$exp_y}, got {$act_x}x{$act_y}.\n"; save_actual_image($actual); - imagedestroy($expected); return; } $pixels_changed = 0; @@ -109,7 +110,25 @@ function test_image_equals_file($filename, $actual) echo "The images differ in {$pixels_changed} pixels.\n"; save_actual_image($actual); } - imagedestroy($expected); +} + +/** + * Returns the truecolor version of an image. + * + * @param resource $image + * @return resource + */ +function test_to_truecolor($image) +{ + if (imageistruecolor($image)) { + return $image; + } else { + $width = imagesx($image); + $height = imagesy($image); + $result = imagecreatetruecolor($width, $height); + imagecopy($result, $image, 0,0, 0,0, $width, $height); + return $result; + } } /** diff --git a/ext/gd/tests/test_image_equals_file_palette.phpt b/ext/gd/tests/test_image_equals_file_palette.phpt new file mode 100644 index 00000000000..130fa432024 --- /dev/null +++ b/ext/gd/tests/test_image_equals_file_palette.phpt @@ -0,0 +1,42 @@ +--TEST-- +test_image_equals_file(): comparing palette images +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECT-- +The images differ in 25 pixels. +The images are equal. +===DONE=== +--CLEAN-- +