Random background image script
Saturday, September 27th, 2008Well, I was trying to make an automatic backgrounc color script for a webpage that randomly changed. This is what I came up with.
You can view the site at http://tranquilpenguin.com/domain.php
The code is:
<?php
header(”Content-type: image/png”);
$q=rand(1,3);
$a=0;
$b=rand(0,254);
$c=rand(0,254);
$im=@imagecreatetruecolor(1, 2000) or die(”Cannot Initialize new GD image stream”);
$background_color = imagecolorallocate($im, 0, 0, 0);
$color=imagecolorallocate($im,$b,$c,0);
imageline($im, 0, 0, 1, 0, $color);
for ($i=0;$i<200;$i++) {
if ($q==1) {$color=imagecolorallocate($im,$b,$c,$a);}
if ($q==2) {$color=imagecolorallocate($im,$a,$b,$c);}
if ($q==3) {$color=imagecolorallocate($im,$c,$a,$b);}
imageline($im, 0, $i, 10, $i, $color);
if ($i <= 254) {$a++;} else{$a=$a-1;}
}
$z=$i;
while($z<2000){
imageline($im, 0, $z, 10, $z, $color);
$z++;
}
$color=imagecolorallocate($im,$b,$c,0);
imageline($im, 0, 2000, 10, 2000, $color);
imagepng($im);
imagedestroy($im);
?>
In the CSS of the main page just call the background like this:
body{
background: url(http://YOURDOMAIN.com/colors2.php);
background-repeat:repeat-x;
}
Feel free to tell me a better way to do it.