Skip to content

Commit 7f51352

Browse files
committed
imagick: increase test coverage
1 parent 2992310 commit 7f51352

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

layers/imagick/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testoutput

layers/imagick/test.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
exit(1);
66
}
77

8-
foreach (['PNG', 'JPG', 'GIF', 'WEBP', 'HEIC'] as $format) {
8+
$expected_formats = ['PNG', 'JPG', 'GIF', 'WEBP', 'HEIC', 'AVIF'];
9+
10+
foreach ( $expected_formats as $format) {
911
if (!\Imagick::queryFormats($format)) {
1012
echo sprintf('FAIL: Imagick does not support "%s".', $format).PHP_EOL;
1113
exit(1);
@@ -27,16 +29,48 @@
2729
exit(1);
2830
}
2931

32+
3033
try {
31-
$image = new \Imagick(__DIR__.'/test.pdf');
32-
$image->writeImage('/tmp/imagick-test.jpg');
33-
assert(file_exists('/tmp/imagick-test.jpg'));
34+
$tmpdir = '/tmp/imagicktest';
35+
mkdir($tmpdir);
36+
37+
foreach ($expected_formats as $format) {
38+
//for all files in the testfiles directory
39+
foreach (glob(__DIR__.'/testfiles/*') as $file) {
40+
$image = new \Imagick($file);
41+
$output_path = $tmpdir . '/' .pathinfo($file, PATHINFO_FILENAME) . '.' . strtolower($format);
42+
$image->writeImage($output_path);
43+
validateImageFile($output_path);
44+
}
45+
}
46+
47+
// compare the size of the AVIF image with the original JPG, buggy builds may just copy the jpg
48+
assert(filesize( $tmpdir . '/jpg_test.avif') < filesize(__DIR__.'/testfiles/jpg_test.jpg') * 0.9);
49+
50+
// copy the output files to the testoutput directory, if it exists. Useful for local testing
51+
if (file_exists('/var/task/testoutput')){
52+
foreach (glob($tmpdir.'/*') as $file) {
53+
copy($file, '/var/task/testoutput/'.basename($file));
54+
}
55+
}
56+
3457
} catch(\ImagickException $e) {
35-
echo sprintf('FAIL: Imagick cannot convert PDF "%s".', $e->getMessage()).PHP_EOL;
58+
echo sprintf('FAIL: Imagick failed to write image "%s".', $e->getMessage()).PHP_EOL;
3659
exit(1);
3760
} catch (\Throwable $e) {
3861
echo sprintf('FAIL: Imagick failed with "%s" exception: %s', get_class($e), $e->getMessage()).PHP_EOL;
3962
exit(1);
4063
}
4164

65+
// some basic image validation
66+
function validateImageFile($file) {
67+
assert(file_exists($file), 'File does not exist: ' . $file);
68+
assert(filesize($file) > 128, 'File size ( '. filesize($file) .' byte ) is < byte for ' . $file );
69+
// only for supported formats
70+
if (in_array(pathinfo($file, PATHINFO_EXTENSION), ['png', 'jpg', 'webp', 'avif']) && version_compare(phpversion(), '8.3', '>=')) {
71+
assert(getimagesize($file) !== false, 'getimagesize failed for ' . $file);
72+
}
73+
74+
}
75+
4276
exit(0);
1.31 KB
Binary file not shown.
5.28 KB
Loading
4.97 KB
Loading

0 commit comments

Comments
 (0)