<?php
// favicon/icon.php — vygeneruje PNG ikonu (GOICT Rental) v požadované velikosti přes GD.
// Použití: favicon/icon.php?s=32  (povolené: 16,32,48,64,96,128,180,192,512)
// Kreslí přesně dle favicon/icon.svg: zelený zaoblený čtverec + bílé šipky (viewBox 64).

$allowed = [16, 32, 48, 64, 96, 128, 180, 192, 512];
$s = isset($_GET['s']) ? (int)$_GET['s'] : 32;
if (!in_array($s, $allowed, true)) { $s = 32; }

if (!function_exists('imagecreatetruecolor')) {
    // GD není k dispozici – přesměruj na SVG (moderní prohlížeče ho zvládnou)
    header('Location: icon.svg');
    exit;
}

$ss = 4;              // supersampling kvůli hladkým hranám
$S = $s * $ss;
$k = $S / 64.0;      // měřítko z viewBoxu 64

$im = imagecreatetruecolor($S, $S);
imagealphablending($im, false);
imagesavealpha($im, true);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagealphablending($im, true);

$green = imagecolorallocate($im, 0x21, 0xbf, 0x73);
$white = imagecolorallocate($im, 255, 255, 255);

// Zaoblený čtverec (rx = 14 ve viewBoxu 64)
$r = (int) round(14 * $k);
$w = $S; $h = $S;
imagefilledrectangle($im, $r, 0, $w - 1 - $r, $h - 1, $green);
imagefilledrectangle($im, 0, $r, $w - 1, $h - 1 - $r, $green);
$d = 2 * $r;
imagefilledellipse($im, $r, $r, $d, $d, $green);
imagefilledellipse($im, $w - 1 - $r, $r, $d, $d, $green);
imagefilledellipse($im, $r, $h - 1 - $r, $d, $d, $green);
imagefilledellipse($im, $w - 1 - $r, $h - 1 - $r, $d, $d, $green);

$rx = function ($x, $y, $ww, $hh) use ($im, $white, $k) {
    imagefilledrectangle($im, (int)round($x * $k), (int)round($y * $k),
        (int)round(($x + $ww) * $k), (int)round(($y + $hh) * $k), $white);
};
$tri = function ($pts) use ($im, $white, $k) {
    $p = [];
    foreach ($pts as $pt) { $p[] = (int)round($pt[0] * $k); $p[] = (int)round($pt[1] * $k); }
    imagefilledpolygon($im, $p, $white);
};

// Bílé prvky (přesně dle SVG)
$rx(20, 14, 8, 24);                             // levá tyčka nahoře
$tri([[14, 42], [34, 42], [24, 53]]);          // šipka dolů
$rx(36, 26, 8, 24);                             // pravá tyčka dole
$tri([[30, 22], [50, 22], [40, 11]]);          // šipka nahoru

// Downsample na cílovou velikost
$out = imagecreatetruecolor($s, $s);
imagealphablending($out, false);
imagesavealpha($out, true);
imagecopyresampled($out, $im, 0, 0, 0, 0, $s, $s, $S, $S);

header('Content-Type: image/png');
header('Cache-Control: public, max-age=2592000, immutable'); // 30 dní
imagepng($out);
imagedestroy($out);
imagedestroy($im);
