Helical Alpha Code

pngpix: dynamic pixel generation


<?
#####################
#
# name: pngpix (pngpix.php)
# vers: 0.1a
# date: 071206
# first: 071206
# auth: "Keith Beckman" <kbeckm@alphahelical.com>
# site: http://alphahelical.com/code/misc/pngpix
# desc: pngpix was inspired by the venerable pixelgif.com. However, I like pngs
# better . . . Okay, so this works best with some .htaccess voodoo. The
# script needs to see a query-string like colour/transparency. You can do
# URIs as script.php?colour/transparency, or script/colour/transparency.
# You choose. For random colours, provide no arguments. For random colours
# with specified transparency, provide a first argument != six chars, with
# a second argument either hex (2 chars) or dec (3 chars) from 0 to 127
# for alpha channel. Just remember that Internet Exploder doesn't handle
# png alpha channels properly. I guess MS doesn't like anything that's
# free and open . . .
#
# Oh, this also requires that PHP be compiled with the GD library.
#
# todo: height/width alteration and random transparency? Easy, but I'm not sure
# I'd ever use them, and I like to avoid feature creep. If you want to do
# the first one, just be sure you alter the cache name to reflect size!
#
# Also, a cache timeout of sorts would be nice. I'll probably put that in
# v0.2a . . . it'll be necessary if you serve this to the public rather
# than keeping it for personal use.
#
# licence: pngpix is copyright 2006 Keith Beckman, and is protected by
#  the GNU GPL v 2.0 (http://www.gnu.org/licenses/gpl.txt).
#
#####################

/**** config ****/

$tempdir = '/tmp/pixelpng';

define('default_width',100);
define('default_height',100);

/** end config **/

/***** main *****/
header("Content-type: image/png");
if($_SERVER[QUERY_STRING] != '') {
 header("X-query-string: $_SERVER[QUERY_STRING]"); // here for debug, but might as well . . .
 }
else {
 header("X-query-string: none");
 }

$colour = parseq();
$pix = makePix($colour[0],$colour[1],$colour[2],$colour[3]);
header("Content-disposition: inline; filename=\"$pix[1].png\"");
echo $pix[0];
/*** end main ***/

function parseq() {
/*
  example.com/pixel/0ff00f/0f
  example.com/pixel/rnd/9a
  example.com/pixel/colour/transparency
  example.com/pixel?colour/transparency
  
  colour is any six-char string. other numbers of characters yield random colours
  
 */

 $q = strtolower(preg_replace('/^\//','',$_SERVER['QUERY_STRING']));
 $q_array = explode('/',$q);
 $colourlen = strlen($q_array[0]);
 switch(strlen($q_array[0])) {
  case 6:
   for($i=0; $i<3; $i++) {
    $colour[$i] = intval(substr($q_array[0],$i*2,2),16);
    }
   break;
  default:
   $colour = randColour();
   header("Cache-Control: no-cache");
   header("Pragma: no-cache");
  }

 switch(strlen($q_array[1])) {
  case 2:
   $colour[] = intval($q_array[1],16); // if trans is hex
   break;
  case 3:
   $colour[] = intval($q_array[1],10); // if trans is dec
   break;
  }
 return $colour;

 }

function randColour() {
 for($i=0; $i<3; $i++) {
  $colour[$i] = mt_rand(0,0xFF);
  }
 return $colour;
 }

function makePix($red,$grn,$blu,$trans=0,$width=default_width,$height=default_height) {
 global $tempdir;
 $red = $red % 0x100;
 $grn = $grn % 0x100;
 $blu = $blu % 0x100;
 $trans = $trans % 0x80;

 $name = dechex($red).'-'.dechex($grn).'-'.dechex($blu).'-'.dechex($trans);

 if(!file_exists("$tempdir/$name.png")) {
  if(!is_dir($tempdir)) {
   mkdir($tempdir);
   }
  header("X-cached-file: false");
  $pix = imagecreate($width,$height);
  $colour = imagecolorallocatealpha($pix,$red,$grn,$blu,$trans);
  imagefill($pix,0,0,$colour);
  imagesavealpha($pix,TRUE);
  imagepng($pix,"$tempdir/$name.png");
  }
 else { header("X-cached-file: true"); }

 return(array(file_get_contents("$tempdir/$name.png"),$name));
 }

?>

Generated by GNU enscript 1.6.1 and enscriptclean.

pngpix | Download Source | View Source