From 8cf121827ddd3cdd7945b47afbc9920b599ef2c5 Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Mon, 23 Nov 2026 13:26:29 +0100 Subject: [PATCH] Add image/apng media type PHP 8.6 bundles libmagic 5.48, which reports animated PNG files as image/apng. The GD decoders read the media type through finfo, could not map this value and threw, so every decoder in the chain failed and the user got "Unsupported image source type". Map image/apng to the PNG format, like the other alias media types. --- src/MediaType.php | 4 +++- .../Drivers/Gd/Decoders/BinaryImageDecoderTest.php | 11 +++++++++++ .../Drivers/Gd/Decoders/FilePathImageDecoderTest.php | 1 + tests/Unit/Drivers/Gd/DriverTest.php | 1 + tests/Unit/Drivers/Imagick/DriverTest.php | 1 + tests/Unit/FileExtensionTest.php | 2 +- tests/Unit/FormatTest.php | 2 +- tests/Unit/MediaTypeTest.php | 4 ++++ 8 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/MediaType.php b/src/MediaType.php index e4c56d184..9faa41b26 100644 --- a/src/MediaType.php +++ b/src/MediaType.php @@ -19,6 +19,7 @@ enum MediaType: string case IMAGE_GIF = 'image/gif'; case IMAGE_PNG = 'image/png'; case IMAGE_X_PNG = 'image/x-png'; + case IMAGE_APNG = 'image/apng'; case IMAGE_AVIF = 'image/avif'; case IMAGE_X_AVIF = 'image/x-avif'; case IMAGE_BMP = 'image/bmp'; @@ -115,7 +116,8 @@ public function format(): Format self::IMAGE_X_WEBP => Format::WEBP, self::IMAGE_GIF => Format::GIF, self::IMAGE_PNG, - self::IMAGE_X_PNG => Format::PNG, + self::IMAGE_X_PNG, + self::IMAGE_APNG => Format::PNG, self::IMAGE_AVIF, self::IMAGE_X_AVIF => Format::AVIF, self::IMAGE_BMP, diff --git a/tests/Unit/Drivers/Gd/Decoders/BinaryImageDecoderTest.php b/tests/Unit/Drivers/Gd/Decoders/BinaryImageDecoderTest.php index d107e0d0a..7859658e4 100644 --- a/tests/Unit/Drivers/Gd/Decoders/BinaryImageDecoderTest.php +++ b/tests/Unit/Drivers/Gd/Decoders/BinaryImageDecoderTest.php @@ -9,6 +9,7 @@ use Intervention\Image\Drivers\Gd\Decoders\BinaryImageDecoder; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Exceptions\InvalidArgumentException; +use Intervention\Image\Format; use Intervention\Image\Image; use Intervention\Image\Tests\BaseTestCase; use Intervention\Image\Tests\Resource; @@ -53,6 +54,16 @@ public function testDecodeAnimatedGif(): void $this->assertCount(4, $image); } + public function testDecodeAnimatedPng(): void + { + $image = $this->decoder->decode(Resource::create('animation.png')->data()); + $this->assertInstanceOf(Image::class, $image); + $this->assertEquals(16, $image->width()); + $this->assertEquals(16, $image->height()); + $this->assertCount(1, $image); + $this->assertEquals(Format::PNG, $image->origin()->format()); + } + public function testDecodeJpegWithExif(): void { $image = $this->decoder->decode(Resource::create('exif.jpg')->data()); diff --git a/tests/Unit/Drivers/Gd/Decoders/FilePathImageDecoderTest.php b/tests/Unit/Drivers/Gd/Decoders/FilePathImageDecoderTest.php index 662ad844e..645431284 100644 --- a/tests/Unit/Drivers/Gd/Decoders/FilePathImageDecoderTest.php +++ b/tests/Unit/Drivers/Gd/Decoders/FilePathImageDecoderTest.php @@ -53,6 +53,7 @@ public static function validFormatPathsProvider(): Generator yield [Resource::create('gradient.bmp')->path(), null]; yield [Resource::create('circle.png')->path(), null]; yield [Resource::create('circle.png')->stringablePath(), null]; + yield [Resource::create('animation.png')->path(), null]; yield ['no-path', FileNotFoundException::class]; yield [str_repeat('x', PHP_MAXPATHLEN + 1), InvalidArgumentException::class]; } diff --git a/tests/Unit/Drivers/Gd/DriverTest.php b/tests/Unit/Drivers/Gd/DriverTest.php index 74fcc91a0..1a7924469 100644 --- a/tests/Unit/Drivers/Gd/DriverTest.php +++ b/tests/Unit/Drivers/Gd/DriverTest.php @@ -136,6 +136,7 @@ public static function supportsDataProvider(): Generator yield [true, Format::PNG]; yield [true, MediaType::IMAGE_PNG]; yield [true, MediaType::IMAGE_X_PNG]; + yield [true, MediaType::IMAGE_APNG]; yield [true, FileExtension::PNG]; yield [true, 'png']; yield [true, 'image/png']; diff --git a/tests/Unit/Drivers/Imagick/DriverTest.php b/tests/Unit/Drivers/Imagick/DriverTest.php index 082037c68..79b4cc77b 100644 --- a/tests/Unit/Drivers/Imagick/DriverTest.php +++ b/tests/Unit/Drivers/Imagick/DriverTest.php @@ -151,6 +151,7 @@ public static function supportsDataProvider(): Generator yield [true, Format::PNG]; yield [true, MediaType::IMAGE_PNG]; yield [true, MediaType::IMAGE_X_PNG]; + yield [true, MediaType::IMAGE_APNG]; yield [true, FileExtension::PNG]; yield [true, 'png']; yield [true, 'image/png']; diff --git a/tests/Unit/FileExtensionTest.php b/tests/Unit/FileExtensionTest.php index a8ffe6c90..2ae5a31ce 100644 --- a/tests/Unit/FileExtensionTest.php +++ b/tests/Unit/FileExtensionTest.php @@ -166,7 +166,7 @@ public static function mediaTypesDataProvider(): Generator yield [FileExtension::JPEG, 4, MediaType::IMAGE_JPEG]; yield [FileExtension::WEBP, 2, MediaType::IMAGE_WEBP]; yield [FileExtension::GIF, 1, MediaType::IMAGE_GIF]; - yield [FileExtension::PNG, 2, MediaType::IMAGE_PNG]; + yield [FileExtension::PNG, 3, MediaType::IMAGE_PNG]; yield [FileExtension::AVIF, 2, MediaType::IMAGE_AVIF]; yield [FileExtension::BMP, 9, MediaType::IMAGE_BMP]; yield [FileExtension::TIFF, 1, MediaType::IMAGE_TIFF]; diff --git a/tests/Unit/FormatTest.php b/tests/Unit/FormatTest.php index 0cfe9161f..07b2cb3e6 100644 --- a/tests/Unit/FormatTest.php +++ b/tests/Unit/FormatTest.php @@ -93,7 +93,7 @@ public function testMediaTypesPng(): void $format = Format::PNG; $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); - $this->assertCount(2, $mediaTypes); + $this->assertCount(3, $mediaTypes); $this->assertEquals(MediaType::IMAGE_PNG, $format->mediaType()); } diff --git a/tests/Unit/MediaTypeTest.php b/tests/Unit/MediaTypeTest.php index 308ff7229..8d6b5adaa 100644 --- a/tests/Unit/MediaTypeTest.php +++ b/tests/Unit/MediaTypeTest.php @@ -93,6 +93,9 @@ public function testFormatPng(): void $mime = MediaType::IMAGE_X_PNG; $this->assertEquals(Format::PNG, $mime->format()); + + $mime = MediaType::IMAGE_APNG; + $this->assertEquals(Format::PNG, $mime->format()); } public function testFormatAvif(): void @@ -206,6 +209,7 @@ public static function fileExtensionsDataProvider(): Generator yield [MediaType::IMAGE_GIF, 1, FileExtension::GIF]; yield [MediaType::IMAGE_PNG, 1, FileExtension::PNG]; yield [MediaType::IMAGE_X_PNG, 1, FileExtension::PNG]; + yield [MediaType::IMAGE_APNG, 1, FileExtension::PNG]; yield [MediaType::IMAGE_AVIF, 1, FileExtension::AVIF]; yield [MediaType::IMAGE_X_AVIF, 1, FileExtension::AVIF]; yield [MediaType::IMAGE_BMP, 1, FileExtension::BMP];