Name: 2009-08-18 10:35
Output a thousand Sussmen in an individualistic way.
<?php
$message = str_repeat('Sussman', 1000);
define('GRID_W', 80);
define('GRID_H', 24);
define('BORDER', 4);
define('HUE_OFFSET', mt_rand(0, 1000) / 1000);
define('HUE_MUL', 10);
define('LIGHTNESS_POWER', 3);
function hsl_to_rgb($h, $s, $l) {
if($s == 0) {
$color = array('r' => $l, 'g' => $l, 'b' => $l);
} else {
$t2 = ($l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s);
$t1 = 2 * $l - $t2;
$color = array('r' => $h + 1/3, 'g' => $h, 'b' => $h - 1/3);
foreach($color as $k => $t3) {
if($t3 < 0) $t3 += 1;
if($t3 > 1) $t3 -= 1;
if(6 * $t3 < 1)
$color[$k] = $t1 + ($t2 - $t1) * 6 * $t3;
elseif(2 * $t3 < 1)
$color[$k] = $t2;
elseif(3 * $t3 < 2)
$color[$k] = $t1 + ($t2 - $t1) * (2/3 - $t3) * 6;
else
$color[$k] = $t1;
}
}
return $color;
}
define('NORTH', 1);
define('EAST', 2);
define('SOUTH', 3);
define('WEST', 4);
define('FONT', 2);
define('FONT_W', 6);
define('FONT_H', 11);
define('X0', 0);
define('Y0', -3);
define('LEN', strlen($message));
$opposites = array(NORTH => SOUTH, SOUTH => NORTH, EAST => WEST, WEST => EAST);
$movements_x = array(NORTH => 0, EAST => 1, SOUTH => 0, WEST => -1);
$movements_y = array(NORTH => -1, EAST => 0, SOUTH => 1, WEST => 0);
$img = imagecreatetruecolor(2 * BORDER + GRID_W * FONT_W, 2 * BORDER + GRID_H * FONT_H);
$x = mt_rand(0, GRID_W - 1);
$y = mt_rand(0, GRID_H - 1);
$from = 0;
for($i = 0; $i < LEN; $i++) {
$pos = $i / LEN;
$rgb = hsl_to_rgb(fmod((HUE_OFFSET + $pos) * HUE_MUL, 1), 1, pow($pos, LIGHTNESS_POWER));
$color = imagecolorallocate($img, round($rgb['r'] * 255), round($rgb['g'] * 255), round($rgb['b'] * 255));
imagestring($img, FONT, BORDER + X0 + $x * FONT_W, BORDER + Y0 + $y * FONT_H, $message[$i], $color);
imagecolordeallocate($img, $color);
$valid_directions = array(NORTH, EAST, SOUTH, WEST);
$invalid_directions = array($from);
if($x == 0) $invalid_directions[] = WEST;
elseif($x == GRID_W - 1) $invalid_directions[] = EAST;
if($y == 0) $invalid_directions[] = NORTH;
elseif($y == GRID_H - 1) $invalid_directions[] = SOUTH;
$valid_directions = array_diff($valid_directions, $invalid_directions);
$direction = $valid_directions[array_rand($valid_directions)];
$x += $movements_x[$direction];
$y += $movements_y[$direction];
$from = $opposites[$direction];
}
header('Content-type: image/png');
imagepng($img);
?>