php-src/ext/gd/tests/func.inc
Christoph M. Becker dc09f30933 A picture is worth a thousand message digests
The bundled GD test suites makes heavy use of md5() to verify the result
of drawing operations. This leads to fragile tests (even a slight change
in a PNG header would cause failure, and of course there is the
possibility of collisions), and even worse, eventual test failures are
rather unrevealing.

Therefore we replace all md5() verification with a simplistic
test_image_equals_file(), which is basically a simplified port of libgd's
gdTestImageCompareToFile(), adapted to the needs of PHPTs.

In the long run better tests helpers should be introduced (see also
<http://news.php.net/php.internals/94081>), but for now this solution
is preferable over the former.

(cherry picked from commit 24f9e96792)
2016-09-11 12:56:53 +02:00

130 lines
2.9 KiB
PHP

<?php
function get_gd_version()
{
return GD_VERSION;
}
function get_php_info()
{
ob_start();
phpinfo();
$info = ob_get_contents();
ob_end_clean();
return $info;
}
function get_freetype_version()
{
$version = 0;
if (preg_match(',FreeType Version => (\d+\.\d+\.\d+),s', get_php_info(), $match)) {
$version = $match[1];
}
return $version;
}
function get_libjpeg_version()
{
$version = 0;
if (preg_match(',libJPEG Version => ([a-z0-9]+),s', get_php_info(), $match)) {
$version = $match[1];
}
return $version;
}
function get_libpng_version()
{
$version = 0;
if (preg_match(',libPNG Version => (\d+\.\d+\.\d+),s', get_php_info(), $match)) {
$version = $match[1];
}
return $version;
}
function get_libxpm_version()
{
$version = 0;
if (preg_match(',libXpm Version => (\d+),s', get_php_info(), $match)) {
$version = $match[1];
}
return $version;
}
/**
* Tests that an in-memory image equals a PNG file.
*
* It checks for equal image sizes, and whether any pixels are different.
* The textual result is printed, so the EXPECT section should contain the line
* "The images are equal."
*
* If the PNG file does not exists, or the images are not equal, a diagnostic
* message is printed, and the actual file is stored right beside the temporary
* .php test file with the extension .out.png, to be able to manually inspect
* the result.
*
* @param string $filename
* @param resource $actual
* @return void
*/
function test_image_equals_file($filename, $actual)
{
if (!file_exists($filename)) {
echo "The expected image does not exist.\n";
save_actual_image($actual);
return;
}
$expected = imagecreatefrompng($filename);
$exp_x = imagesx($expected);
$exp_y = imagesy($expected);
$act_x = imagesx($actual);
$act_y = imagesy($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;
for ($y = 0; $y < $exp_y; $y++) {
for ($x = 0; $x < $exp_x; $x ++) {
$exp_c = imagecolorat($expected, $x, $y);
$act_c = imagecolorat($actual, $x, $y);
if ($exp_c != $act_c) {
$pixels_changed++;
}
}
}
if (!$pixels_changed) {
echo "The images are equal.\n";
} else {
echo "The images differ in {$pixels_changed} pixels.\n";
save_actual_image($actual);
}
imagedestroy($expected);
}
/**
* Saves an actual image to disk.
*
* The image is saved right beside the temporary .php test file with the
* extension .out.png.
*
* @param resource $image
* @return void
*/
function save_actual_image($image)
{
$pathinfo = pathinfo($_SERVER['SCRIPT_FILENAME']);
$filename = "{$pathinfo['dirname']}/{$pathinfo['filename']}.out.png";
imagepng($image, $filename);
}