<?php
/*
Author: Josiah McClurg, 2008
Website: http://www.jcmcclurg.com
Blog: http://www.josiahland.com
Favorite comic: http://www.qwantz.com
Sorry about the lack of comments. Perhaps I will correct the problem if enough interest is indicated in my doing so.
As usual, no copyright. Freely distribute, please.
*/
header("Content-type: image/png");
$width = 256;
$height = 256;
$seed = (is_numeric($_GET['s']) && $_GET['s'] != 0) ? srand(intval($_GET['s'])) : 0;
$angle = (is_numeric($_GET['a']))? toInt($_GET['a']) : rand(0,359);
$x = (is_numeric($_GET['x']) && toInt($_GET['x']) < $width && toInt($_GET['x'] >= 0))? $_GET['x'] : rand(0,$width - 1);
$y = (is_numeric($_GET['y']) && toInt($_GET['y']) < $height && toInt($_GET['y'] >= 0))? $_GET['y'] : rand(0, $height - 1);
$c1 = (is_numeric($_GET['c1']))? toInt($_GET['c1']) : 0xFF0000;
$c2 = (is_numeric($_GET['c2']))? toInt($_GET['c2']) : 0x0000FF;
function toInt($int)
{
if(strtolower(substr($int,0,2)) == "0x")
{
return(hexdec($int));
}
else
{
return(intval($int));
}
}
function isGrey($color)
{
$r = ($color >> 16) & 0xFF;
$g = ($color >> 8) & 0xFF;
$b = ($color) & 0xFF;
if(abs($r - $g) < 15 && abs($r - $b) < 15 && $r <= 0x7f)
{
return true;
}
else
{
return false;
}
}
// Returns the next free position of the path going
// in angle direction, starting at x, y
function findFreePath($im, $angle, $x, $y, $maxWidth, $maxHeight)
{
$xInc = sqrt(2)*cos($angle);
$yInc = sqrt(2)*sin($angle);
$newX = $x;
$newY = $y;
$endX = $newX;
$endY = $newY;
$ox = $endX;
$oy = $endY;
$color = 0xFFFFFF;
$isGrey = false;
while($newX < $maxWidth && $newY < $maxHeight && $newX >= 0 && $newY >= 0 && !$isGrey)
{
$newX += $xInc;
$newY += $yInc;
if(round($newX) != $endX || round($newY) != $endY)
{
$endY = round($newY);
$endX = round($newX);
$oy = $endY;
$ox = $endX;
// look ahead 3px because of antialiasing
for($i = 0; $i < 3 && !$isGrey; $i++)
{
$oy += $yInc;
$ox += $xInc;
$color = imagecolorat($im,round($ox),round($oy));
$isGrey = isGrey($color);
}
if($isGrey)
{
$endY = round($oy);
$endX = round($ox);
}
}
}
return(array($endX, $endY));
}
function fixPoly(&$poly)
{
$len = count($poly);
$offset = 0;
$lastSlope = NULL;
$f = false;
for($i = 4; $i < $len - 8; $i += 2)
{
$dx = $poly[$i - $offset + 2] - $poly[$i - $offset];
$dy = $poly[$i - $offset + 3] - $poly[$i - $offset + 1];
if($dx == 0)
{
if($f)
{
array_splice($poly, $i - $offset,2);
$offset += 2;
}
else
{
$f = true;
}
}
elseif((abs($lastSlope - $dy/$dx) < 1))
{
array_splice($poly, $i - $offset,2);
$offset += 2;
}
else
{
$lastSlope = $dy/$dx;
}
}
}
function polygon($im,$poly,$n,$c)
{
$l = count($poly);
for($i = 0; $i < $l; $i += 2)
{
imageellipse($im,$poly[$i],$poly[$i + 1],5,5,$c);
}
imagefilledpolygon($im,$poly,$n,$c);
}
function fadeFill($im, $angle, $startX, $startY, $maxWidth, $maxHeight, $endX, $endY,$color1,$color2)
{
$perpAngle1 = $angle + pi()/2;
$perpAngle2 = $angle - pi()/2;
$startX = round($startX);
$startY = round($startY);
$endX = round($endX);
$endY = round($endY);
$xInc = sqrt(2)*cos($angle)*4;
$yInc = sqrt(2)*sin($angle)*4;
$currentX = $startX;
$currentY = $startY;
$oldX = -1;
$oldY = -1;
sqrt(pow(($endX - $endY),2) + pow(($endY - $startY),2));
$poly1 = array($startX,$startY);
$poly2 = array($startX,$startY);
while(abs($currentX - $endX) > 5 || abs($currentY - $endY) > 5)
{
$currentX += $xInc;
$currentY += $yInc;
if(abs($currentX -$oldX) >= 1 || abs($currentY -$oldY) >= 1)
{
list($endX1, $endY1) = findFreePath($im,$perpAngle1,$currentX,$currentY,$maxWidth,$maxHeight);
$poly1[] = $endX1;
$poly1[] = $endY1;
list($endX2, $endY2) = findFreePath($im,$perpAngle2,$currentX,$currentY,$maxWidth,$maxHeight);
$poly2[] = $endX2;
$poly2[] = $endY2;
$oldX = $currentX;
$oldY = $currentY;
}
}
$poly1[] = $endX;
$poly1[] = $endY;
fixPoly($poly1);
$poly2[] = $endX;
$poly2[] = $endY;
fixPoly($poly2);
$len1 = count($poly1)/2;
$len2 = count($poly2)/2;
if($len1 >= 3) imagefilledpolygon($im,$poly1,count($poly1)/2,$color1);
if($len2 >= 3) imagefilledpolygon($im,$poly2,count($poly2)/2,$color2);
}
function lines($im, $angle, $x,$y, $maxWidth, $maxHeight,$black,$color1,$color2)
{
list($endX, $endY) = findFreePath($im, $angle, $x, $y, $maxWidth, $maxHeight);
if($endY != $y || $endX != $x)
{
imageline($im,$x,$y,$endX,$endY,$black);
fadeFill($im, $angle, $x, $y, $maxWidth, $maxHeight, $endX, $endY,$color1,$color2);
$newX = (rand(0,256)*($endX - $x))/256;
$newY = $newX*tan($angle);
$newX = $x + $newX;
$newY = $y + $newY;
lines($im,deg2rad(rand(0,355)), $newX, $newY, $maxWidth,$maxHeight,$black,$color1,$color2);
}
}
/*//$string = $_GET['text'];
$string =
"Look, I'm overlaying all kinds
of text on this Christmas tree.
Turns out, this text is DYNAMIC!
Don't believe me? Here's a
totally random number: ".rand(0,100)."
Still not convinced? Here's the
time at which you loaded this
page:
".date("r");
$im = imagecreatefrompng("button1.png");
$orange = imagecolorallocate($im, 110, 100, 30);
$s = preg_split("/[\r\n]/",$string);
$l = count($s);
for($i = 0; $i < $l; $i++)
{
imagestring($im, 3, 3, 15*($i + 1), $s[$i], $orange);
}*/
// Create the image
$im = imagecreatetruecolor($width,$height);
// Turn antialiasing on
//imagealphablending($im,false);
imageantialias($im,true);
$white = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
$r = ($c1 >> 16) & 0xFF;
$g = ($c1 >> 8) & 0xFF;
$b = ($c1) & 0xFF;
$color1 = imagecolorallocatealpha($im,$r,$g,$b,110);
$r = ($c2 >> 16) & 0xFF;
$g = ($c2 >> 8) & 0xFF;
$b = ($c2) & 0xFF;
$color2 = imagecolorallocatealpha($im,$r,$g,$b,110);
// Set the background
imagefill($im,0,0,$white);
/*$yellow_x = 100;
$yellow_y = 75;
$red_x = 120;
$red_y = 165;
$blue_x = 187;
$blue_y = 125;
$radius = 150;
// allocate colors with alpha values
$yellow = imagecolorallocatealpha($im, 255, 255, 0, 75);
$red = imagecolorallocatealpha($im, 255, 0, 0, 75);
$blue = imagecolorallocatealpha($im, 0, 0, 255, 75);
// drawing 3 overlapped circle
imagefilledellipse($im, $yellow_x, $yellow_y, $radius, $radius, $yellow);
imagefilledellipse($im, $red_x, $red_y, $radius, $radius, $red);
imagefilledellipse($im, $blue_x, $blue_y, $radius, $radius, $blue);
imagefilledpolygon($im,array(0,0,0,0,256,256,256,256),4,$blue);*/
//fadeFill($im, 2*pi() - deg2Rad(-80), 128, 0, $width, $height, 128, 256);
//fadeFill($im, 2*pi() - deg2Rad(-95), 128, 0, $width, $height, 128, 256);
lines($im, 2* pi() - deg2Rad($angle), $x, $height - $y, $width, $height,$black,$color1,$color2);
//imagestring($im, 3, 3, 15, "Hello", $color1);
//imagestring($im, 3, 3, 30, "Hello", $color2);
//lines($im, 2*pi() - deg2Rad(-90), 128, 0, $width, $height);
imagepng($im);
imagedestroy($im);
?>