<?php
// myrudimentarydrawing.php
// RJM Programming 12/10/2013

if (isset($_GET['shape'])) {
  if ($_GET['shape'] == "rectangle") {
   $image_canvas = imagecreatetruecolor($_GET['width'] * 2 + $_GET['x'],$_GET['height'] * 2 + $_GET['y']);
   if (strlen($_GET['fill']) > 0) {
     imagefilledrectangle ($image_canvas , $_GET['x'], $_GET['y'], $_GET['width'], $_GET['height'], $_GET['fill'] );
   } else {
     imagerectangle ($image_canvas , $_GET['x'], $_GET['y'], $_GET['width'], $_GET['height'], $_GET['col'] );
   }
   if (strlen($_GET['outfile']) > 0) {
     if (str_replace(".jp", "", $_GET['outfile']) != $_GET['outfile'] || str_replace(".JP", "", $_GET['outfile']) != $_GET['outfile']) {
      imagejpeg($image_canvas, $_GET['outfile']) or die("error saving jpeg");
     } else if (str_replace(".gif", "", $_GET['outfile']) != $_GET['outfile'] || str_replace(".GIF", "", $_GET['outfile']) != $_GET['outfile']) {
      imagegif($image_canvas, $_GET['outfile']) or die("error saving jpeg");
     } else {
      imagepng($image_canvas, $_GET['outfile']) or die("error saving png");
     }
     echo '<html><head></head><body><img title="' . $_GET['outfile'] . '" src="' . $_GET['outfile'] . '"/><p>Image is called ' . $_GET['outfile'] . '</p><script> var qs=prompt("See image above ... Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=rectangle&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '"); window.location = qs; ' . '</script></body></html>';
   } else {
     header('Content-type: image/png');
     imagepng($image_canvas) or die("error saving png");
   }
  } else if ($_GET['shape'] == "ellipse") {
   $image_canvas = imagecreatetruecolor($_GET['width'] * 2 + $_GET['x'],$_GET['height'] * 2 + $_GET['y']);
   if (strlen($_GET['fill']) > 0) {
     imagefilledellipse ($image_canvas , $_GET['x'], $_GET['y'], $_GET['width'], $_GET['height'], $_GET['fill'] );
   } else {
     imageellipse ($image_canvas , $_GET['x'], $_GET['y'], $_GET['width'], $_GET['height'], $_GET['col'] );
   }
   if (strlen($_GET['outfile']) > 0) {
     if (str_replace(".jp", "", $_GET['outfile']) != $_GET['outfile'] || str_replace(".JP", "", $_GET['outfile']) != $_GET['outfile']) {
      imagejpeg($image_canvas, $_GET['outfile']) or die("error saving jpeg");
     } else if (str_replace(".gif", "", $_GET['outfile']) != $_GET['outfile'] || str_replace(".GIF", "", $_GET['outfile']) != $_GET['outfile']) {
      imagegif($image_canvas, $_GET['outfile']) or die("error saving jpeg");
     } else {
      imagepng($image_canvas, $_GET['outfile']) or die("error saving png");
     }
     echo '<html><head></head><body><img title="' . $_GET['outfile'] . '" src="' . $_GET['outfile'] . '"/><p>Image is called ' . $_GET['outfile'] . '</p><script> var qs=prompt("See image above ... Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=ellipse&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '"); window.location = qs; ' . '</script></body></html>';
   } else {
     header('Content-type: image/png');
     imagepng($image_canvas) or die("error saving png");
   }
  } else if (str_replace("?", "", $_SERVER['REQUEST_URI']) != $_SERVER['REQUEST_URI']) { 
   echo '<html><head></head><body><script> var qs=prompt("Illegal URL ... Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '"); window.location = qs; ' . '</script></body></html>';
  } else {
   echo '<html><head></head><body><script> var qs=prompt("Illegal URL ... Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=ellipse&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=rectangle&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344"); window.location = qs; ' . '</script></body></html>';
  }
  // echo '<html><head></head><body><script> alert("here"); </script></body></html>';
} else if (strlen($_SERVER['QUERY_STRING']) > 1 && str_replace("?", "", $_SERVER['REQUEST_URI']) != $_SERVER['REQUEST_URI']) {
  echo '<html><head></head><body><script> var qs=prompt("Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '"); window.location = qs; ' . '</script></body></html>';
} else if (strlen($_SERVER['QUERY_STRING']) > 1) {
  echo '<html><head></head><body><script> var qs=prompt("Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=rectangle&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . "?" . $_SERVER['QUERY_STRING'] . '"); window.location = qs; ' . '</script></body></html>';
} else {
  echo '<html><head></head><body><script> var qs=prompt("Enter URL eg. http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=ellipse&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344","http://' . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . '?shape=rectangle&outfile=&x=30&y=40&width=34&height=45&col=112233&fill=223344"); window.location = qs; ' . '</script></body></html>';
}


?>