<?php
$text = isset($_GET['text']) ? urldecode($_GET['text']) : 'Зображення';
$w    = min(max((int)($_GET['w'] ?? 800), 100), 1200);
$h    = min(max((int)($_GET['h'] ?? 450), 60), 800);

$text = htmlspecialchars($text, ENT_XML1, 'UTF-8');

// ── Шрифт ──────────────────────────────────────────────────
// Змінюй тут, розкоментовуючи потрібний рядок:

$font = 'Russo One, Arial, sans-serif';
// $font = 'Unbounded, Arial, sans-serif';       // сучасний, широкий
// $font = 'Dela Gothic One, Arial, sans-serif'; // великий блок
// $font = 'Exo 2, Arial, sans-serif';           // техно
// $font = 'Golos Text, Arial, sans-serif';      // мінімалізм
// $font = 'Commissioner, Arial, sans-serif';    // гротеск
// $font = 'Montserrat, Arial, sans-serif';      // геометрія
// $font = 'Raleway, Arial, sans-serif';         // елегантний
// $font = 'Oswald, Arial, sans-serif';          // конденсований
// $font = 'Bernier Shade, Arial, sans-serif';   // декоративний (потребує @font-face на сервері)

// ── Автопідгон розміру шрифту (1 символ → великий, 90 → малий) ──
$len    = max(1, mb_strlen($text));
$maxFs  = round($h * 0.30);
$minFs  = round($h * 0.09);
$t      = min(1, log($len) / log(90));
$fs     = round(max($minFs, $maxFs - $t * ($maxFs - $minFs)));

// ── Перенос тексту по рядках ─────────────────────────────────
// Ліміт символів на рядок залежить від розміру шрифту
$charsPerLine = max(8, (int)round(($w * 0.84) / ($fs * 0.55)));

$words = explode(' ', $text);
$lines = []; $line = '';
foreach ($words as $word) {
    $test = $line ? $line . ' ' . $word : $word;
    if (mb_strlen($test) <= $charsPerLine) {
        $line = $test;
    } else {
        if ($line) $lines[] = $line;
        $line = $word;
    }
}
if ($line) $lines[] = $line;
$lines = array_slice($lines, 0, 4);

// ── Позиції тексту (вертикально по центру) ────────────────────
$lineH      = round($fs * 1.2);
$totalH     = count($lines) * $lineH;
$startY     = round($h / 2 - $totalH / 2 + $fs * 0.85);

// ── Шари тексту (імітація glow-ефекту без JS) ─────────────────
// Шар A — широкий розмитий glow
$layerA = '';
foreach ($lines as $i => $l) {
    $y = $startY + $i * $lineH;
    $layerA .= '<text x="50%" y="' . $y . '" text-anchor="middle"'
             . ' font-family="' . $font . '" font-size="' . $fs . '"'
             . ' font-weight="900" letter-spacing="0.03em"'
             . ' fill="white" filter="url(#blurHeavy)" opacity="0.35">' . $l . '</text>';
}

// Шар B — середній glow
$layerB = '';
foreach ($lines as $i => $l) {
    $y = $startY + $i * $lineH;
    $layerB .= '<text x="50%" y="' . $y . '" text-anchor="middle"'
             . ' font-family="' . $font . '" font-size="' . $fs . '"'
             . ' font-weight="900" letter-spacing="0.03em"'
             . ' fill="white" filter="url(#blurMed)" opacity="0.4">' . $l . '</text>';
}

// Шар C — чіткий текст з glow
$layerC = '';
foreach ($lines as $i => $l) {
    $y = $startY + $i * $lineH;
    $layerC .= '<text x="50%" y="' . $y . '" text-anchor="middle"'
             . ' font-family="' . $font . '" font-size="' . $fs . '"'
             . ' font-weight="900" letter-spacing="0.03em"'
             . ' fill="white" filter="url(#textGlow)">' . $l . '</text>';
}

// ── Розміри ───────────────────────────────────────────────────
$cx  = (int)($w / 2);
$w4  = $w - 4;
$h4  = $h - 4;

// ── Заголовок HTTP ────────────────────────────────────────────
header('Content-Type: image/svg+xml; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<svg xmlns="http://www.w3.org/2000/svg" width="' . $w . '" height="' . $h . '" viewBox="0 0 ' . $w . ' ' . $h . '">'

. '<defs>'

// Google Fonts — підключення шрифтів
// (працює коли SVG відкривається напряму в браузері, не через <img>)
. '<style>'
. '@import url(\'https://fonts.googleapis.com/css2?family=Russo+One&amp;family=Unbounded:wght@900&amp;family=Dela+Gothic+One&amp;family=Exo+2:wght@900&amp;family=Golos+Text:wght@900&amp;family=Commissioner:wght@900&amp;family=Montserrat:wght@900&amp;family=Raleway:wght@900&amp;family=Oswald:wght@700&amp;display=swap\');'
. '</style>'

// Градієнт фону: фіолетовий → синьо-фіолетовий → маджента
. '<linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">'
. '<stop offset="0%"   style="stop-color:#4B0082"/>'
. '<stop offset="55%"  style="stop-color:#7B68EE"/>'
. '<stop offset="100%" style="stop-color:#FF00FF"/>'
. '</linearGradient>'

// Центральний glow
. '<radialGradient id="centerGlow" cx="50%" cy="50%" r="55%">'
. '<stop offset="0%"   style="stop-color:#FF00FF;stop-opacity:0.22"/>'
. '<stop offset="100%" style="stop-color:#4B0082;stop-opacity:0"/>'
. '</radialGradient>'

// Фільтр: сильний blur (шар A)
. '<filter id="blurHeavy" x="-30%" y="-30%" width="160%" height="160%">'
. '<feGaussianBlur in="SourceGraphic" stdDeviation="12"/>'
. '</filter>'

// Фільтр: середній blur (шар B)
. '<filter id="blurMed" x="-15%" y="-15%" width="130%" height="130%">'
. '<feGaussianBlur in="SourceGraphic" stdDeviation="4"/>'
. '</filter>'

// Фільтр: glow для чіткого тексту (шар C)
. '<filter id="textGlow" x="-15%" y="-15%" width="130%" height="130%">'
. '<feGaussianBlur in="SourceGraphic" stdDeviation="3" result="blur"/>'
. '<feComposite in="SourceGraphic" in2="blur" operator="over"/>'
. '</filter>'

. '</defs>'

// Фон
. '<rect width="' . $w . '" height="' . $h . '" fill="url(#bg)" rx="8"/>'

// Центральний glow
. '<ellipse cx="' . $cx . '" cy="' . round($h/2) . '" rx="' . round($w*0.55) . '" ry="' . round($h*0.55) . '" fill="url(#centerGlow)"/>'

// Горизонтальна смуга сканлайнів (легка)
. '<rect width="' . $w . '" height="' . $h . '" rx="8"'
. ' fill="url(#scanlines)" opacity="0.04"/>'

// Текст: шар A (широкий glow)
. $layerA

// Текст: шар B (середній glow)
. $layerB

// Текст: шар C (чіткий з glow)
. $layerC

// Подвійна рамка (біла напівпрозора)
. '<rect x="2" y="2" width="' . $w4 . '" height="' . $h4 . '" fill="none" stroke="white" stroke-width="1.5" rx="7" opacity="0.35"/>'
. '<rect x="6" y="6" width="' . ($w-12) . '" height="' . ($h-12) . '" fill="none" stroke="white" stroke-width="0.5" rx="5" opacity="0.15"/>'

. '</svg>';