3 namespace BookStack\Uploads;
5 use Illuminate\Http\UploadedFile;
6 use Intervention\Image\ImageManager;
10 public function __construct(
11 protected ImageManager $imageTool
16 * Save the given UploadedFile instance as the application favicon.
18 public function saveForUploadedImage(UploadedFile $file): void
20 $targetPath = public_path('favicon.ico');
21 if (!is_writeable($targetPath)) {
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);
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);
37 * Restore the original favicon image.
39 public function restoreOriginal(): void
41 $targetPath = public_path('favicon.ico');
42 $original = public_path('icon.ico');
43 if (!is_writeable($targetPath)) {
47 copy($original, $targetPath);
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)
55 protected function bmpToIco(string $bmpData, int $width, int $height): string
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));
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
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
75 // $pxCount = $width * $height;
76 // $pxMask = hex2bin('00000000');
77 // $mask = str_repeat($pxMask, $pxCount);
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);
90 // Join & return the combined parts of the ICO image data
91 return $header . $entry . $rawBmpData . $mask;