Skip to content

Commit

Permalink
Merge pull request #346 from Synchro/separate-formats
Browse files Browse the repository at this point in the history
Break out the list of supported image formats
  • Loading branch information
ADmad authored Dec 7, 2022
2 parents 1419df6 + b07f2ce commit 17b0284
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ public function run(Image $image)
*/
public function getFormat(Image $image)
{
$allowed = [
if (array_key_exists($this->fm, static::allowedFormats())) {
return $this->fm;
}

if ($format = array_search($image->mime(), static::allowedFormats(), true)) {
return $format;
}

return 'jpg';
}

/**
* Get a list of supported image formats and MIME types.
*
* @return array<string,string>
*/
public static function allowedFormats()
{
return [
'avif' => 'image/avif',
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
Expand All @@ -54,16 +72,6 @@ public function getFormat(Image $image)
'webp' => 'image/webp',
'tiff' => 'image/tiff',
];

if (array_key_exists($this->fm, $allowed)) {
return $this->fm;
}

if ($format = array_search($image->mime(), $allowed, true)) {
return $format;
}

return 'jpg';
}

/**
Expand Down

0 comments on commit 17b0284

Please sign in to comment.