<?php
 
/* File:    border.php
  * Author:  Paul E. Zaremba III
  * Date:    2004-12-17
  * License: Do what you will, just leave this header intact
  * Parameters:
  *   color            = hex RGB value (default 000000)
  *   red, green, blue = hex representations of the respective colors (00,00,00)
  *   x, y             = width, height (8, 8)
  *   ty               = fi (filler), tl (top-left), br (bottom-right), tr, bl
  */

include_once "cache_ctl.phpi";

/*********************
 * Forward declarations
 *********************/
$ok_to_draw true;
$is_ellipse true;
$type       = ( $_REQUEST['ty'] ? $_REQUEST['ty'] : 'fi' ); // filler by default
$cloc       = array( 'x' => 0'y' => ); // center at 00 by default
$color      = array( 'red'   => 0,
                     
'green' => 0,
                     
'blue'  => );
$size       = array( 'x' => 8'y' => );
$arcstart   0;

if ( 
$_REQUEST['color'] )
{
   
$pm '[a-fA-F0-9]{2}';
   if ( 
preg_match"/($pm)($pm)($pm)/"$_REQUEST['color'], $matches ) )
   {
      list( 
$color['red'], $color['green'], $color['blue'] ) =
         
array_maphexdecarray_slice($matches13) );
   }
   else
   {
      
printf"Invalid color '%s' passed in."$_REQUEST['color'] );
      
$ok_to_draw false;
   }
}

foreach ( 
array_keys$color ) as $col )
{
   
// allow individual overrides
   
if ( $_REQUEST[$col] )
   {
      
$color[$col] = hexdec($_REQUEST[$col]);
   }
   
   if ( 
$color[$col] < || $color[$col] > 255 )
   {
      
printf("Invalid '%s' color given: %s"$col$color[$col] );
      
$ok_to_draw false;
   }
}

// see if we need to override
foreach ( array_keys($size) as $axis )
{
   if ( 
$_REQUEST[$axis] )
   {
      if ( 
$_REQUEST[$axis] > && $_REQUEST[$axis] <= 64)
      {
         
$size[$axis] = $_REQUEST[$axis];
      }
      else
      {
         
printf("Invalid '%s' range given: %s"$axis$_REQUEST[$axis]);
         
$ok_to_draw false;
      }
   }
}

switch ( 
$type )
{
case 
'fi'// filler (solid box)
   
$is_ellipse false;
   break;

case 
'tl':
   
// move center to bottom right
   
$cloc = array ( 'x' => $size['x'] - 1'y' => $size['y'] - );
   
$arcstart 180;
   break;

case 
'tr':
   
// move center to bottom left
   
$cloc['y'] = $size['y'] - 1;
   
$arcstart 270;
   break;

case 
'br':
   
// do nothing
   
$arcstart 0;
   break;

case 
'bl':
   
// move center to top right
   
$cloc['x'] = $size['x'] - 1;
   
$arcstart 90;
   break;

default:
   
printf"Invalid type '%s' given"$type );
   
$ok_to_draw false;
   break;
}

/**************
 * Helper func
 **************/
function scaleSize$size )
{
   return (
$size 2) - 1;
}


// $ok_to_draw is used as a guard so we can check all parameters
// at once (instead of check, reload, check reload) then not draw
// if we found a problem
if ( $ok_to_draw )
{
   
// create the image
   
$image      imagecreate$size['x'], $size['y'] );
   
//imageantialias( $image, true );
   // allocate the background color
   
imagecolorallocatealpha$image255255255);

    
   
// filler user a solid box fill, the rest use ellipse
   
$icol imagecolorallocate$image$color['red'], $color['green'],
                                 
$color['blue'] );

   if ( 
$type == 'fi' )
   {
      
$ok_to_draw imagefill$image00$icol );
   }
   else
   {
      
$ok_to_draw imagefilledarc$image,
                                    
$cloc['x'], $cloc['y'],
                                    
scaleSize($size['x']), scaleSize($size['y']),
                                    
$arcstart$arcstart 90,
                                    
$icol,
                                    
IMG_ARC_PIE );
   }

   if ( 
$ok_to_draw )
   {
      
header("Content-type: image/png");
      
imagepng($image);
   }
   else
   {
      print( 
"Unable to draw image!" );
   }
   
   
imagedestroy($image);
}


 
?>