diff --git a/src/main/php/img/io/AvifStreamReader.class.php b/src/main/php/img/io/AvifStreamReader.class.php new file mode 100755 index 00000000..83070462 --- /dev/null +++ b/src/main/php/img/io/AvifStreamReader.class.php @@ -0,0 +1,29 @@ +quality= $quality; + } + + /** + * Output an image + * + * @param resource handle + * @return bool + */ + public function output($handle) { + if (imageistruecolor($handle)) return imageavif($handle, null, $this->quality); + + // Prevent "imageavif(): avif error - avif doesn't support palette images" + $width= imagesx($handle); + $height= imagesy($handle); + $copy= imagecreatetruecolor($width, $height); + imagecopy($copy, $handle, 0, 0, 0, 0, $width, $height); + + try { + return imageavif($copy, null, $this->quality); + } finally { + PHP_VERSION_ID <= 80000 && imagedestroy($copy); + } + } +} \ No newline at end of file diff --git a/src/test/php/img/unittest/AvifImageReaderTest.class.php b/src/test/php/img/unittest/AvifImageReaderTest.class.php new file mode 100755 index 00000000..42915b0f --- /dev/null +++ b/src/test/php/img/unittest/AvifImageReaderTest.class.php @@ -0,0 +1,16 @@ +image->saveTo(new AvifStreamWriter(new class() implements OutputStream { + public function write($arg) { throw new OperationFailed('Could not write: Intentional exception'); } + public function flush() { } + public function close() { } + })); + } + + #[Test] + public function write() { + $s= new MemoryOutputStream(); + $this->image->saveTo(new AvifStreamWriter($s)); + Assert::notEquals('', $s->bytes()); + } +} \ No newline at end of file diff --git a/src/test/php/img/unittest/ImageTypeSupport.class.php b/src/test/php/img/unittest/ImageTypeSupport.class.php index 942e5998..00f432c3 100755 --- a/src/test/php/img/unittest/ImageTypeSupport.class.php +++ b/src/test/php/img/unittest/ImageTypeSupport.class.php @@ -32,7 +32,7 @@ public function assertions(Context $context) { $supported= imagetypes(); foreach ($this->types as $type) { yield new Assertion( - $supported & constant('IMG_'.strtoupper($type)), + $supported & defined('IMG_'.strtoupper($type)), new Verify('Image type "'.$type.'" is supported') ); }