Random Futurama Quote with Character (Rated 5)Description:
Display a random script from the Hit TV show futurama in your signature!!
This script creates the random character image, and a string as an image from a text file.
GD Image library needs to be installed on the server you intend to host this on. Code starts here
<?PHP
// This is going to output the image as a PNG
header ("Content-type: image/png");
// Randomise seed
srand((float)microtime()*10000000);
// Random image function
function random_image($dir = './')
{
$allowed_types = array('gif');
if(!$dh = @opendir($dir))
die('Unable to open directory.');
else
{
while(($file = readdir($dh)) !== false)
{
if($file == '.' || $file == '..')
continue;
// check extension
list($name,$ext) = explode('.',$file);
if(!in_array(strtolower($ext),$allowed_types))
continue;
else
$images[] = $file;
}
$key = array_rand($images);
return $dir . '/' . $images[$key];
}
}
$image = random_image();
// get contents of a file into a string
$filename = "quotes.txt";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
$quotes = explode("<hr>",$contents) ;
$key = rand(0,count($quotes)-1) ;
$lines = explode("\n",$quotes[$key]) ;
$largestLine = 0 ;
$count = count($lines) ;
for ($i = 0 ; $i < $count ; $i++)
{
if (strlen($lines[$i]) > $largestLine)
$largestLine = strlen($lines[$i]) ;
}
$character = getImageSize($image);
$pxTMP = ($largestLine * 6.5);
$px = $pxTMP + + $character[0] ;
$py = (($count+1) * 15);
if ($py < $character[1])
$py = $character[1] ;
$fullImage = @imagecreate($px,$py);
$characterImage = @imagecreatefromgif($image);
imagecopyresized($fullImage,$characterImage,0,0,0,0,$character[0],$character[1],$character[0],$character[1]);
$im = @imagecreate($px, $py) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$trans=imagecolortransparent($im,$background_color);
$text_color = imagecolorallocate ($im, 0, 0, 0);
for ($i = 0 ; $i < count($lines) ; $i++)
{
imagestring ($im, 2, 5, ($i+1)*15, $lines[$i], $text_color) ;
}
imagecopyresized($fullImage,$im,$character[0],0,0,0,$pxTMP,$py,$pxTMP,$character[1]);
$trans=imagecolortransparent($fullImage,$background_color);
imagepng ($fullImage);
?>
See it working here http://www.devscripts.net/futurama/siggy.php
Last Edited: August 29, 2003, 10:45 pm
Submitted by Devscripts on 29-08-2003 22:45 |