3 namespace BookStack\Uploads;
5 use Illuminate\Http\UploadedFile;
6 use Intervention\Image\ImageManager;
10 protected string $path;
12 public function __construct(
13 protected ImageManager $imageTool
15 $this->path = public_path('favicon.ico');
19 * Save the given UploadedFile instance as the application favicon.
21 public function saveForUploadedImage(UploadedFile $file): void
23 if (!is_writeable($this->path)) {
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);
33 file_put_contents($this->path, $icoData);
37 * Restore the original favicon image.
39 public function restoreOriginal(): void
41 $original = public_path('icon.ico');
42 if (!is_writeable($this->path)) {
46 copy($original, $this->path);
50 * Restore the original favicon image if no favicon image is already in use.
52 public function restoreOriginalIfNotExists(): void
54 if (!file_exists($this->path)) {
55 $this->restoreOriginal();
60 * Get the path to the favicon file.
62 public function getPath(): string
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)
72 protected function pngToIco(string $bmpData, int $width, int $height): string
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
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
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);
95 // Join & return the combined parts of the ICO image data
96 return $header . $entry . $bmpData;