$stop) {
[$x1, $y1] = $point($prev);
[$x2, $y2] = $point($stop);
$large = ($stop - $prev) > 0.5 ? 1 : 0;
$paths .= sprintf(
" \n",
$colors[$i],
$c,
$c,
$x1,
$y1,
$R,
$R,
$large,
$x2,
$y2,
);
$prev = $stop;
}
$rx = round($size * $radiusRatio, 2);
$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";