diff --git a/src/FileExtension.php b/src/FileExtension.php index 3cfea7825..2a5854cee 100644 --- a/src/FileExtension.php +++ b/src/FileExtension.php @@ -55,4 +55,24 @@ public function format(): Format self::HEIF => Format::HEIC, }; } + + /** + * Return media types for the current format + * + * @return array + */ + public function mediaTypes(): array + { + return $this->format()->mediaTypes(); + } + + /** + * Return the first found media type for the current format + * + * @return MediaType + */ + public function mediaType(): MediaType + { + return $this->format()->mediaType(); + } } diff --git a/tests/Unit/FileExtensionTest.php b/tests/Unit/FileExtensionTest.php index db99ae8e3..4092aabb3 100644 --- a/tests/Unit/FileExtensionTest.php +++ b/tests/Unit/FileExtensionTest.php @@ -6,7 +6,9 @@ use Intervention\Image\FileExtension; use Intervention\Image\Format; +use Intervention\Image\MediaType; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; final class FileExtensionTest extends BaseTestCase { @@ -81,4 +83,27 @@ public function testFormatHeic(): void $ext = FileExtension::HEIF; $this->assertEquals(Format::HEIC, $ext->format()); } + + #[DataProvider('mediaTypesDataProvider')] + public function testMediatypes(FileExtension $extension, int $mediaTypeCount, MediaType $mediaType): void + { + $this->assertCount($mediaTypeCount, $extension->mediaTypes()); + $this->assertEquals($mediaType, $extension->mediaType()); + } + + public static function mediaTypesDataProvider(): array + { + return [ + [FileExtension::JPEG, 4, MediaType::IMAGE_JPEG], + [FileExtension::WEBP, 2, MediaType::IMAGE_WEBP], + [FileExtension::GIF, 1, MediaType::IMAGE_GIF], + [FileExtension::PNG, 2, MediaType::IMAGE_PNG], + [FileExtension::AVIF, 2, MediaType::IMAGE_AVIF], + [FileExtension::BMP, 8, MediaType::IMAGE_BMP], + [FileExtension::TIFF, 1, MediaType::IMAGE_TIFF], + [FileExtension::TIF, 1, MediaType::IMAGE_TIFF], + [FileExtension::JP2, 3, MediaType::IMAGE_JP2], + [FileExtension::HEIC, 3, MediaType::IMAGE_HEIC], + ]; + } }