]> BookStack Code Mirror - bookstack/blob - app/Uploads/FaviconHandler.php
78c9a899b1bf009d21cbe47475a2092ca94927c7
[bookstack] / app / Uploads / FaviconHandler.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use Illuminate\Http\UploadedFile;
6 use Intervention\Image\ImageManager;
7
8 class FaviconHandler
9 {
10     public function __construct(
11         protected ImageManager $imageTool
12     ) {
13     }
14
15     /**
16      * Save the given UploadedFile instance as the application favicon.
17      */
18     public function saveForUploadedImage(UploadedFile $file): void
19     {
20         $imageData = file_get_contents($file->getRealPath());
21         $image = $this->imageTool->make($imageData);
22         $image->resize(32, 32);
23         $bmpData = $image->encode('bmp');
24         $icoData = $this->bmpToIco($bmpData, 32, 32);
25
26         // TODO - Below are test paths
27         file_put_contents(public_path('uploads/test.ico'), $icoData);
28         file_put_contents(public_path('uploads/test.bmp'), $bmpData);
29
30         // TODO - Permission check for icon overwrite
31         // TODO - Write to correct location
32         // TODO - Handle deletion and restore of original icon on user icon clear
33     }
34
35     /**
36      * Convert BMP image data to ICO file format.
37      * Built following the file format info from Wikipedia:
38      * https://en.wikipedia.org/wiki/ICO_(file_format)
39      */
40     protected function bmpToIco(string $bmpData, int $width, int $height): string
41     {
42         // Trim off the header of the bitmap file
43         $rawBmpData = substr($bmpData, 14);
44
45         // ICO header
46         $header = pack('v', 0x00); // Reserved. Must always be 0
47         $header .= pack('v', 0x01); // Specifies ico image
48         $header .= pack('v', 0x01); // Specifies number of images
49
50         // ICO Image Directory
51         $entry = hex2bin(dechex($width)); // Image width
52         $entry .= hex2bin(dechex($height)); // Image height
53         $entry .= "\0"; // Color palette, typically 0
54         $entry .= "\0"; // Reserved
55
56         // Color planes, Appears to remain 1 for bmp image data
57         $entry .= pack('v', 0x01);
58         // Bits per pixel, can range from 1 to 32. From testing conversion
59         // via intervention from png typically provides this as 32.
60         $entry .= pack('v', 0x20);
61         // Size of the image data in bytes
62         $entry .= pack('V', strlen($rawBmpData));
63         // Offset of the bmp data from file start
64         $entry .= pack('V', strlen($header) + strlen($entry) + 4);
65
66         // Join & return the combined parts of the ICO image data
67         return $header . $entry . $rawBmpData;
68     }
69 }