Skip to content

Instantly share code, notes, and snippets.

@stefandz
Created June 4, 2014 11:06
Show Gist options
  • Select an option

  • Save stefandz/2029d38266fb1d47b3cd to your computer and use it in GitHub Desktop.

Select an option

Save stefandz/2029d38266fb1d47b3cd to your computer and use it in GitHub Desktop.
<?php
/*
Image based on overlapping circle arcs
SDDS 1st Jun 2014
*/
// draw parameters
define('NUM_RINGS', '8');
define('BASE_ALPHA', '120'); // 0 is opaque, 127 is transparent
define('IMG_WIDTH', '500');
define('IMG_HEIGHT', '500');
// top left, bottom left, bottom right, top right
$points = array(array(0,0), array(0,IMG_HEIGHT), array(IMG_WIDTH,IMG_HEIGHT), array(IMG_WIDTH,0));
// helper functions
function HuetoRGB( $v1, $v2, $vH )
{
if ( $vH < 0 )
{
$vH += 1;
}
if ( $vH > 1 )
{
$vH -= 1;
}
if ( ( 6 * $vH ) < 1 )
{
return ( $v1 + ( $v2 - $v1 ) * 6 * $vH );
}
if ( ( 2 * $vH ) < 1 )
{
return ( $v2 );
}
if ( ( 3 * $vH ) < 2 )
{
return ( $v1 + ( $v2 - $v1 ) * ( ( 2 / 3 ) - $vH ) * 6 );
}
return ( $v1 );
}
function HSLtoRGB ( $H, $S, $L )
{
if ( $S == 0 )
{
$R = $L * 255;
$G = $L * 255;
$B = $L * 255;
}
else
{
if ( $L < 0.5 )
{
$var_2 = $L * ( 1 + $S );
}
else
{
$var_2 = ( $L + $S ) - ( $S * $L );
}
$var_1 = 2 * $L - $var_2;
$R = 255 * HuetoRGB( $var_1, $var_2, $H + ( 1 / 3 ) );
$G = 255 * HuetoRGB( $var_1, $var_2, $H );
$B = 255 * HuetoRGB( $var_1, $var_2, $H - ( 1 / 3 ) );
}
return array( $R, $G, $B );
}
// create a white image with alpha, blending and AA
$img = imagecreatetruecolor( IMG_WIDTH, IMG_HEIGHT );
$background = imagecolorallocate( $img, 255, 255, 255);
imagefill($img, 0, 0, $background);
imagealphablending( $img, true );
imagesavealpha($img, true);
imageantialias($img, true);
// for each point...
foreach ($points as $point) {
// random hue (0..1), then get RGB value of that and assign to a colour
$hue = mt_rand() / mt_getrandmax();
list($redVal, $greenVal, $blueVal) = HSLtoRGB($hue, 1.0, 0.5);
$colour = imagecolorallocatealpha($img, $redVal, $greenVal, $blueVal, BASE_ALPHA);
// draw several overlapping circle of increasing radius from this point
for ($i = 1; $i <= NUM_RINGS; $i++) {
imagefilledellipse($img, $point[0], $point[1], 2*$i*IMG_WIDTH/NUM_RINGS, 2*$i*IMG_HEIGHT/NUM_RINGS, $colour);
}
}
// output the image as a PNG
header( "Content-type: image/png" );
imagepng( $img );
imagedestroy( $img );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment