2023-02-09 00:06:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Uploads;
|
|
|
|
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Intervention\Image\ImageManager;
|
|
|
|
|
|
|
|
class FaviconHandler
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
protected ImageManager $imageTool
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the given UploadedFile instance as the application favicon.
|
|
|
|
*/
|
|
|
|
public function saveForUploadedImage(UploadedFile $file): void
|
|
|
|
{
|
2023-02-09 14:24:43 +01:00
|
|
|
$targetPath = public_path('favicon.ico');
|
|
|
|
if (!is_writeable($targetPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 00:06:42 +01:00
|
|
|
$imageData = file_get_contents($file->getRealPath());
|
|
|
|
$image = $this->imageTool->make($imageData);
|
|
|
|
$image->resize(32, 32);
|
|
|
|
$bmpData = $image->encode('bmp');
|
|
|
|
$icoData = $this->bmpToIco($bmpData, 32, 32);
|
|
|
|
|
2023-02-09 16:14:41 +01:00
|
|
|
// file_put_contents(public_path('icon.bmp'), $bmpData);
|
|
|
|
// file_put_contents(public_path('icon-test.png'), $image->encode('png'));
|
2023-02-09 14:24:43 +01:00
|
|
|
file_put_contents($targetPath, $icoData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the original favicon image.
|
|
|
|
*/
|
|
|
|
public function restoreOriginal(): void
|
|
|
|
{
|
|
|
|
$targetPath = public_path('favicon.ico');
|
|
|
|
$original = public_path('icon.ico');
|
|
|
|
if (!is_writeable($targetPath)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-09 00:06:42 +01:00
|
|
|
|
2023-02-09 14:24:43 +01:00
|
|
|
copy($original, $targetPath);
|
2023-02-09 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert BMP image data to ICO file format.
|
|
|
|
* Built following the file format info from Wikipedia:
|
|
|
|
* https://en.wikipedia.org/wiki/ICO_(file_format)
|
|
|
|
*/
|
|
|
|
protected function bmpToIco(string $bmpData, int $width, int $height): string
|
|
|
|
{
|
|
|
|
// Trim off the header of the bitmap file
|
|
|
|
$rawBmpData = substr($bmpData, 14);
|
2023-02-09 16:14:41 +01:00
|
|
|
// Double the height in the "BITMAPINFOHEADER" since, when in an ICO file, half
|
|
|
|
// of the image data is expected to be a mask.
|
|
|
|
$rawBmpData[8] = hex2bin(dechex($height * 2));
|
2023-02-09 00:06:42 +01:00
|
|
|
|
|
|
|
// ICO header
|
|
|
|
$header = pack('v', 0x00); // Reserved. Must always be 0
|
|
|
|
$header .= pack('v', 0x01); // Specifies ico image
|
|
|
|
$header .= pack('v', 0x01); // Specifies number of images
|
|
|
|
|
|
|
|
// ICO Image Directory
|
|
|
|
$entry = hex2bin(dechex($width)); // Image width
|
|
|
|
$entry .= hex2bin(dechex($height)); // Image height
|
|
|
|
$entry .= "\0"; // Color palette, typically 0
|
|
|
|
$entry .= "\0"; // Reserved
|
|
|
|
|
2023-02-09 16:14:41 +01:00
|
|
|
// AND mask
|
|
|
|
// $pxCount = $width * $height;
|
|
|
|
// $pxMask = hex2bin('00000000');
|
|
|
|
// $mask = str_repeat($pxMask, $pxCount);
|
|
|
|
$mask = '';
|
|
|
|
|
2023-02-09 00:06:42 +01:00
|
|
|
// Color planes, Appears to remain 1 for bmp image data
|
|
|
|
$entry .= pack('v', 0x01);
|
|
|
|
// Bits per pixel, can range from 1 to 32. From testing conversion
|
2023-02-09 16:14:41 +01:00
|
|
|
// via intervention from png typically provides this as 24.
|
|
|
|
$entry .= pack('v', 0x18);
|
2023-02-09 00:06:42 +01:00
|
|
|
// Size of the image data in bytes
|
2023-02-09 16:14:41 +01:00
|
|
|
$entry .= pack('V', strlen($rawBmpData) + strlen($mask));
|
2023-02-09 00:06:42 +01:00
|
|
|
// Offset of the bmp data from file start
|
|
|
|
$entry .= pack('V', strlen($header) + strlen($entry) + 4);
|
|
|
|
|
|
|
|
// Join & return the combined parts of the ICO image data
|
2023-02-09 16:14:41 +01:00
|
|
|
return $header . $entry . $rawBmpData . $mask;
|
2023-02-09 00:06:42 +01:00
|
|
|
}
|
|
|
|
}
|