<?php session_start(); //must be at beginning


//read-only of width and height after frist GET
function sendGetPage ($sizeReadonly="") {
  
//too many ' and " in heredoc?
  
$serverNamePhpSelf = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
  
$width = $_POST['width'];
  
$height = $_POST['height'];
  
$x1 = $_POST['x1'];
  
$y1 = $_POST['y1'];
  
$x2 = $_POST['x2'];
  
$y2 = $_POST['y2'];

  
//  $getpage = <<< endOfGetPage
echo <<< endOfGetPage
<html>
<head>
<title>PHP Drawing images</title>
</head>
<!-- presents generic image creator in browser -->
<body>
<form name="drawingForm"
action="http://$serverNamePhpSelf";
method="post">
<input name="shape" value="rectangle" type="radio" checked>Rectangle
<input name="shape" value="ellipse" type="radio" >Ellipse
<input name="shape" value="line" type="radio" >Line
<input name="shape" value="point" type="radio">Point
<br>
X1
<input type="text" size="3" maxlength="4"  name="x1" value="$x1">
Y1
<input type="text" size="3" maxlength="4"  name="y1" value="$y1">
X2/Width
<input type="text" size="3" maxlength="4"  name="x2" value="$x2">
Y2/Height
<input type="text" size="3" maxlength="4"  name="y2" value="$y2">
Image size: Width
<input type="text" size="3" maxlength="4"  name="width" value="$width" $sizeReadonly>
Height
<input type="text" size="3" maxlength="4"  name="height" value="$height" $sizeReadonly>
  Color:
<select name="color">
<option>white</option>
<option selected>black</option>
<option>red</option>
<option>blue</option>
<option>green</option>
<option>yellow</option>
<option>cyan</option>
<option>magenta</option>
</select>
<input value="Draw it" type="submit">
</form>
endOfGetPage;
}

//initial page load.  display form
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  
sendGetPage();
  echo
"</body>\n</html>";
  
unlink("drawingfile.png");
  
//$_SESSION['firstposting'] = true;  //HTTP_SESSION_VARS is pre 4.1
  
exit();  
}



if (
$_SESSION['firstposting']) {
  
$_SESSION['queryHistory'] = "";     // \0 delimited fields
  
$_SESSION['firstposting'] = false;
}


$width = $_POST['width'];
$height = $_POST['height'];
$x1 = $_POST['x1'];
$y1 = $_POST['y1'];
$w = $x2 = $_POST['x2'];
$h = $y2 = $_POST['y2'];
$shape = $_POST['shape'];
$color = $_POST['color'];
//echo "color: ".$color."\n";

//type check and range check here...
//form input is always string!!!  must use is-numeric, not is-int
if (!is_numeric($width) || !is_numeric($height) || !is_numeric($x1) || !is_numeric($y1) ||
    !
is_numeric($x2) || !is_numeric($y2) ||
    
$width<1 || $width>1000 || $height<1 || $height>1000) {
  echo
"BAD INPUT. width: $width h: $height x1: $x1 y1: $y1 x2: $x2 y2: $y2\n";
  
sendGetPage();
  echo
"</body>\n</html>";
  exit();
}  


if (
file_exists("drawingfile.png") && is_readable("drawingfile.png")) {
  
$im = ImageCreateFromPNG("drawingfile.png");
}
else {
  
$im = ImageCreateTrueColor($width, $height);

  
ImageAlphaBlending($im, false);
  
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 255, 255, 255));
}

//could probably do only one each call
$colors['white'] = ImageColorAllocate($im, 255, 255, 255);
$colors['black'] = ImageColorAllocate($im, 0, 0, 0);
$colors['red'] = ImageColorAllocate($im, 255, 0, 0);
$colors['blue'] = ImageColorAllocate($im, 0, 0, 255);
$colors['green'] = ImageColorAllocate($im, 0, 255, 0);
$colors['yellow'] = ImageColorAllocate($im, 255, 255, 0);
$colors['cyan'] = ImageColorAllocate($im, 0, 255, 255);
$colors['magenta'] = ImageColorAllocate($im, 255, 0, 255);

$color = $colors[$color];   //user's color choice

switch ($shape) {
case
"line":
   
ImageLine($im, $x1, $y1, $x2, $y2, $color);
   break;
case
"point":
   
ImageSetPixel($im, $x1, $y1, $color);
   break;
case
"ellipse":
   
ImageFilledEllipse($im, $x1, $y1, $w, $h, $color);
   break;
case
"rectangle":
   
ImageFilledRectangle($im, $x1, $y1, $x2, $y2, $color);
   break;
}


//$gray = ImageColorResolveAlpha($im, 70, 70, 70, 63);
//  ImageAlphaBlending($im, true);
//ImageFilledRectangle($im, 60, 60, 120, 120, $gray);


/*  can only send one part to browser.  multipart stuff is for uploading to server???
$boundary_string = "\n" . "--End" . "\n";
##$end_of_data = "\n" . "--End--" . "\n";

header("Content-type: multipart/form-data; boundary=--End");
header("Content-type: text/html");
echo $boundary_string;
*/

sendGetPage("readonly");

//echo $boundary_string;

/*changed to write to file and use img tag:
header('Content-Type: image/png');
ImagePNG($im);
*/

//my directory needs to be 777 for apache to write to it:
if (($fp = fopen("drawingfile.png","w")) == false) {
  echo
"<p>file open failed!</p>";
  exit();
}
ImagePNG($im,"drawingfile.png");

echo
"<hr>\n<img src=\"drawingfile.png\">\n";
echo
"</body>\n</html>";
?>