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 $imageData = file_get_contents($file->getRealPath());
21 $image = $this->imageTool->make($imageData);
22 $image->resize(32, 32);
23 $bmpData = $image->encode('bmp');
24 $icoData = $this->bmpToIco($bmpData, 32, 32);
26 // TODO - Below are test paths
27 file_put_contents(public_path('uploads/test.ico'), $icoData);
28 file_put_contents(public_path('uploads/test.bmp'), $bmpData);
30 // TODO - Permission check for icon overwrite
31 // TODO - Write to correct location
32 // TODO - Handle deletion and restore of original icon on user icon clear
36 * Convert BMP image data to ICO file format.
37 * Built following the file format info from Wikipedia:
38 * https://en.wikipedia.org/wiki/ICO_(file_format)
40 protected function bmpToIco(string $bmpData, int $width, int $height): string
42 // Trim off the header of the bitmap file
43 $rawBmpData = substr($bmpData, 14);
46 $header = pack('v', 0x00); // Reserved. Must always be 0
47 $header .= pack('v', 0x01); // Specifies ico image
48 $header .= pack('v', 0x01); // Specifies number of images
50 // ICO Image Directory
51 $entry = hex2bin(dechex($width)); // Image width
52 $entry .= hex2bin(dechex($height)); // Image height
53 $entry .= "\0"; // Color palette, typically 0
54 $entry .= "\0"; // Reserved
56 // Color planes, Appears to remain 1 for bmp image data
57 $entry .= pack('v', 0x01);
58 // Bits per pixel, can range from 1 to 32. From testing conversion
59 // via intervention from png typically provides this as 32.
60 $entry .= pack('v', 0x20);
61 // Size of the image data in bytes
62 $entry .= pack('V', strlen($rawBmpData));
63 // Offset of the bmp data from file start
64 $entry .= pack('V', strlen($header) + strlen($entry) + 4);
66 // Join & return the combined parts of the ICO image data
67 return $header . $entry . $rawBmpData;