]> BookStack Code Mirror - bookstack/blob - app/Uploads/FaviconHandler.php
Got favicons better supported, can't get transparency right
[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         $targetPath = public_path('favicon.ico');
21         if (!is_writeable($targetPath)) {
22             return;
23         }
24
25         $imageData = file_get_contents($file->getRealPath());
26         $image = $this->imageTool->make($imageData);
27         $image->resize(32, 32);
28         $bmpData = $image->encode('bmp');
29         $icoData = $this->bmpToIco($bmpData, 32, 32);
30
31 //        file_put_contents(public_path('icon.bmp'), $bmpData);
32 //        file_put_contents(public_path('icon-test.png'), $image->encode('png'));
33         file_put_contents($targetPath, $icoData);
34     }
35
36     /**
37      * Restore the original favicon image.
38      */
39     public function restoreOriginal(): void
40     {
41         $targetPath = public_path('favicon.ico');
42         $original = public_path('icon.ico');
43         if (!is_writeable($targetPath)) {
44             return;
45         }
46
47         copy($original, $targetPath);
48     }
49
50     /**
51      * Convert BMP image data to ICO file format.
52      * Built following the file format info from Wikipedia:
53      * https://en.wikipedia.org/wiki/ICO_(file_format)
54      */
55     protected function bmpToIco(string $bmpData, int $width, int $height): string
56     {
57         // Trim off the header of the bitmap file
58         $rawBmpData = substr($bmpData, 14);
59         // Double the height in the "BITMAPINFOHEADER" since, when in an ICO file, half
60         // of the image data is expected to be a mask.
61         $rawBmpData[8] = hex2bin(dechex($height * 2));
62
63         // ICO header
64         $header = pack('v', 0x00); // Reserved. Must always be 0
65         $header .= pack('v', 0x01); // Specifies ico image
66         $header .= pack('v', 0x01); // Specifies number of images
67
68         // ICO Image Directory
69         $entry = hex2bin(dechex($width)); // Image width
70         $entry .= hex2bin(dechex($height)); // Image height
71         $entry .= "\0"; // Color palette, typically 0
72         $entry .= "\0"; // Reserved
73
74         // AND mask
75 //        $pxCount = $width * $height;
76 //        $pxMask = hex2bin('00000000');
77 //        $mask = str_repeat($pxMask, $pxCount);
78         $mask = '';
79
80         // Color planes, Appears to remain 1 for bmp image data
81         $entry .= pack('v', 0x01);
82         // Bits per pixel, can range from 1 to 32. From testing conversion
83         // via intervention from png typically provides this as 24.
84         $entry .= pack('v', 0x18);
85         // Size of the image data in bytes
86         $entry .= pack('V', strlen($rawBmpData) + strlen($mask));
87         // Offset of the bmp data from file start
88         $entry .= pack('V', strlen($header) + strlen($entry) + 4);
89
90         // Join & return the combined parts of the ICO image data
91         return $header . $entry . $rawBmpData . $mask;
92     }
93 }