Skip to content

Commit

Permalink
(#13) Generator: Add native support for WEBP format.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored Feb 14, 2023
1 parent ba35fd7 commit 87e03bf
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/ImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,15 @@ public function isOk(string $path, ?string $format = null): bool
'image/gif' => 'gif',
'image/png' => 'png',
'image/jpeg' => 'jpg',
'image/webp' => 'webp',
];

$fInfo = finfo_open(FILEINFO_MIME_TYPE);
assert($fInfo !== false);
$contentType = (string) finfo_file($fInfo, $path);
if (isset($formatMap[$contentType]) === false) {
trigger_error(sprintf('Content type "%s" is not supported now.', $contentType));

return false;
}
$format = $formatMap[$contentType];
Expand All @@ -190,15 +193,16 @@ public function isOk(string $path, ?string $format = null): bool
'jpg' => 'imagecreatefromjpeg',
'jpeg' => 'imagecreatefromjpeg',
'gif' => 'imagecreatefromgif',
'webp' => 'imagecreatefromwebp',
];

$format = strtolower($format);
if (isset($formatToFunction[$format]) === false) {
throw new \InvalidArgumentException(
'Format "' . $format . '" is not supported. Did you mean "'
. implode('", "', array_keys($formatToFunction))
. '"?',
);
throw new \InvalidArgumentException(sprintf(
'Format "%s" is not supported. Did you mean "%s"?',
$format,
implode('", "', array_keys($formatToFunction)),
));
}
$function = $formatToFunction[$format];
if (Helper::functionIsAvailable($function) === false) {
Expand Down

0 comments on commit 87e03bf

Please sign in to comment.