Skip to content

Commit

Permalink
Cover Tranformer support wepb
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed May 31, 2024
1 parent 2f6fb93 commit de7b109
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Kobo/ImageProcessor/CoverTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

class CoverTransformer
{
public const JPG = '.jpg';
public const PNG = '.png';
public const GIF = '.gif';
public const JPEG = '.jpeg';
public const WEBP = '.wepb';

public function __construct()
{
}
Expand All @@ -22,11 +28,12 @@ public function streamFile(string $coverPath, int $maxWidth, int $maxHeight, boo
return;
}

// We only support jpeg & png & gif
if (false === str_ends_with($coverPath, '.jpg')
&& false === str_ends_with($coverPath, '.png')
&& false === str_ends_with($coverPath, '.gif')
&& false === str_ends_with($coverPath, '.jpeg')
// We only support jpeg & png & gif & webp
if (false === str_ends_with($coverPath, self::JPG)
&& false === str_ends_with($coverPath, self::PNG)
&& false === str_ends_with($coverPath, self::GIF)
&& false === str_ends_with($coverPath, self::JPEG)
&& false === str_ends_with($coverPath, self::WEBP)
) {
$this->fallback($coverPath);

Expand Down Expand Up @@ -75,8 +82,9 @@ public function streamFile(string $coverPath, int $maxWidth, int $maxHeight, boo
// Load the original image based on the extension
$extension = pathinfo($coverPath, PATHINFO_EXTENSION);
$originalImage = match ($extension) {
'png' => imagecreatefrompng($coverPath),
'gif' => imagecreatefromgif($coverPath),
self::PNG => imagecreatefrompng($coverPath),
self::GIF => imagecreatefromgif($coverPath),
self::WEBP => imagecreatefromwebp($coverPath),
default => imagecreatefromjpeg($coverPath),
};
if (false === $originalImage) {
Expand All @@ -98,8 +106,9 @@ public function streamFile(string $coverPath, int $maxWidth, int $maxHeight, boo

// Output the image based on the extension
match ($extension) {
'png' => imagepng($image, null, 9),
'gif' => imagegif($image),
self::PNG => imagepng($image, null, 9),
self::GIF => imagegif($image),
self::WEBP => imagewebp($image),
default => imagejpeg($image, null, 100),
};

Expand Down

0 comments on commit de7b109

Please sign in to comment.