Use the logo as favicon
favicon.svg, favicon.ico (16/32/48) and apple-touch-icon.png, generated from the same geometry as .brand__mark (resources/favicon/generate.php). Linked from all layouts via one element. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
664088e139
commit
82b352c053
10 files changed
+232
-3
No files matched your search
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
// Generates webroot/favicon.svg, favicon.ico and apple-touch-icon.png (needs ext-gd). Run after a logo change:
|
||||
// php resources/favicon/generate.php webroot
|
||||
// Mirrors .brand__mark in webroot/css/plenario.css:
|
||||
// rounded square (radius 6/22), conic gradient 40/25/20/15 % from 12 o'clock, clockwise.
|
||||
declare(strict_types=1);
|
||||
|
||||
$out = rtrim($argv[1] ?? 'webroot', '/\\');
|
||||
$colors = ['#4b3fe0', '#e0457b', '#12a08a', '#f29a1f'];
|
||||
$stops = [0.40, 0.65, 0.85, 1.00];
|
||||
$radiusRatio = 6 / 22;
|
||||
|
||||
// ---- SVG ---------------------------------------------------------------------------------
|
||||
$size = 32;
|
||||
$c = $size / 2;
|
||||
$R = 24; // larger than the corner distance, the clip path trims it
|
||||
$point = function (float $fraction) use ($c, $R): array {
|
||||
$a = deg2rad($fraction * 360);
|
||||
|
||||
return [round($c + $R * sin($a), 3), round($c - $R * cos($a), 3)];
|
||||
};
|
||||
$paths = '';
|
||||
$prev = 0.0;
|
||||
foreach ($stops as $i => $stop) {
|
||||
[$x1, $y1] = $point($prev);
|
||||
[$x2, $y2] = $point($stop);
|
||||
$large = ($stop - $prev) > 0.5 ? 1 : 0;
|
||||
$paths .= sprintf(
|
||||
" <path fill=\"%s\" d=\"M%d %d L%s %s A%d %d 0 %d 1 %s %s Z\"/>\n",
|
||||
$colors[$i],
|
||||
$c,
|
||||
$c,
|
||||
$x1,
|
||||
$y1,
|
||||
$R,
|
||||
$R,
|
||||
$large,
|
||||
$x2,
|
||||
$y2,
|
||||
);
|
||||
$prev = $stop;
|
||||
}
|
||||
$rx = round($size * $radiusRatio, 2);
|
||||
$svg = <<<SVG
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {$size} {$size}" width="{$size}" height="{$size}">
|
||||
<title>PlenarIO</title>
|
||||
<clipPath id="r"><rect width="{$size}" height="{$size}" rx="{$rx}"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
{$paths} </g>
|
||||
</svg>
|
||||
|
||||
SVG;
|
||||
file_put_contents($out . '/favicon.svg', $svg);
|
||||
|
||||
// ---- PNG / ICO (GD, 4x4 supersampling) ---------------------------------------------------
|
||||
$rgb = fn(string $hex): array => [hexdec(substr($hex, 1, 2)), hexdec(substr($hex, 3, 2)), hexdec(substr($hex, 5, 2))];
|
||||
$palette = array_map($rgb, $colors);
|
||||
|
||||
$render = function (int $n, float $padding = 0.0) use ($stops, $palette, $radiusRatio): string {
|
||||
$img = imagecreatetruecolor($n, $n);
|
||||
imagealphablending($img, false);
|
||||
imagesavealpha($img, true);
|
||||
$ss = 4;
|
||||
$inner = $n * (1 - 2 * $padding);
|
||||
$r = $inner * $radiusRatio;
|
||||
$offset = $n * $padding;
|
||||
for ($y = 0; $y < $n; $y++) {
|
||||
for ($x = 0; $x < $n; $x++) {
|
||||
$sum = [0.0, 0.0, 0.0];
|
||||
$cover = 0;
|
||||
for ($sy = 0; $sy < $ss; $sy++) {
|
||||
for ($sx = 0; $sx < $ss; $sx++) {
|
||||
$px = $x + ($sx + 0.5) / $ss - $offset;
|
||||
$py = $y + ($sy + 0.5) / $ss - $offset;
|
||||
if ($px < 0 || $py < 0 || $px > $inner || $py > $inner) {
|
||||
continue;
|
||||
}
|
||||
// rounded corners
|
||||
$cx = min(max($px, $r), $inner - $r);
|
||||
$cy = min(max($py, $r), $inner - $r);
|
||||
if (($px - $cx) ** 2 + ($py - $cy) ** 2 > $r ** 2) {
|
||||
continue;
|
||||
}
|
||||
$angle = atan2($px - $inner / 2, -($py - $inner / 2)); // 0 = top, clockwise
|
||||
$f = fmod($angle / (2 * M_PI) + 1, 1);
|
||||
$k = 0;
|
||||
while ($k < 3 && $f >= $stops[$k]) {
|
||||
$k++;
|
||||
}
|
||||
foreach ($palette[$k] as $j => $v) {
|
||||
$sum[$j] += $v;
|
||||
}
|
||||
$cover++;
|
||||
}
|
||||
}
|
||||
$total = $ss * $ss;
|
||||
if ($cover === 0) {
|
||||
$col = imagecolorallocatealpha($img, 0, 0, 0, 127);
|
||||
} else {
|
||||
$col = imagecolorallocatealpha(
|
||||
$img,
|
||||
(int)round($sum[0] / $cover),
|
||||
(int)round($sum[1] / $cover),
|
||||
(int)round($sum[2] / $cover),
|
||||
(int)round(127 * (1 - $cover / $total)),
|
||||
);
|
||||
}
|
||||
imagesetpixel($img, $x, $y, $col);
|
||||
}
|
||||
}
|
||||
ob_start();
|
||||
imagepng($img, null, 9);
|
||||
$png = (string)ob_get_clean();
|
||||
|
||||
return $png;
|
||||
};
|
||||
|
||||
// favicon.ico with 16, 32 and 48 px (PNG compressed entries)
|
||||
$sizes = [16, 32, 48];
|
||||
$images = array_map(fn(int $n): string => $render($n), $sizes);
|
||||
$ico = pack('vvv', 0, 1, count($sizes));
|
||||
$offset = 6 + 16 * count($sizes);
|
||||
foreach ($sizes as $i => $n) {
|
||||
$ico .= pack('CCCCvvVV', $n, $n, 0, 0, 1, 32, strlen($images[$i]), $offset);
|
||||
$offset += strlen($images[$i]);
|
||||
}
|
||||
file_put_contents($out . '/favicon.ico', $ico . implode('', $images));
|
||||
|
||||
// iOS ignores transparency (fills black) and rounds the corners itself: opaque full-bleed square.
|
||||
$apple = imagecreatetruecolor(180, 180);
|
||||
$stopsPx = $stops;
|
||||
for ($y = 0; $y < 180; $y++) {
|
||||
for ($x = 0; $x < 180; $x++) {
|
||||
$angle = atan2($x + 0.5 - 90, -($y + 0.5 - 90));
|
||||
$f = fmod($angle / (2 * M_PI) + 1, 1);
|
||||
$k = 0;
|
||||
while ($k < 3 && $f >= $stopsPx[$k]) {
|
||||
$k++;
|
||||
}
|
||||
imagesetpixel($apple, $x, $y, imagecolorallocate($apple, ...$palette[$k]));
|
||||
}
|
||||
}
|
||||
imagepng($apple, $out . '/apple-touch-icon.png', 9);
|
||||
echo "ok\n";
|
||||
Reference in new issue
Block a user