Skip to content

Commit 2fb873f

Browse files
committed
Favicon: Moved resizing to specific resizer class
1 parent bff1f50 commit 2fb873f

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

app/Uploads/FaviconHandler.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
namespace BookStack\Uploads;
44

55
use Illuminate\Http\UploadedFile;
6-
use Intervention\Image\ImageManager;
76

87
class FaviconHandler
98
{
109
protected string $path;
1110

1211
public function __construct(
13-
protected ImageManager $imageTool
12+
protected ImageResizer $imageResizer,
1413
) {
1514
$this->path = public_path('favicon.ico');
1615
}
@@ -25,10 +24,8 @@ public function saveForUploadedImage(UploadedFile $file): void
2524
}
2625

2726
$imageData = file_get_contents($file->getRealPath());
28-
$image = $this->imageTool->make($imageData);
29-
$image->resize(32, 32);
30-
$bmpData = $image->encode('png');
31-
$icoData = $this->pngToIco($bmpData, 32, 32);
27+
$pngData = $this->imageResizer->resizeImageData($imageData, 32, 32, false, 'png');
28+
$icoData = $this->pngToIco($pngData, 32, 32);
3229

3330
file_put_contents($this->path, $icoData);
3431
}
@@ -81,7 +78,7 @@ public function getOriginalPath(): string
8178
* Built following the file format info from Wikipedia:
8279
* https://en.wikipedia.org/wiki/ICO_(file_format)
8380
*/
84-
protected function pngToIco(string $bmpData, int $width, int $height): string
81+
protected function pngToIco(string $pngData, int $width, int $height): string
8582
{
8683
// ICO header
8784
$header = pack('v', 0x00); // Reserved. Must always be 0
@@ -100,11 +97,11 @@ protected function pngToIco(string $bmpData, int $width, int $height): string
10097
// via intervention from png typically provides this as 24.
10198
$entry .= pack('v', 0x00);
10299
// Size of the image data in bytes
103-
$entry .= pack('V', strlen($bmpData));
100+
$entry .= pack('V', strlen($pngData));
104101
// Offset of the bmp data from file start
105102
$entry .= pack('V', strlen($header) + strlen($entry) + 4);
106103

107104
// Join & return the combined parts of the ICO image data
108-
return $header . $entry . $bmpData;
105+
return $header . $entry . $pngData;
109106
}
110107
}

app/Uploads/ImageResizer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,17 @@ public function resizeToThumbnailUrl(
105105

106106
/**
107107
* Resize the image of given data to the specified size, and return the new image data.
108+
* Format will remain the same as the input format, unless specified.
108109
*
109110
* @throws ImageUploadException
110111
*/
111-
public function resizeImageData(string $imageData, ?int $width, ?int $height, bool $keepRatio): string
112-
{
112+
public function resizeImageData(
113+
string $imageData,
114+
?int $width,
115+
?int $height,
116+
bool $keepRatio,
117+
?string $format = null,
118+
): string {
113119
try {
114120
$thumb = $this->intervention->make($imageData);
115121
} catch (Exception $e) {
@@ -127,7 +133,7 @@ public function resizeImageData(string $imageData, ?int $width, ?int $height, bo
127133
$thumb->fit($width, $height);
128134
}
129135

130-
$thumbData = (string) $thumb->encode();
136+
$thumbData = (string) $thumb->encode($format);
131137

132138
// Use original image data if we're keeping the ratio
133139
// and the resizing does not save any space.

0 commit comments

Comments
 (0)