mirror of
https://github.com/php/php-src.git
synced 2025-08-18 15:08:55 +02:00
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.
This commit is contained in:
parent
c596b02a5b
commit
c696bc8cfa
2 changed files with 63 additions and 2 deletions
|
@ -82,7 +82,9 @@ function test_image_equals_file($filename, $actual)
|
||||||
save_actual_image($actual);
|
save_actual_image($actual);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$actual = test_to_truecolor($actual);
|
||||||
$expected = imagecreatefrompng($filename);
|
$expected = imagecreatefrompng($filename);
|
||||||
|
$expected = test_to_truecolor($expected);
|
||||||
$exp_x = imagesx($expected);
|
$exp_x = imagesx($expected);
|
||||||
$exp_y = imagesy($expected);
|
$exp_y = imagesy($expected);
|
||||||
$act_x = imagesx($actual);
|
$act_x = imagesx($actual);
|
||||||
|
@ -90,7 +92,6 @@ function test_image_equals_file($filename, $actual)
|
||||||
if ($exp_x != $act_x || $exp_y != $act_y) {
|
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";
|
echo "The image size differs: expected {$exp_x}x{$exp_y}, got {$act_x}x{$act_y}.\n";
|
||||||
save_actual_image($actual);
|
save_actual_image($actual);
|
||||||
imagedestroy($expected);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$pixels_changed = 0;
|
$pixels_changed = 0;
|
||||||
|
@ -109,7 +110,25 @@ function test_image_equals_file($filename, $actual)
|
||||||
echo "The images differ in {$pixels_changed} pixels.\n";
|
echo "The images differ in {$pixels_changed} pixels.\n";
|
||||||
save_actual_image($actual);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
42
ext/gd/tests/test_image_equals_file_palette.phpt
Normal file
42
ext/gd/tests/test_image_equals_file_palette.phpt
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
--TEST--
|
||||||
|
test_image_equals_file(): comparing palette images
|
||||||
|
--SKIPIF--
|
||||||
|
<?php
|
||||||
|
if (!extension_loaded('gd')) die('skip gd extension not available');
|
||||||
|
?>
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'func.inc';
|
||||||
|
|
||||||
|
$im = imagecreate(10, 10);
|
||||||
|
imagecolorallocate($im, 255, 255, 255);
|
||||||
|
$red = imagecolorallocate($im, 255, 0, 0);
|
||||||
|
imagefilledrectangle($im, 3,3, 7,7, $red);
|
||||||
|
|
||||||
|
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'test_image_equals_file_palette.png';
|
||||||
|
imagepng($im, $filename);
|
||||||
|
|
||||||
|
$im = imagecreate(10, 10);
|
||||||
|
imagecolorallocate($im, 255, 255, 255);
|
||||||
|
$blue = imagecolorallocate($im, 0, 0, 255);
|
||||||
|
imagefilledrectangle($im, 3,3, 7,7, $blue);
|
||||||
|
|
||||||
|
test_image_equals_file($filename, $im);
|
||||||
|
|
||||||
|
$im = imagecreate(10, 10);
|
||||||
|
imagecolorallocate($im, 255, 255, 255);
|
||||||
|
imagecolorallocate($im, 0, 0, 0);
|
||||||
|
$red = imagecolorallocate($im, 255, 0, 0);
|
||||||
|
imagefilledrectangle($im, 3,3, 7,7, $red);
|
||||||
|
|
||||||
|
test_image_equals_file($filename, $im);
|
||||||
|
?>
|
||||||
|
===DONE===
|
||||||
|
--EXPECT--
|
||||||
|
The images differ in 25 pixels.
|
||||||
|
The images are equal.
|
||||||
|
===DONE===
|
||||||
|
--CLEAN--
|
||||||
|
<?php
|
||||||
|
unlink(__DIR__ . DIRECTORY_SEPARATOR . 'test_image_equals_file_palette.png');
|
||||||
|
?>
|
Loading…
Add table
Add a link
Reference in a new issue