<?php
header('Content-Type: image/png');

$size = isset($_GET['s']) ? (int)$_GET['s'] : 192;
$s = max(48, min(1024, $size));

$img = imagecreatetruecolor($s, $s);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparent);

// 背景圓角矩形
$bg = imagecolorallocate($img, 30, 41, 59);
$r = round($s * 0.15);
$points = [];
for ($i = 0; $i <= 360; $i += 5) {
    $rad = deg2rad($i);
    $cx = $s / 2;
    $cy = $s / 2;
    $hw = ($s / 2) - $r;
    $hh = ($s / 2) - $r;
    $px = $cx + ($hw * cos($rad) + $r * cos($rad) * abs(cos($rad)));
    $py = $cy + ($hh * sin($rad) + $r * sin($rad) * abs(sin($rad)));
    $points[] = $px;
    $points[] = $py;
}
imagefilledpolygon($img, $points, count($points) / 2, $bg);

// 主方塊
$blue = imagecolorallocate($img, 59, 130, 246);
$blue_light = imagecolorallocatealpha($img, 59, 130, 246, 40);
$m = round($s * 0.15);
$bx = $m;
$by = round($s * 0.22);
$bw = $s - 2 * $m;
$bh = round($s * 0.48);
$cr = round($s * 0.03);

// 主方塊背景
for ($y = $by + $cr; $y < $by + $bh - $cr; $y++) {
    for ($x = $bx; $x < $bx + $bw; $x++) {
        $dist = min(abs($x - $bx), abs($x - ($bx + $bw - 1)), abs($y - $by), abs($y - ($by + $bh - 1)));
        if ($dist <= $cr) {
            $alpha = 127 * (1 - $dist / $cr) * 0.3;
            $c = imagecolorallocatealpha($img, 59, 130, 246, $alpha);
            imagesetpixel($img, $x, $y, $c);
        } else {
            imagesetpixel($img, $x, $y, $blue_light);
        }
    }
}

// 主方塊邊框
imagesetthickness($img, max(2, round($s * 0.04)));
imagerectangle($img, $bx, $by, $bx + $bw, $by + $bh, $blue);

// 中間十字線
$cx = $s / 2;
$cy = $by + $bh / 2;
$line_alpha = imagecolorallocatealpha($img, 59, 130, 246, 80);
imagesetthickness($img, max(1, round($s * 0.025)));
imageline($img, $bx, $cy, $bx + $bw, $cy, $line_alpha);
imageline($img, $cx, $by, $cx, $by + $bh, $line_alpha);

// 四個小方塊
$bs = round($s * 0.16);
$gap = round($s * 0.06);
$blue_full = imagecolorallocatealpha($img, 59, 130, 246, 60);
$blue_dark = imagecolorallocatealpha($img, 59, 130, 246, 100);

// 左上
imagefilledrectangle($img, $bx + $gap, $by + $gap, $bx + $gap + $bs, $by + $gap + $bs, $blue_dark);
// 右上
imagefilledrectangle($img, $bx + $bw - $gap - $bs, $by + $gap, $bx + $bw - $gap, $by + $gap + $bs, $blue_full);
// 左下
imagefilledrectangle($img, $bx + $gap, $by + $bh - $gap - $bs, $bx + $gap + $bs, $by + $bh - $gap, $blue_full);
// 右下
imagefilledrectangle($img, $bx + $bw - $gap - $bs, $by + $bh - $gap - $bs, $bx + $bw - $gap, $by + $bh - $gap, $blue_dark);

// 底部文件條
$yellow = imagecolorallocate($img, 245, 158, 11);
$ybx = round($s * 0.38);
$yby = round($s * 0.76);
$ybw = round($s * 0.24);
$ybh = round($s * 0.10);
imagefilledrectangle($img, $ybx, $yby, $ybx + $ybw, $yby + $ybh, $yellow);

// 勾勾（右上角）
$green = imagecolorallocate($img, 34, 197, 94);
if ($s > 100) {
    $gcx = round($s * 0.25);
    $gcy = round($s * 0.17);
    $gs = round($s * 0.08);
    imagefilledellipse($img, $gcx, $gcy, $gs * 2, $gs * 2, $green);
    $white = imagecolorallocate($img, 255, 255, 255);
    imagesetthickness($img, max(2, round($s * 0.015)));
    $gs2 = round($gs * 0.4);
    imageline($img, $gcx - $gs2, $gcy, $gcx - $gs2 * 0.3, $gcy + $gs2, $white);
    imageline($img, $gcx - $gs2 * 0.3, $gcy + $gs2, $gcx + $gs2, $gcy - $gs2 * 0.5, $white);
}

imagepng($img);
imagedestroy($img);
