docker-php-extension-installer/scripts/tests/gd

98 lines
2.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env php
2019-12-27 10:28:50 +00:00
<?php
require_once __DIR__ . '/_bootstrap.php';
2021-09-29 07:31:34 +00:00
echo 'Creating sample image... ';
2019-12-27 10:28:50 +00:00
$imageWidth = 8;
$imageHeight = 16;
$image = imagecreatetruecolor($imageWidth, $imageHeight);
2021-09-29 07:31:34 +00:00
echo "done.\n";
2019-12-27 13:56:03 +00:00
$formats = [
2019-12-27 10:28:50 +00:00
'gd2',
'gif',
'jpeg',
'png',
'wbmp',
'webp',
'wbmp',
'xbm',
'xpm',
'gd',
2019-12-27 13:56:03 +00:00
];
2019-12-27 10:28:50 +00:00
if (PHP_VERSION_ID >= 70200) {
2021-09-29 07:31:34 +00:00
$formats[] = 'bmp';
if (PHP_VERSION_ID >= 80100) {
$formats[] = 'avif';
}
2019-12-27 10:28:50 +00:00
}
$tempFile = null;
$image2 = null;
2019-12-27 13:56:03 +00:00
2019-12-27 10:28:50 +00:00
try {
foreach ($formats as $format) {
2021-09-29 07:31:34 +00:00
echo "Checking format {$format}... ";
2019-12-27 13:56:03 +00:00
$loadFuntion = "imagecreatefrom{$format}";
2019-12-27 10:28:50 +00:00
if (!function_exists($loadFuntion)) {
2019-12-27 13:56:03 +00:00
throw new Exception("{$loadFuntion}() function is missing");
2019-12-27 10:28:50 +00:00
}
2019-12-27 13:56:03 +00:00
$saveFuntion = "image{$format}";
2019-12-27 10:28:50 +00:00
if (!function_exists($saveFuntion)) {
2021-09-29 07:31:34 +00:00
if ($format === 'xpm') {
echo "skipping tests since PHP can't save images to that format.\n";
continue;
}
2019-12-27 13:56:03 +00:00
throw new Exception("{$saveFuntion}() function is missing");
2019-12-27 10:28:50 +00:00
}
$tempFile = tempnam(sys_get_temp_dir(), 'dpei');
ob_start();
2019-12-27 10:28:50 +00:00
if ($saveFuntion($image, $tempFile) === false) {
2019-12-27 13:56:03 +00:00
throw new Exception("{$saveFuntion}() failed");
2019-12-27 10:28:50 +00:00
}
$contents = ob_get_contents();
ob_end_clean();
if (!is_file($tempFile)) {
throw new Exception("{$saveFuntion}() didn't create a file");
}
if (filesize($tempFile) < 1) {
if ($format !== 'xbm' || PHP_VERSION_ID >= 50600 || $contents === '') {
throw new Exception("{$saveFuntion}() created an empty file");
}
file_put_contents($tempFile, $contents);
2019-12-27 10:28:50 +00:00
}
$image2 = $loadFuntion($tempFile);
unlink($tempFile);
$tempFile = null;
2020-09-18 18:22:23 +00:00
if (!(is_resource($image2) || is_object($image2)) || imagesx($image2) !== $imageWidth || imagesy($image2) !== $imageHeight) {
2019-12-27 13:56:03 +00:00
throw new Exception("{$loadFuntion}() failed");
2019-12-27 10:28:50 +00:00
}
imagedestroy($image2);
2021-09-29 07:31:34 +00:00
echo "done.\n";
2019-12-27 10:28:50 +00:00
}
if (!function_exists('imagefttext')) {
throw new Exception('imagefttext() function is missing');
}
if (!function_exists('imageantialias')) {
throw new Exception('imageantialias() function is missing');
}
$rc = 0;
} catch (Exception $x) {
$rc = 1;
fwrite(STDERR, $x->getMessage());
} catch (Throwable $x) {
$rc = 1;
fwrite(STDERR, $x->getMessage());
2019-12-27 10:28:50 +00:00
} finally {
imagedestroy($image);
if (is_resource($image2)) {
imagedestroy($image2);
}
2019-12-27 13:56:03 +00:00
if ($tempFile !== null) {
2019-12-27 10:28:50 +00:00
unlink($tempFile);
}
}
exit($rc);