diff --git a/src/MediaType.php b/src/MediaType.php index c5f1b6e0..71dc5732 100644 --- a/src/MediaType.php +++ b/src/MediaType.php @@ -69,4 +69,24 @@ public function format(): Format self::IMAGE_X_HEIC => Format::HEIC, }; } + + /** + * Return the possible file extension for the current media type + * + * @return array + */ + public function fileExtensions(): array + { + return $this->format()->fileExtensions(); + } + + /** + * Return the first file extension for the current media type + * + * @return FileExtension + */ + public function fileExtension(): FileExtension + { + return $this->format()->fileExtension(); + } } diff --git a/tests/Unit/MediaTypeTest.php b/tests/Unit/MediaTypeTest.php index 93f1888a..aeca4bc2 100644 --- a/tests/Unit/MediaTypeTest.php +++ b/tests/Unit/MediaTypeTest.php @@ -4,9 +4,11 @@ namespace Intervention\Image\Tests\Unit; +use Intervention\Image\FileExtension; use Intervention\Image\Format; use Intervention\Image\MediaType; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; final class MediaTypeTest extends BaseTestCase { @@ -105,4 +107,46 @@ public function testFormatHeic(): void $mime = MediaType::IMAGE_HEIF; $this->assertEquals(Format::HEIC, $mime->format()); } + + #[DataProvider('fileExtensionsDataProvider')] + public function testFileExtensions( + MediaType $mediaType, + int $fileExtensionCount, + FileExtension $fileExtension + ): void { + $this->assertCount($fileExtensionCount, $mediaType->fileExtensions()); + $this->assertEquals($fileExtension, $mediaType->fileExtension()); + } + + public static function fileExtensionsDataProvider(): array + { + return [ + [MediaType::IMAGE_JPEG, 2, FileExtension::JPG], + [MediaType::IMAGE_JPG, 2, FileExtension::JPG], + [MediaType::IMAGE_PJPEG, 2, FileExtension::JPG], + [MediaType::IMAGE_X_JPEG, 2, FileExtension::JPG], + [MediaType::IMAGE_WEBP, 1, FileExtension::WEBP], + [MediaType::IMAGE_X_WEBP, 1, FileExtension::WEBP], + [MediaType::IMAGE_GIF, 1, FileExtension::GIF], + [MediaType::IMAGE_PNG, 1, FileExtension::PNG], + [MediaType::IMAGE_X_PNG, 1, FileExtension::PNG], + [MediaType::IMAGE_AVIF, 1, FileExtension::AVIF], + [MediaType::IMAGE_X_AVIF, 1, FileExtension::AVIF], + [MediaType::IMAGE_BMP, 1, FileExtension::BMP], + [MediaType::IMAGE_MS_BMP, 1, FileExtension::BMP], + [MediaType::IMAGE_X_BITMAP, 1, FileExtension::BMP], + [MediaType::IMAGE_X_BMP, 1, FileExtension::BMP], + [MediaType::IMAGE_X_MS_BMP, 1, FileExtension::BMP], + [MediaType::IMAGE_X_WINDOWS_BMP, 1, FileExtension::BMP], + [MediaType::IMAGE_X_WIN_BITMAP, 1, FileExtension::BMP], + [MediaType::IMAGE_X_XBITMAP, 1, FileExtension::BMP], + [MediaType::IMAGE_TIFF, 2, FileExtension::TIF], + [MediaType::IMAGE_JP2, 8, FileExtension::JP2], + [MediaType::IMAGE_JPX, 8, FileExtension::JP2], + [MediaType::IMAGE_JPM, 8, FileExtension::JP2], + [MediaType::IMAGE_HEIC, 2, FileExtension::HEIC], + [MediaType::IMAGE_X_HEIC, 2, FileExtension::HEIC], + [MediaType::IMAGE_HEIF, 2, FileExtension::HEIC], + ]; + } }