diff --git a/src/Actions/GenerateOgImage.php b/src/Actions/GenerateOgImage.php index be18406..abb72a2 100644 --- a/src/Actions/GenerateOgImage.php +++ b/src/Actions/GenerateOgImage.php @@ -19,7 +19,7 @@ public static function handle(string $mdPath): string ->setScreenshotType('webp', 85) ->screenshot(); - $filename = Str::slug(str_replace('/', '-', $mdPath)).'.png'; + $filename = Str::slug(str_replace('/', '-', $mdPath)).'.webp'; $filepath = 'images/ogimages/'.$filename; Storage::disk(GetPrezetDisk::handle())->put($filepath, $screenshot); diff --git a/src/Actions/GetImage.php b/src/Actions/GetImage.php index 6123cc5..10b5113 100644 --- a/src/Actions/GetImage.php +++ b/src/Actions/GetImage.php @@ -10,7 +10,8 @@ class GetImage { public static function handle(string $path): string { - self::validateFileExtension($path); + $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); + self::validateFileExtension($extension); $size = self::extractSize($path); $path = self::removeSize($path); @@ -21,13 +22,12 @@ public static function handle(string $path): string $image = self::resizeImage($image, $size); } - return self::outputImage($image, pathinfo($path, PATHINFO_EXTENSION)); + return self::outputImage($image, $extension); } - protected static function validateFileExtension(string $path): void + protected static function validateFileExtension(string $extension): void { - $allowedExtensions = ['png', 'jpg', 'webp']; - $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); + $allowedExtensions = ['png', 'jpg', 'jpeg', 'webp']; if (! in_array($extension, $allowedExtensions)) { abort(404, 'Invalid file extension'); @@ -99,6 +99,7 @@ private static function outputImage(GdImage $image, string $extension): string imagepng($image); break; case 'jpg': + case 'jpeg': imagejpeg($image); break; case 'webp': diff --git a/src/Http/Controllers/ImageController.php b/src/Http/Controllers/ImageController.php index d7880e6..498fdab 100644 --- a/src/Http/Controllers/ImageController.php +++ b/src/Http/Controllers/ImageController.php @@ -13,7 +13,11 @@ public function __invoke(Request $request, string $path): Response $file = GetImage::handle($path); return response($file, 200, [ - 'Content-Type' => 'image/webp', + 'Content-Type' => match (pathinfo($path, PATHINFO_EXTENSION)) { + 'jpg', 'jpeg' => 'image/jpeg', + 'png' => 'image/png', + default => 'image/webp' + }, 'Cache-Control' => 'public, max-age=31536000', ]); }