Random image display with PHP

So I wrote the following piece of code to display the random image above, mainly so I could show off my photos without having to create a photo gallery thingy. It is pretty straightforward and works fairly well.

The code, with comments:

< ?
//INITIALIZE RANDOM GENERATOR
srand((float) microtime() *10000000);

//IMAGES TO BE DISPLAYED
$imagefiles = array(“image1.jpg”,”image2.jpg”,”image3.jpg”);

//GRAB A RANDOM FILENAME FROM THE ARRAY
$imagefiles = $imagefiles[rand(0,count($imagefiles)-1)];

//PATH WHERE THE IMAGES ARE STORED ON OUR WEB SERVER
$path = “/home/user/www/” . $imagefiles;

//TELL THE BROWSER WE ARE SENDING AN IMAGE
header(“Content-type: image/jpeg”);

//OPEN THE IMAGEFILE
$jpeg = fopen($path,”r”);

//READ THE IMAGEFILE
$image = fread($jpeg,filesize($path));

//SEND IT DOWN THE PIPE
echo $image;
?>

Voila! Place the code in a seperate file such as image.php, then call the image in your page using the img tag like this:
<img src=image.php>

Good luck.

Thanks for reading. Share on Google+Tweet about this on TwitterShare on FacebookEmail this to someone