fileExtension(); } if ($identifier instanceof MediaType) { return $identifier->fileExtension(); } try { $extension = self::from(strtolower($identifier)); } catch (Error) { try { $extension = MediaType::from(strtolower($identifier))->fileExtension(); } catch (Error) { throw new NotSupportedException('Unable to create file extension from "' . $identifier . '".'); } } return $extension; } /** * Try to create media type from given identifier and return null on failure * * @param string|Format|MediaType|FileExtension $identifier * @return FileExtension|null */ public static function tryCreate(string|self|Format|MediaType $identifier): ?self { try { return self::create($identifier); } catch (NotSupportedException) { return null; } } /** * Return the matching format for the current file extension * * @return Format */ public function format(): Format { return match ($this) { self::JPEG, self::JPG => Format::JPEG, self::WEBP => Format::WEBP, self::GIF => Format::GIF, self::PNG => Format::PNG, self::AVIF => Format::AVIF, self::BMP => Format::BMP, self::TIF, self::TIFF => Format::TIFF, self::JP2, self::JP2K, self::J2K, self::JPF, self::JPM, self::JPG2, self::J2C, self::JPC, self::JPX => Format::JP2, self::HEIC, 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(); } }