|
5 | 5 | exit(1); |
6 | 6 | } |
7 | 7 |
|
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) { |
9 | 11 | if (!\Imagick::queryFormats($format)) { |
10 | 12 | echo sprintf('FAIL: Imagick does not support "%s".', $format).PHP_EOL; |
11 | 13 | exit(1); |
|
27 | 29 | exit(1); |
28 | 30 | } |
29 | 31 |
|
| 32 | + |
30 | 33 | 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 | + |
34 | 57 | } 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; |
36 | 59 | exit(1); |
37 | 60 | } catch (\Throwable $e) { |
38 | 61 | echo sprintf('FAIL: Imagick failed with "%s" exception: %s', get_class($e), $e->getMessage()).PHP_EOL; |
39 | 62 | exit(1); |
40 | 63 | } |
41 | 64 |
|
| 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 | + |
42 | 76 | exit(0); |
0 commit comments