]> BookStack Code Mirror - bookstack/blob - app/Uploads/FaviconHandler.php
Lexical: Media form improvements
[bookstack] / app / Uploads / FaviconHandler.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use Illuminate\Http\UploadedFile;
6
7 class FaviconHandler
8 {
9     protected string $path;
10
11     public function __construct(
12         protected ImageResizer $imageResizer,
13     ) {
14         $this->path = public_path('favicon.ico');
15     }
16
17     /**
18      * Save the given UploadedFile instance as the application favicon.
19      */
20     public function saveForUploadedImage(UploadedFile $file): void
21     {
22         if (!is_writeable($this->path)) {
23             return;
24         }
25
26         $imageData = file_get_contents($file->getRealPath());
27         $pngData = $this->imageResizer->resizeImageData($imageData, 32, 32, false, 'png');
28         $icoData = $this->pngToIco($pngData, 32, 32);
29
30         file_put_contents($this->path, $icoData);
31     }
32
33     /**
34      * Restore the original favicon image.
35      * Returned boolean indicates if the copy occurred.
36      */
37     public function restoreOriginal(): bool
38     {
39         $permissionItem = file_exists($this->path) ? $this->path : dirname($this->path);
40         if (!is_writeable($permissionItem)) {
41             return false;
42         }
43
44         return copy($this->getOriginalPath(), $this->path);
45     }
46
47     /**
48      * Restore the original favicon image if no favicon image is already in use.
49      * Returns a boolean to indicate if the file exists.
50      */
51     public function restoreOriginalIfNotExists(): bool
52     {
53         if (file_exists($this->path)) {
54             return true;
55         }
56
57         return $this->restoreOriginal();
58     }
59
60     /**
61      * Get the path to the favicon file.
62      */
63     public function getPath(): string
64     {
65         return $this->path;
66     }
67
68     /**
69      * Get the path of the original favicon copy.
70      */
71     public function getOriginalPath(): string
72     {
73         return public_path('icon.ico');
74     }
75
76     /**
77      * Convert PNG image data to ICO file format.
78      * Built following the file format info from Wikipedia:
79      * https://en.wikipedia.org/wiki/ICO_(file_format)
80      */
81     protected function pngToIco(string $pngData, int $width, int $height): string
82     {
83         // ICO header
84         $header = pack('v', 0x00); // Reserved. Must always be 0
85         $header .= pack('v', 0x01); // Specifies ico image
86         $header .= pack('v', 0x01); // Specifies number of images
87
88         // ICO Image Directory
89         $entry = hex2bin(dechex($width)); // Image width
90         $entry .= hex2bin(dechex($height)); // Image height
91         $entry .= "\0"; // Color palette, typically 0
92         $entry .= "\0"; // Reserved
93
94         // Color planes, Appears to remain 1 for bmp image data
95         $entry .= pack('v', 0x01);
96         // Bits per pixel, can range from 1 to 32. From testing conversion
97         // via intervention from png typically provides this as 24.
98         $entry .= pack('v', 0x00);
99         // Size of the image data in bytes
100         $entry .= pack('V', strlen($pngData));
101         // Offset of the bmp data from file start
102         $entry .= pack('V', strlen($header) + strlen($entry) + 4);
103
104         // Join & return the combined parts of the ICO image data
105         return $header . $entry . $pngData;
106     }
107 }