From 2fb873f7efeb0279aea0241c09ec51ddcdd2f119 Mon Sep 17 00:00:00 2001
From: Dan Brown <email@danb.me>
Date: Sun, 19 Nov 2023 15:57:19 +0000
Subject: [PATCH] Favicon: Moved resizing to specific resizer class

---
 app/Uploads/FaviconHandler.php | 15 ++++++---------
 app/Uploads/ImageResizer.php   | 12 +++++++++---
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/app/Uploads/FaviconHandler.php b/app/Uploads/FaviconHandler.php
index c637356e0..d5044943c 100644
--- a/app/Uploads/FaviconHandler.php
+++ b/app/Uploads/FaviconHandler.php
@@ -3,14 +3,13 @@
 namespace BookStack\Uploads;
 
 use Illuminate\Http\UploadedFile;
-use Intervention\Image\ImageManager;
 
 class FaviconHandler
 {
     protected string $path;
 
     public function __construct(
-        protected ImageManager $imageTool
+        protected ImageResizer $imageResizer,
     ) {
         $this->path = public_path('favicon.ico');
     }
@@ -25,10 +24,8 @@ class FaviconHandler
         }
 
         $imageData = file_get_contents($file->getRealPath());
-        $image = $this->imageTool->make($imageData);
-        $image->resize(32, 32);
-        $bmpData = $image->encode('png');
-        $icoData = $this->pngToIco($bmpData, 32, 32);
+        $pngData = $this->imageResizer->resizeImageData($imageData, 32, 32, false, 'png');
+        $icoData = $this->pngToIco($pngData, 32, 32);
 
         file_put_contents($this->path, $icoData);
     }
@@ -81,7 +78,7 @@ class FaviconHandler
      * Built following the file format info from Wikipedia:
      * https://en.wikipedia.org/wiki/ICO_(file_format)
      */
-    protected function pngToIco(string $bmpData, int $width, int $height): string
+    protected function pngToIco(string $pngData, int $width, int $height): string
     {
         // ICO header
         $header = pack('v', 0x00); // Reserved. Must always be 0
@@ -100,11 +97,11 @@ class FaviconHandler
         // via intervention from png typically provides this as 24.
         $entry .= pack('v', 0x00);
         // Size of the image data in bytes
-        $entry .= pack('V', strlen($bmpData));
+        $entry .= pack('V', strlen($pngData));
         // Offset of the bmp data from file start
         $entry .= pack('V', strlen($header) + strlen($entry) + 4);
 
         // Join & return the combined parts of the ICO image data
-        return $header . $entry . $bmpData;
+        return $header . $entry . $pngData;
     }
 }
diff --git a/app/Uploads/ImageResizer.php b/app/Uploads/ImageResizer.php
index 0d090a94b..e229bb5a0 100644
--- a/app/Uploads/ImageResizer.php
+++ b/app/Uploads/ImageResizer.php
@@ -105,11 +105,17 @@ class ImageResizer
 
     /**
      * Resize the image of given data to the specified size, and return the new image data.
+     * Format will remain the same as the input format, unless specified.
      *
      * @throws ImageUploadException
      */
-    public function resizeImageData(string $imageData, ?int $width, ?int $height, bool $keepRatio): string
-    {
+    public function resizeImageData(
+        string $imageData,
+        ?int $width,
+        ?int $height,
+        bool $keepRatio,
+        ?string $format = null,
+    ): string {
         try {
             $thumb = $this->intervention->make($imageData);
         } catch (Exception $e) {
@@ -127,7 +133,7 @@ class ImageResizer
             $thumb->fit($width, $height);
         }
 
-        $thumbData = (string) $thumb->encode();
+        $thumbData = (string) $thumb->encode($format);
 
         // Use original image data if we're keeping the ratio
         // and the resizing does not save any space.