]> BookStack Code Mirror - bookstack/blob - app/Uploads/FaviconHandler.php
Added default favicon creation upon access.
[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     protected string $path;
11
12     public function __construct(
13         protected ImageManager $imageTool
14     ) {
15         $this->path = public_path('favicon.ico');
16     }
17
18     /**
19      * Save the given UploadedFile instance as the application favicon.
20      */
21     public function saveForUploadedImage(UploadedFile $file): void
22     {
23         if (!is_writeable($this->path)) {
24             return;
25         }
26
27         $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);
32
33         file_put_contents($this->path, $icoData);
34     }
35
36     /**
37      * Restore the original favicon image.
38      */
39     public function restoreOriginal(): void
40     {
41         $original = public_path('icon.ico');
42         if (!is_writeable($this->path)) {
43             return;
44         }
45
46         copy($original, $this->path);
47     }
48
49     /**
50      * Restore the original favicon image if no favicon image is already in use.
51      */
52     public function restoreOriginalIfNotExists(): void
53     {
54         if (!file_exists($this->path)) {
55             $this->restoreOriginal();
56         }
57     }
58
59     /**
60      * Get the path to the favicon file.
61      */
62     public function getPath(): string
63     {
64         return $this->path;
65     }
66
67     /**
68      * Convert PNG image data to ICO file format.
69      * Built following the file format info from Wikipedia:
70      * https://en.wikipedia.org/wiki/ICO_(file_format)
71      */
72     protected function pngToIco(string $bmpData, int $width, int $height): string
73     {
74         // ICO header
75         $header = pack('v', 0x00); // Reserved. Must always be 0
76         $header .= pack('v', 0x01); // Specifies ico image
77         $header .= pack('v', 0x01); // Specifies number of images
78
79         // ICO Image Directory
80         $entry = hex2bin(dechex($width)); // Image width
81         $entry .= hex2bin(dechex($height)); // Image height
82         $entry .= "\0"; // Color palette, typically 0
83         $entry .= "\0"; // Reserved
84
85         // Color planes, Appears to remain 1 for bmp image data
86         $entry .= pack('v', 0x01);
87         // Bits per pixel, can range from 1 to 32. From testing conversion
88         // via intervention from png typically provides this as 24.
89         $entry .= pack('v', 0x00);
90         // Size of the image data in bytes
91         $entry .= pack('V', strlen($bmpData));
92         // Offset of the bmp data from file start
93         $entry .= pack('V', strlen($header) + strlen($entry) + 4);
94
95         // Join & return the combined parts of the ICO image data
96         return $header . $entry . $bmpData;
97     }
98 }