3 namespace BookStack\Uploads;
5 use Illuminate\Http\UploadedFile;
9 protected string $path;
11 public function __construct(
12 protected ImageResizer $imageResizer,
14 $this->path = public_path('favicon.ico');
18 * Save the given UploadedFile instance as the application favicon.
20 public function saveForUploadedImage(UploadedFile $file): void
22 if (!is_writeable($this->path)) {
26 $imageData = file_get_contents($file->getRealPath());
27 $pngData = $this->imageResizer->resizeImageData($imageData, 32, 32, false, 'png');
28 $icoData = $this->pngToIco($pngData, 32, 32);
30 file_put_contents($this->path, $icoData);
34 * Restore the original favicon image.
35 * Returned boolean indicates if the copy occurred.
37 public function restoreOriginal(): bool
39 $permissionItem = file_exists($this->path) ? $this->path : dirname($this->path);
40 if (!is_writeable($permissionItem)) {
44 return copy($this->getOriginalPath(), $this->path);
48 * Restore the original favicon image if no favicon image is already in use.
49 * Returns a boolean to indicate if the file exists.
51 public function restoreOriginalIfNotExists(): bool
53 if (file_exists($this->path)) {
57 return $this->restoreOriginal();
61 * Get the path to the favicon file.
63 public function getPath(): string
69 * Get the path of the original favicon copy.
71 public function getOriginalPath(): string
73 return public_path('icon.ico');
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)
81 protected function pngToIco(string $pngData, int $width, int $height): string
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
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
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);
104 // Join & return the combined parts of the ICO image data
105 return $header . $entry . $pngData;