<?php

//cgi/cli call of program:
//php -q onepixelimage.php colorname_or_6hexdigits >imagefilename.png
if (!isset($_SERVER['REQUEST_METHOD'])) {
  
//make a one pixel image.
  //run with -q
  //argv1 is color name or 6 hex digit RGB, else white.
  //redirect output.
  
  
$im = ImageCreateTrueColor(1, 1);
  
  if (
$argc != 2)
    
$color = ImageColorAllocate($im, 255, 255, 255);
  else {
    
$color = getcolor($argv[1]);

    
//             somehow black is false???
    
if (!$color && strtolower($argv[1])!="black") {   //argv[1] not a valid colorname, so see if is 6 hex digits
      
$arg1 = strtolower($argv[1]);
      if (
strlen($arg1)!=6 || strspn($arg1,'abcdef0123456789')!=6) {
    
$stderr = fopen("php://stderr","w");
    
fwrite($stderr, "gack! must be 6 hex digits or a standard color name\n");
    exit();
      }
      
$hexes = sscanf($arg1,"%2s%2s%2s");
      
$color = ImageColorAllocate($im,hexdec($hexes[0]),hexdec($hexes[1]),hexdec($hexes[2]));
    }  
  }
  
ImageAlphaBlending($im, false);
  
ImageFilledRectangle($im, 0, 0, 1, 1, $color);
  
ImagePNG($im);
  
// ImageGIF($im);
}

//initial GET of web page
else if (!isset($_GET['colorname']) && !isset($_GET['hex6digits'])) {
echo <<< endofgetpage
<html>
<head>
<title>Make a one pixel image</title>

<script language="javascript">
<!--Hide from non-JavaScript browsers

//form submittal verifier.
//Check that either a colorname selected or 6 hex digits entered

function verify()
{
  var msg;              //string to contain error msgs
  
  if (document.colorform.colorname.selectedIndex==0 &&
      document.colorform.hex6digits.value.match(/^ *
$/))
    msg = "Either select a color name or enter 6 hex digits";

  else if (document.colorform.hex6digits.value.length>0 &&
       !document.colorform.hex6digits.value.match(/^
[0-9a-fA-F]{6}$/))
    msg = "Must be 6 hex digits (0123456789abcdef)";

  if (msg.length > 0)
{       //if any error
    alert(msg);                 //inform user of the errors.
    return false;               //prevents form submittal
  
}
  else
    return true;                //form is OK, submit to server
}    
//-->
</script>
</head>
<body>
Make a one pixel image
<br>
Select a color name or enter 6 hex digits of RGB
<br>
<form name="colorform" action="$PHP_SELF" onsubmit="return verify();">
RGB 6 hex digits
<input type="text" size="6" maxlength="6" name="hex6digits">
Color name
<select name="colorname">
<option>
<option>white
<option>black
<option>red
<option>blue
<option>lime
<option>green
<option>yellow
<option>aqua_cyan
<option>fuchsia_magenta
<option>gray
<option>html_green
<option>maroon
<option>navy
<option>olive
<option>silver
<option>teal
</select>
<input type="submit" value="Make it and send it to me now">
</form>
</body>
</html>
endofgetpage;
}

else if (
$_GET['hex6digits'] != "") {

  
$arg1 = strtolower($_GET['hex6digits']);
  if (
strlen($arg1)!=6 || strspn($arg1,'abcdef0123456789')!=6) {
    echo
"gack! must be 6 hex digits or a standard color name\n";
    exit();
  }

  
$im = ImageCreateTrueColor(1, 1);

  
$hexes = sscanf($arg1,"%2s%2s%2s");
  
$color = ImageColorAllocate($im,hexdec($hexes[0]),hexdec($hexes[1]),hexdec($hexes[2]));
  
  
ImageAlphaBlending($im, false);
  
ImageFilledRectangle($im, 0, 0, 1, 1, $color);
  
header("Content-type: image/png");  
  
ImagePNG($im);
}

else if (
$_GET['colorname'] != "") {
  
$im = ImageCreateTrueColor(1, 1);
  
  
$color = getcolor($_GET['colorname']);

  
ImageAlphaBlending($im, false);
  
ImageFilledRectangle($im, 0, 0, 1, 1, $color);
  
header("Content-type: image/png");  
  
ImagePNG($im);
}


function
getcolor($colorname) {
  global
$im;
  switch (
strtolower($colorname)) {
  case
'white': $color = ImageColorAllocate($im, 255, 255, 255);
    break;
  case
'black': $color = ImageColorAllocate($im, 0, 0, 0);
    break;
  case
'red': $color = ImageColorAllocate($im, 255, 0, 0);
    break;
  case
'blue': $color = ImageColorAllocate($im, 0, 0, 255);
    break;
  case
'lime':
  case
'green': $color = ImageColorAllocate($im, 0, 255, 0);
    break;
  case
'yellow': $color = ImageColorAllocate($im, 255, 255, 0);
    break;
  case
'aqua':
  case
'cyan': $color = ImageColorAllocate($im, 0, 255, 255);
    break;
  case
'fuchsia':
  case
'magenta': $color = ImageColorAllocate($im, 255, 0, 255);
    break;
  case
'gray': $color = ImageColorAllocate($im,128,128,128);
    break;
  case
'html_green': $color = ImageColorAllocate($im, 0,128,0);
    break;
  case
'maroon': $color = ImageColorAllocate($im,128,0,0);
    break;
  case
'navy': $color = ImageColorAllocate($im,0,0,128);
    break;
  case
'olive': $color = ImageColorAllocate($im,128,128,0);
    break;
  case
'silver': $color = ImageColorAllocate($im,192,192,192);
    break;
  case
'teal': $color = ImageColorAllocate($im,0,128,128);
    break;
  default: return
false;
  }
  return
$color;
}  

?>