<?php

function sendGetPage () {
  
//too many ' and " in heredoc?
  
$serverNamePhpSelf = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

  global
$width, $height, $colorscheme, $fileformat, $jpegquality, $fillpercent,
    
$blockwidth, $blockheight, $randomregular, $rowoffset, $seedrandom, $seed,
    
$background, $customcolorred, $customcolorgreen, $customcolorblue;

  if (isset(
$blockwidth))
    
$blockWidthValue = $blockwidth;
  else
    
$blockWidthValue = "10";
  if (isset(
$blockheight))
    
$blockHeightValue = $blockheight;
  else
    
$blockHeightValue = "10";

  if (isset(
$width))
    
$widthValue = $width;
  else
    
$widthValue = "300";
  if (isset(
$height))
    
$heightValue = $height;
  else
    
$heightValue = "300";

  if (!isset(
$randomregular) || $randomregular=="random")
    
$randomregularRandomchecked = "checked";
  elseif (
$randomregular =="regular")
    
$randomregularRegularchecked = "checked";

  if (isset(
$rowoffset))
    
$rowoffsetValue = $rowoffset;
  else
    
$rowoffsetValue = "0";

  if (!isset(
$background) || $background=="White")
    
$backgroundWhiteSelected = "selected";
  elseif (
$background == "Black")
    
$backgroundBlackSelected = "selected";
  elseif (
$background == "Red")
    
$backgroundRedSelected = "selected";
  elseif (
$background == "Green")
    
$backgroundGreenSelected = "selected";
  elseif (
$background == "Blue")
    
$backgroundBlueSelected = "selected";
  elseif (
$background == "Cyan")
    
$backgroundCyanSelected = "selected";
  elseif (
$background == "Magenta")
    
$backgroundMagentaSelected = "selected";
  elseif (
$background == "Yellow")
    
$backgroundYellowSelected = "selected";
  elseif (
$background == "CUSTOM")
    
$backgroundCUSTOMSelected = "selected";

  if (
$background == "CUSTOM") {
    
$customcolorredValue = $customcolorred;
    
$customcolorgreenValue = $customcolorgreen;
    
$customcolorblueValue = $customcolorblue;
  }

  if (
$seedrandom == "yes")
    
$seedrandomYesChecked = "checked";

  if (isset(
$seed))
    
$seedValue = $seed;
  else
    
$seedValue = "1";

  if (!isset(
$colorscheme) || $colorscheme=="RGB")
    
$colorschemeRGBchecked = "checked";
  elseif (
$colorscheme=="BW")
    
$colorschemeBWchecked = "checked";
  elseif (
$colorscheme=="8colors")
    
$colorscheme8colorschecked = "checked";
  elseif (
$colorscheme=="16HTMLcolors")
    
$colorscheme16HTMLcolorschecked = "checked";

  if (!isset(
$fileformat) || $fileformat=="PNG")
    
$fileformatPNGchecked = "checked";
  elseif (
$fileformat=="JPEG")
    
$fileformatJPEGchecked = "checked";
  elseif (
$fileformat=="GIF")
    
$fileformatGIFchecked = "checked";
  elseif (
$fileformat=="WBMP")
    
$fileformatWBMPchecked = "checked";

  if (
$fileformat == "JPEG")
    
$jpegqualityValue = $jpegquality;
  else
    
$jpegqualityValue = "7.5";

  if (isset(
$fillpercent))
    
$fillpercentValue = $fillpercent;
  else
    
$fillpercentValue = "100";

  echo <<< endOfGetPage
<html>
<head>
<title>PHP generate random images file</title>
</head>
<body>
Generate random or regular image file.  
<br>
<form name="drawingForm"
action="http://$serverNamePhpSelf";
method="post">
  Random or regular (tiling):
<input name="randomregular" value="random" type="radio" $randomregularRandomchecked>Random
<input name="randomregular" value="regular" type="radio" $randomregularRegularchecked>Regular
&nbsp;&nbsp;&nbsp;&nbsp;
  Tiling offset in blocks:
<input type="text" size="2" maxlength="3"  name="rowoffset" value="$rowoffsetValue">
<br>
  Color scheme:
<input name="colorscheme" value="RGB" type="radio" $colorschemeRGBchecked>RGB
<input name="colorscheme" value="BW" type="radio" $colorschemeBWchecked>BW
<input name="colorscheme" value="8colors" type="radio" $colorscheme8colorschecked>8 colors
<input name="colorscheme" value="16HTMLcolors" type="radio" $colorscheme16HTMLcolorschecked>16 HTML colors
<br>
  File format:
<input name="fileformat" value="PNG" type="radio" $fileformatPNGchecked>PNG
<input name="fileformat" value="GIF" type="radio" $fileformatGIFchecked>GIF
<input name="fileformat" value="JPEG" type="radio" $fileformatJPEGchecked>JPEG
<input name="fileformat" value="WBMP" type="radio" $fileformatWBMPchecked>WBMP
<!--<input name="fileformat" value="XBM" type="radio" >XBM-->
&nbsp;&nbsp;&nbsp;&nbsp;
JPEG quality:
<input type="text" size="2" maxlength="4"  name="jpegquality" value="$jpegqualityValue">

<br>
  Same-seeded random?:
<input name="seedrandom" value="yes" type="checkbox" $seedrandomYesChecked>
&nbsp;&nbsp;&nbsp;&nbsp;
Seed:
<input type="text" size="1" maxlength="3"  name="seed" value="$seedValue">
<br>
Percent blocks filled:
<input type="text" size="3" maxlength="3"  name="fillpercent" value="$fillpercentValue">
&nbsp;&nbsp;&nbsp;&nbsp;
Background color:
<select name="background">
<option $backgroundWhiteSelected>White</option>
<option $backgroundBlackSelected>Black</option>
<option $backgroundRedSelected>Red</option>
<option $backgroundGreenSelected>Green</option>
<option $backgroundBlueSelected>Blue</option>
<option $backgroundCyanSelected>Cyan</option>
<option $backgroundMagentaSelected>Magenta</option>
<option $backgroundYellowSelected>Yellow</option>
<option $backgroundCUSTOMSelected>CUSTOM</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
CUSTOM background color:
R
<input type="text" size="2" maxlength="3"  name="customcolorred" value="$customcolorredValue">
G
<input type="text" size="2" maxlength="3"  name="customcolorgreen" value="$customcolorgreenValue">
B
<input type="text" size="2" maxlength="3"  name="customcolorblue" value="$customcolorblueValue">

<br>
  Block size: Width:
<input type="text" size="2" maxlength="4"  name="blockwidth" value="$blockWidthValue">
  Height:
<input type="text" size="2" maxlength="4"  name="blockheight" value="$blockHeightValue">

<br>
Image size: Width
<input type="text" size="3" maxlength="4"  name="width" value="$widthValue">
Height
<input type="text" size="3" maxlength="4"  name="height" value="$heightValue">
&nbsp;&nbsp;&nbsp;&nbsp;
<input value="Generate it" type="submit">
</form>
<!--<a href="rand_viewimages.html">View the last 3 jpeg gif png images</a>-->
endOfGetPage;
}

//initial page load
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  
sendGetPage();
  echo
"</body>\n</html>";
  exit();  
}

//posted:

$width = $_POST['width'];
$height = $_POST['height'];
$randomregular = $_POST['randomregular'];
$rowoffset = $_POST['rowoffset'];
$fileformat = $_POST['fileformat'];
$jpegquality = $_POST['jpegquality'];
$colorscheme = $_POST['colorscheme'];
$seedrandom = $_POST['seedrandom'];
$seed = $_POST['seed'];
$fillpercent = $_POST['fillpercent'];
$background = $_POST['background'];
$customcolorred = $_POST['customcolorred'];
$customcolorgreen = $_POST['customcolorgreen'];
$customcolorblue = $_POST['customcolorblue'];
$blockwidth = $_POST['blockwidth'];
$blockheight = $_POST['blockheight'];

//craps out if size > 1000 ???

//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($fillpercent) ||
    !
is_numeric($blockwidth) || !is_numeric($blockheight) || !is_numeric($rowoffset) ||
    !
is_numeric($seed) ||
    
$width<1 || $width>1000 || $height<1 || $height>1000 ||
    
$blockwidth<1 || $blockwidth>$width || $blockheight<1 || $blockheight>$height ||
    
$fillpercent<0 || $fillpercent>100 ||
    
$rowoffset<0 ||
    (
$fileformat=="JPEG" &&
     (!
is_numeric($jpegquality) || $jpegquality<0 || $jpegquality>10)) ||
    (
$background=="CUSTOM" &&
     (!
is_numeric($customcolorred) ||
      !
is_numeric($customcolorgreen) ||
      !
is_numeric($customcolorblue) ||
      
$customcolorred<0 || $customcolorred>255 ||
      
$customcolorgreen<0 || $customcolorgreen>255 ||
      
$customcolorblue<0 || $customcolorblue>255)    )) {
  
sendGetPage();
  echo
"<p><b>BAD INPUT. width: $width h: $height fillpercent: $fillpercent </b>\n";
  echo
"<b>BAD INPUT. blockwidth: $blockwidth blockheight: $blockheight </b>\n";
  echo
"or somewhere \n";
echo
"</body>\n</html>";
  exit();
}  


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

ImageAlphaBlending($im, false);
if (
$fillpercent==100 || $background=="White")  //common case
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 255, 255, 255));
elseif (
$background == "Black")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 0, 0, 0));
elseif (
$background == "Red")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 255, 0, 0));
elseif (
$background == "Green")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 0, 255, 0));
elseif (
$background == "Blue")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 0, 0, 255));
elseif (
$background == "Cyan")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 0, 255, 255));
elseif (
$background == "Magenta")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 255, 0, 255));
elseif (
$background == "Yellow")
     
ImageFilledRectangle($im, 0, 0, $width, $height, ImageColorAllocate($im, 255, 255, 0));
elseif (
$background == "CUSTOM")
     
ImageFilledRectangle($im, 0, 0, $width, $height,
              
ImageColorAllocate($im, $customcolorred,
                         
$customcolorgreen,
                         
$customcolorblue ));


$colors['white'] = ImageColorAllocate($im, 255, 255, 255);
$colors['black'] = ImageColorAllocate($im, 0, 0, 0);
$colors[0] = &$colors['white'];
$colors[1] = &$colors['black'];
if (
colorscheme != "BW") {
  
//+ RGB:
  
$colors['red'] = ImageColorAllocate($im, 255, 0, 0);
  
$colors['blue'] = ImageColorAllocate($im, 0, 0, 255);
  
$colors['green'] = ImageColorAllocate($im, 0, 255, 0);
  
$colors[2] = &$colors['red'];
  
$colors[3] = &$colors['blue'];
  
$colors[4] = &$colors['green'];
  if (
colorscheme != "RGB") {
      
//+ 8 colors:
    
$colors['yellow'] = ImageColorAllocate($im, 255, 255, 0);
    
$colors['cyan'] = ImageColorAllocate($im, 0, 255, 255);
    
$colors['magenta'] = ImageColorAllocate($im, 255, 0, 255);
    
$colors[5] = &$colors['yellow'];
    
$colors[6] = &$colors['cyan'];
    
$colors[7] = &$colors['magenta'];
    if (
colorscheme != "8colors") {
      
//+ 16 HTML colors
      
$colors['gray'] = ImageColorAllocate($im,128,128,128);
      
$colors['html_green'] = ImageColorAllocate($im, 0,128,0);
      
$colors['maroon'] = ImageColorAllocate($im,128,0,0);
      
$colors['navy'] = ImageColorAllocate($im,0,0,128);
      
$colors['olive'] = ImageColorAllocate($im,128,128,0);
      
$colors['silver'] = ImageColorAllocate($im,192,192,192);
      
$colors['teal'] = ImageColorAllocate($im,0,128,128);
      
$colors[8] = &$colors['cyan'];   //== aqua
      
$colors[9] = &$colors['black'];
      
$colors[10] = &$colors['blue'];
      
$colors[11] = &$colors['magenta'];    //==fuchsia
      
$colors[12] = &$colors['gray'];
      
$colors[13] = &$colors['html_green'];
      
$colors[14] = &$colors['green'];    //==lime
      
$colors[15] = &$colors['maroon'];
      
$colors[16] = &$colors['navy'];
      
$colors[17] = &$colors['olive'];
      
$colors[18] = &$colors['purple'];
      
$colors[19] = &$colors['red'];
      
$colors[20] = &$colors['silver'];
      
$colors[21] = &$colors['teal'];
      
$colors[22] = &$colors['yellow'];
      
$colors[23] = &$colors['white'];
    }
  }
}


if (
$seedrandom == "yes")
     
srand($seed);

if (
$randomregular == "random")    //boolean for loop efficiency
     
$randreg = true;
else {    
//regular tiling
  
$randreg = false;
  if (
$rowoffset > 0)
    switch (
$colorscheme) {  //normalize user's rowoffset value
    
case "RGB": $rowoffset = $rowoffset % 3;
      
$shiftoffset = 3;
      break;
    case
"BW": $rowoffset = $rowoffset % 2;
      
$shiftoffset = 2;
      break;
    case
"8colors": $rowoffset = $rowoffset % 8;
      
$shiftoffset = 8;
      break;
    case
"16HTMLcolors": $rowoffset = $rowoffset % 16;
      
$shiftoffset = 16;
      break;
    }
}

for (
$y=0; $y<$height; $y+=$blockheight) {         //rows
  
if ($shiftoffset > 0)  //!randreg)    //regular tiling
    
$rowblock = $y/$blockheight * $rowoffset % $shiftoffset;    //tiling row's block counter
  
else
    
$rowblock = 0;     //each row like others

  
for ($x=0; $x<$width; $x+=$blockwidth) {             //columns
    
if ($randreg)
      switch (
$colorscheme) {
      case
"RGB": $color = $colors[rand(2,4)];
    break;
      case
"BW": $color = $colors[rand(0,1)];
    break;
      case
"8colors": $color = $colors[rand(0,7)];
    break;
      case
"16HTMLcolors": $color = $colors[rand(8,23)];
    break;
      }
    else {   
//regular tiling
      
switch ($colorscheme) {
      case
"RGB": $color = $colors[$rowblock%3 + 2]; //cycle around indexes 2,3,4
    
break;
      case
"BW": $color = $colors[$rowblock%2];
    break;
      case
"8colors": $color = $colors[$rowblock%8];
    break;
      case
"16HTMLcolors": $color = $colors[$rowblock%16 + 8]; //cycle around indexes 8-23
    
break;
      }
      
$rowblock++;
    }
    if (
$fillpercent == 100)  //common case
      
if ($blockwidth==1 && $blockheight==1)
    
ImageSetPixel($im, $x, $y, $color);
      else
    
ImageFilledRectangle($im, $x, $y, $x+$blockwidth, $y+$blockheight, $color);
    elseif (
$fillpercent >= rand(1,100))
      if (
$blockwidth==1 && $blockheight==1)
    
ImageSetPixel($im, $x, $y, $color);
      else
    
ImageFilledRectangle($im, $x, $y, $x+$blockwidth, $y+$blockheight, $color);
  }
}


sendGetPage("readonly");


switch (
$fileformat) {
case
"JPEG":
   
ImageJPEG($im,"randomimagefile.jpg", $jpegquality);
   echo
"<hr>\n<img src=\"randomimagefile.jpg\">\n";
   echo
"<br>File size: ".filesize("randomimagefile.jpg")." bytes\n";
   break;
case
"WBMP":   //wireless bitmap ?!?!
   
ImageWBMP($im,"randomimagefile.wbmp");
   echo
"<hr>\n<img src=\"randomimagefile.wbmp\">\n";
   echo
"<br>File size: ".filesize("randomimagefile.wbmp")." bytes\n";
   break;
case
"PNG":
   
ImagePNG($im,"randomimagefile.png");
   echo
"<hr>\n<img src=\"randomimagefile.png\">\n";
   echo
"<br>File size: ".filesize("randomimagefile.png")." bytes\n";
   break;
case
"XBM":    //error
   
ImageXBM($im,"randomimagefile.xbm");
   echo
"<hr>\n<img src=\"randomimagefile.xbm\">\n";
   echo
"<br>File size: ".filesize("randomimagefile.xbm")." bytes\n";
   break;
case
"GIF":
   
ImageGIF($im,"randomimagefile.gif");
   echo
"<hr>\n<img src=\"randomimagefile.gif\">\n";
   echo
"<br>File size: ".filesize("randomimagefile.gif")." bytes\n";
   break;
}


echo
"</body>\n</html>";
?>