<?php


function sendGetPage () {
  
//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'];
  
$x3 = $_POST['x3'];
  
$y3 = $_POST['y3'];
  
$xcur = $_POST['xcur'];
  
$ycur = $_POST['ycur'];
  
$points = $_POST['points'];

echo <<< endOfGetPage
<html>
<head>
<title>PHP Sierpinski triangle</title>
</head>
<body>
<form name="drawingForm"
action="http://$serverNamePhpSelf";
method="post">
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>
<br>
Vertices of triangle:
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
<input type="text" size="3" maxlength="4"  name="x2" value="$x2">
Y2
<input type="text" size="3" maxlength="4"  name="y2" value="$y2">
X3
<input type="text" size="3" maxlength="4"  name="x3" value="$x3">
Y3
<input type="text" size="3" maxlength="4"  name="y3" value="$y3">
<br>
  Starting point:
XStart
<input type="text" size="3" maxlength="4"  name="xcur" value="$xcur">
YStart
<input type="text" size="3" maxlength="4"  name="ycur" value="$ycur">
<br>
  Number of points:
<input type="text" size="5" maxlength="7"  name="points" value="$points">
<br>
<input value="Draw it" type="submit">
</form>
endOfGetPage;
}

//initial page load.  display form
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  
unlink("sierpinski.png");
  
bail("");   
}



$width = $_POST['width'];
$height = $_POST['height'];
$x = array();
$y = array();

$x[] = $_POST['x1'];
$y[] = $_POST['y1'];
$x[] = $_POST['x2'];
$y[] = $_POST['y2'];
$x[] = $_POST['x3'];
$y[] = $_POST['y3'];
$xcur = $_POST['xcur'];
$ycur = $_POST['ycur'];
$points = $_POST['points'];

//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($x[0]) || !is_numeric($y[0]) ||
    !
is_numeric($x[2]) || !is_numeric($y[2]) ||
    !
is_numeric($x[1]) || !is_numeric($y[1]) ||
    !
is_numeric($xcur) || !is_numeric($ycur) ||
    !
is_numeric($points) || $points<0 || $points>1000000 ||
    
$width<1 || $width>1000 || $height<1 || $height>1000) {
  
bail( "BAD INPUT. width: $width h: $height  or somewhere...\n");
}  
//larger than 1000 crashes???


$im = ImageCreateTrueColor($width, $height);

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



$black = ImageColorAllocate($im, 0, 0, 0);

$color = $black;  


for (
$i=1; $i<=$points; $i++) {
  
$r = rand(0,2);
  
$xcur = ($x[$r] + $xcur) / 2;
  
$ycur = ($y[$r] + $ycur) / 2;
  
ImageSetPixel($im, $xcur, $ycur, $color);
}




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

sendGetPage();
echo
"<hr>\n<img src=\"sierpinski.png\">\n";
echo
"</body>\n</html>";





function
bail ($reason) {
  echo
$reason;
  
sendGetPage();
  echo
"</body>\n</html>";
  exit();
}

?>