From 2a515a2d9692dee2c7f5b07b0f5b042fa11246d0 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Fri, 16 Oct 2026 21:18:26 +0200 Subject: [PATCH 1/2] Add AVIF support --- .../php/img/io/AvifStreamReader.class.php | 29 ++++++++++++ .../php/img/io/AvifStreamWriter.class.php | 45 +++++++++++++++++++ .../unittest/AvfifImageReaderTest.class.php | 16 +++++++ .../unittest/AvifImageWriterTest.class.php | 27 +++++++++++ 4 files changed, 117 insertions(+) create mode 100755 src/main/php/img/io/AvifStreamReader.class.php create mode 100755 src/main/php/img/io/AvifStreamWriter.class.php create mode 100755 src/test/php/img/unittest/AvfifImageReaderTest.class.php create mode 100755 src/test/php/img/unittest/AvifImageWriterTest.class.php 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/AvfifImageReaderTest.class.php b/src/test/php/img/unittest/AvfifImageReaderTest.class.php new file mode 100755 index 00000000..ce036a1a --- /dev/null +++ b/src/test/php/img/unittest/AvfifImageReaderTest.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 From c55d62f86ac11335ba344285f5ebb08e38ca909f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Fri, 16 Oct 2026 21:24:42 +0200 Subject: [PATCH 2/2] Fix base64 bytes --- .../img/unittest/AvfifImageReaderTest.class.php | 16 ---------------- .../img/unittest/AvifImageReaderTest.class.php | 16 ++++++++++++++++ .../php/img/unittest/ImageTypeSupport.class.php | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) delete mode 100755 src/test/php/img/unittest/AvfifImageReaderTest.class.php create mode 100755 src/test/php/img/unittest/AvifImageReaderTest.class.php diff --git a/src/test/php/img/unittest/AvfifImageReaderTest.class.php b/src/test/php/img/unittest/AvfifImageReaderTest.class.php deleted file mode 100755 index ce036a1a..00000000 --- a/src/test/php/img/unittest/AvfifImageReaderTest.class.php +++ /dev/null @@ -1,16 +0,0 @@ -types as $type) { yield new Assertion( - $supported & constant('IMG_'.strtoupper($type)), + $supported & defined('IMG_'.strtoupper($type)), new Verify('Image type "'.$type.'" is supported') ); }