<?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;

 

 

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']));

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

  $h=str_replace('+',' ',urldecode($_POST['height']));

}

 

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

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

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

  $svgprefix='';

  $svgsuffix='';

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

    $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']) || $x == 0 || $y == 0 || $x == $w || $y == (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 . $svgsuffix;

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

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

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

  $svgprefix='';

  $svgsuffix='';

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

    $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($_POST['background']) || $x == 0 || $y == 0 || $x == $w || $y == (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 . $svgsuffix;

} 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>';

}

?>