33namespace BookStack \Uploads ;
44
55use Illuminate \Http \UploadedFile ;
6- use Intervention \Image \ImageManager ;
76
87class 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}
0 commit comments