<?php

// inline_svg.php

// RJM Programming

// January, 2022

// Thanks to https://stackoverflow.com/questions/1623242/problem-with-dynamic-svg-image-generation

header('Content-type: image/svg+xml');

 

$h=0;

$w=0;

$lastcontent='';

$vbox='';
$pw='100%';
$ph='100%';
$blk='';

 

if (isset($_GET['width'])) {

  $w=str_replace('+',' ',urldecode($_GET['width']));

} else if (isset($_POST['width'])) {

  $w=str_replace('+',' ',urldecode($_POST['width']));

}

if (isset($_GET['height'])) {

  $h=str_replace('+',' ',urldecode($_GET['height']));
  if ($w != 0) { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw='100%'; $ph='100%'; } 

} else if (isset($_POST['height'])) {

  $h=str_replace('+',' ',urldecode($_POST['height']));
  if ($w != 0) { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw='100%'; $ph='100%';  } 

}


if (isset($_GET['image'])) {
  list($width, $height) = getimagesize(str_replace('+',' ',urldecode($_GET['image'])));
  $lastcontent='<image href="' . str_replace('http:','',str_replace('https:','',str_replace('+',' ',urldecode($_GET['image'])) . '" x="20" y="20" height="' . $height . '" width="' . $width . '"/>'));
  if ($h == 0) {
    $h=$height;
  } 
  if ($w == 0) {
    $w=$width;
  }
  if (isset($_GET['insvg'])) {
  $lastcontent='<image href="' . str_replace('http:','',str_replace('https:','',str_replace('+',' ',urldecode($_GET['image'])) . '" x="20" y="20" height="' . $h . '" width="' . $w . '"/>'));
  $w+=40;
  //$w=(($w + 20) / 20) * 20;
  $h+=40;
  //$h=(($h + 20) / 20) * 20;
  if ($vbox != '') { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw=$w; $ph=$h; } 
  }

} else if (isset($_POST['image'])) {
  list($width, $height) = getimagesize(str_replace('+',' ',urldecode($_POST['image'])));
  $lastcontent='<image href="' . str_replace('http:','',str_replace('https:','',str_replace('+',' ',urldecode($_GET['image'])) . '" x="20" y="20" height="' . $height . '" width="' . $width . '"/>'));
  if ($h == 0) {
    $h=$height;
  } 
  if ($w == 0) {
    $w=$width;
  }
  if (isset($_POST['insvg'])) {
  $w+=40;
  //$w=(($w + 20) / 20) * 20;
  $h+=40;
  //$h=(($h + 20) / 20) * 20;
  if ($vbox != '') { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw=$w; $ph=$h;  } 
  }

}

if ($pw == '' && $h != 0) { $pw=$w; }
if ($ph == '' && $h != 0) { $ph=$h; }
 

if (isset($_GET['insvg'])) {

  $innards=str_replace('+',' ',urldecode($_GET['insvg']));

  $preinnards='&#' . str_replace(',',';&#',$innards) . ';';

  $svgprefix='';

  $svgsuffix='';

  if (strpos($innards, '<svg') === false) {

    if (isset($_GET['image'])) {
    $svgprefix='<svg width="' . $pw . '" height="' . $ph . '"' . $blk . ' version="1.1" xmlns="http://www.w3.org/2000/svg">';
    } else {
    $svgprefix='<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">';
    }

    $svgsuffix='</svg>';

    if (trim(str_replace(',','',str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$innards)))))))))))) == '') {

     for ($x=0; $x<=$w; $x+=20) {

      for ($y=0; $y<=(0 + $h); $y+=20) {

       if ($x == 0 && $y == 0) {

         $innards='<text x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         if ($h > 0) { $innards.='<text x="' . $w . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';  }

       } else if (isset($_GET['background']) || isset($_GET['image']) || $x == 0 || $y == 0 || ($x + 20) > $w || ($y + 20) > (0 + $h)) {

         $innards.='<text x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         $innards.='<text x="' . ($w - $x) . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';

       }

      }

     }

    }

  }

  echo '<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"

"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . $svgprefix . $innards . $lastcontent . $svgsuffix;

} else if (isset($_POST['insvg'])) {

  $innards=str_replace('+',' ',urldecode($_POST['insvg']));

  $preinnards='&#' . str_replace(',',';&#',$innards) . ';';

  $svgprefix='';

  $svgsuffix='';

  if (strpos($innards, '<svg') === false) {

    if (isset($_POST['image'])) {
    $svgprefix='<svg width="' . $pw . '" height="' . $ph . '"' . $blk . ' version="1.1" xmlns="http://www.w3.org/2000/svg">';
    } else {
    $svgprefix='<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">';
    }

    $svgsuffix='</svg>';

    if (trim(str_replace(',','',str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$innards)))))))))))) == '') {

     for ($x=0; $x<=$w; $x+=20) {

      for ($y=0; $y<=(0 + $h); $y+=20) {

       if ($x == 0 && $y == 0) {

         $innards='<text x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         if ($h > 0) { $innards.='<text x="' . $w . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';  }

       } else if (isset($_GET['background']) || isset($_GET['image']) || $x == 0 || $y == 0 || ($x + 20) > $w || ($y + 20) > (0 + $h)) {

         $innards.='<text x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         $innards.='<text x="' . ($w - $x) . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';

       }

      }

     }

    }

  }

  echo '<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"

"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . $svgprefix . $innards . $lastcontent . $svgsuffix;

} else if (isset($_POST['image']) || isset($_GET['image'])) {
  echo '<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="' . $w . '" height="' . $h . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . $lastcontent . '
</svg>';

} else {

  echo '<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"

"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">

<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>

</svg>';

}

?>