Skip to content

Instantly share code, notes, and snippets.

@pieman72
Created July 2, 2014 10:15
Show Gist options
  • Save pieman72/e05c74ca88e9209d91be to your computer and use it in GitHub Desktop.
Save pieman72/e05c74ca88e9209d91be to your computer and use it in GitHub Desktop.
E-Sheep! They're what computers dream about.
<?// Generate a random image based on a key
$key = sha1(isset($_GET['s']) ? $_GET['s'] : rand());
$WIDTH = isset($_GET['w']) ? $_GET['w'] : 150;
$HEIGHT = isset($_GET['h']) ? $_GET['h'] : 150;
// Globals
$MAX_SAT_LOSS = 0.4; // 0 - 1.0
$MAX_LUM_SKEW = 0.25; // 0 - 0.5
$COLORS = array();
$IMG = imagecreatetruecolor($WIDTH, $HEIGHT);
if(function_exists('imageantialias')) imageantialias($IMG, true);
// Generate a color from 2-digit hex HSL values
function generateHSL($H, $S, $L){ global $MAX_SAT_LOSS, $MAX_LUM_SKEW, $COLORS, $IMG;
// Generate the base color values from the hue
$hue = $H;
if($hue<43){
$r = 255;
$g = 0 + (($hue-0)*255.0/43);
$b = 0;
}else if($hue<85){
$r = 255 - (($hue-43)*255.0/42);
$g = 255;
$b = 0;
}else if($hue<128){
$r = 0;
$g = 255;
$b = 0 + (($hue-85)*255.0/43);
}else if($hue<171){
$r = 0;
$g = 255 - (($hue-128)*255.0/43);
$b = 255;
}else if($hue<213){
$r = 0 + (($hue-171)*255.0/42);
$g = 0;
$b = 255;
}else{
$r = 255;
$g = 0;
$b = 255 - (($hue-213)*255.0/43);
}
// Scale the base color values by the saturation
$sat = 1.0 - $MAX_SAT_LOSS + $S*$MAX_SAT_LOSS/255;
$r = 128 + ($r-128)*$sat;
$g = 128 + ($g-128)*$sat;
$b = 128 + ($b-128)*$sat;
// Scale the base color values by the luminosity
$lum = 0.5 - $MAX_LUM_SKEW + $L*$MAX_LUM_SKEW*2/255;
$r = ($lum<0.5) ? $r*$lum/0.5 : $r+(255-$r)*($lum-0.5)/0.5;
$g = ($lum<0.5) ? $g*$lum/0.5 : $g+(255-$g)*($lum-0.5)/0.5;
$b = ($lum<0.5) ? $b*$lum/0.5 : $b+(255-$b)*($lum-0.5)/0.5;
// Create the color
$COLORS[] = imagecolorallocate($IMG, $r, $g, $b);
}
// Draw a random shape on the image given one byte of randomness
function drawShape($data, $index){ global $WIDTH, $HEIGHT, $COLORS, $IMG;
// 1 1 1 1 1 1 1 1
// -color- -sh- -th- f
$color = $COLORS[($data>>5) + 1];
$shape = ($data>>3)%4;
$thick = ($data>>1)%4 + 2;
$fill = (bool)($data%2);
$x = round(((($data*$index)%256)%16)*$WIDTH / 15.0);
$y = round(((($data*$index)%256)>>4)*$HEIGHT / 15.0);
$angle = (($data*$index)%256)*2.0*M_PI/255.0;
$size = (($data*$index)%256)*30.0/255.0 + 20;
// Depending on the shape, do different drawing things
imagesetthickness($IMG, $thick);
switch($shape){
case 0: // Line
$rise = sin($angle)*($WIDTH+$HEIGHT);
$run = cos($angle)*($WIDTH+$HEIGHT);
imageline($IMG, $x-$run, $y-$rise, $x+$run, $y+$rise, $color);
break;
case 1: // Square
$rise = sin($angle)*$size;
$run = cos($angle)*$size;
$points = array(
$x-$run, $y-$rise
,$x+$rise, $y-$run
,$x+$run, $y+$rise
,$x-$rise, $y+$run
);
if($fill){
imagefilledpolygon($IMG, $points, 4, $color);
}else{
imagepolygon($IMG, $points, 4, $color);
}
break;
case 2: // Circle
if($fill){
imagefilledellipse($IMG, $x, $y, $size, $size, $color);
}else{
imageellipse($IMG, $x, $y, $size, $size, $color);
}
break;
case 3: // Triangle
$points = array();
for($a=0; $a<3; ++$a){
$angle += 2.0*M_PI/3;
$rise = sin($angle)*$size;
$run = cos($angle)*$size;
$points[] = $x-$run;
$points[] = $y-$rise;
}
if($fill){
imagefilledpolygon($IMG, $points, 3, $color);
}else{
imagepolygon($IMG, $points, 3, $color);
}
break;
}
}
// Pick colors based on triad scheme
$h = hexdec(substr($key, 0, 2));
$s = hexdec(substr($key, 2, 2));
$l = hexdec(substr($key, 4, 2));
for($a=0; $a<3; ++$a){
for($b=0; $b<3; ++$b){
generateHSL($h, $s, ($l+70*(($b+1)%3-1))%256);
}
$h = ($h+85)%256;
}
// Fill with background color
imagefilledrectangle($IMG, 0, 0, $WIDTH, $HEIGHT, $COLORS[0]);
// Draw 17 shapes
for($a=0; $a<17; ++$a){
drawShape(hexdec(substr($key, $a*2+6, 2)), $a);
}
// Output the image contents
header('Last-Modified: '.date('r', strtotime('-100 years')));
header('Cache-Control: max-age=315360000');
header('Expires: '.date('r', strtotime('+10 years')));
header('Content-Type: image/png');
imagepng($IMG, null, 9);
imagedestroy($IMG);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment