From 81d958e410e542b71d14937751720ca826ff16d0 Mon Sep 17 00:00:00 2001 From: Laurent Constantin Date: Fri, 31 May 2024 16:51:34 +0200 Subject: [PATCH] feat(kobo): Cover Tranformer support wepb --- src/Kobo/ImageProcessor/CoverTransformer.php | 27 +++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Kobo/ImageProcessor/CoverTransformer.php b/src/Kobo/ImageProcessor/CoverTransformer.php index df0c7020..d1c77ce9 100644 --- a/src/Kobo/ImageProcessor/CoverTransformer.php +++ b/src/Kobo/ImageProcessor/CoverTransformer.php @@ -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() { } @@ -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); @@ -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) { @@ -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), };