Random Image Script

Released on May 10, 2003
Last Updated: May 10, 2003 3:44 AM
Version: 1.1

Description

This is your standard random image script that takes a slightly different approach than most, namely using standard HTTP headers instead of reading the file through the script.

Installation/Usage

For the simplest possible installation, just drop this code in a directory with the images you want to rotate, and call it like you would a normal image, for example:

<img src="/dropbox/2003/rotate/rotate.php" alt="A Random Image" />

A Random Image

Code

A Rotator Apart

<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:
http://photomatt.net/scripts/randomimage
*/
// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

Notes

For additional information about the thinking behind this script, see A Better Image Rotator.

42 replies on “Random Image Script”

  1. Nice code, thanks.

    I suggest you put the next two lines just before the last line:
    header(“Cache-Control: no-cache, must-revalidate”); // HTTP/1.1
    header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);

    This will prevent the browser and/or the cache-server to cache the images. Otherwise the randomness is pretty poor.

  2. It works, but as Ondra said, the randomness is not necessarily all that.

    However, I inserted those two lines of code, and it didn’t fix it. In fact, my page reverted to the default Kubrick header image when I did that.

    When I took those lines out, it went back to one of my images that’s in the same directory as the script. But it’s not really rotating at all.

  3. Guys, some browser hold on to the cache. You may need to do a force reload. (Ctrl + F5)

    Your visitors aren’t going to be sitting there reloading your site, they’re just going to see something different each time they come.

  4. Yeah, I just noticed that my problem seems to be a Firefox issue. (I’m running Firefox 1.5.0.1 under Mac OSX 10.4.6. Firefox has other “issues” as well.)

    It works just fine in Safari. And, as Matt says, a header image is just a header image. They aren’t going to be sitting there clicking all day, like we do!

  5. Hello,

    i searched very long for such a random script like this. It works perfectly! Thank You

  6. Club webpage was boring and static… now with a random pic loading, it gives it more pazazz and gives it that “professional” touch that looks oh-so nice… Thanks Matt, your code did the trick!

  7. Wow! Thank you. That script rocks. I took a class in C a long time ago, and trying to get back into the swing of things with this webpage. I have to admit seeing that you closed the open file is clean up that’s uncommon to see with people scripting stuff. Good clean code!

  8. I figured out how to get it to resolve on FireFox. You can’t let it resolve the path for you, you have to hard code the full file path with the specific directory to the rotate.php in the calling code’s img src.

  9. Thanks for the script! I have gone through multiple ones with multiple variables needing multiple configuration changes. Someone should delete every other random image script from the Internet.

  10. Thanks for the script, it works great on my blog. I eventually figured out that the forced-reload (CTRL-F5) was necessary to test the randomness, and I put the image-fetching script inside of an un-ordered list tag to scoot it a bit to the right so it doesn’t crowd the left edge of my navigation column. If only I could associate a link with each image, that would enable site visitors to click on an image that would take them to a specific section or page of my website. Would that even be possible, do you think?

  11. Thank you so much Matt!! I was looking for ages to find a decent script that would work with my current PHP scripts and this was it!!

    Thanks!

  12. Matt, I keep finding myself back at your site lately…
    Was about to build something like this but you beat me to it, excellent.
    I chucked it into Drupal’s Kubrick theme port and it works great on sawtell-holiday.com

    thx

Comments are closed.