Random Image Header in PHP

Want to give your readers something a little different every time they visit your blog? Here’s a nice little gimmick for adding a some variety to the top of your website: a Random Image Header.

With a simple bit of code, you can have an image in your webpage header (or anywhere else for that matter) that changes whenever someone views your site. Just press F5 and look at the top of this page a couple times if you want to see what I’m talking about in action. With PHP, it’s very easy to search through a directory of images and pick a random one for display when someone loads a page.

Here’s the code that displays the image at the top of this site:

<img src="<?php GetRandomImage("./images/headers/"); ?>" />

Just a regular old IMG tag, except for the mysterious PHP function providing the source for the image. As you might be able guess from looking at it, GetRandomImage() searches through all the images in “./images/headers/” and randomly picks one to be returned as a path for the IMG tag. So let’s take a look inside the function and see what’s really going on:

function GetRandomImage($header_dir)
  $file_list = glob( $header_dir . "*.jpg" );
  if( count($file_list) == 0 )
    echo "./images/default_header.jpg";
    $random_index = rand( 0, count($file_list) - 1 );
    echo $file_list[$random_index];

Nothing too complicated. The heavy-lifting of searching through the image directory is taken care of by the glob() call in the first line of the function. For simplicity’s sake, I limited the search to jpg files by appending “*.jpg” to the string I passed into glob(). If I wanted to add support for gifs or pngs, I’d have to be a little more creative with the matching pattern I give to glob(), but not too much. The glob() function returns an array of filenames which I stuff into the $file_list variable.

In case there are no jpgs in the header images directory, I check the size of $file_list with the count() function and return the path to a default header image if it is zero. This is something I might consider making a parameter to the GetRandomImage() function in case I want to be able to pick my fall-back image instead of relying on a single hard-coded value.

So, now that glob() has provided me with an array of paths to images in my headers it’s just a simple matter of choosing one. The call to rand( 0, count($file_list) - 1 ) should do the trick and provide me with a number between 0 and the size of the $file_list array minus one (inclusive). The minus one in the function call is necessary to make sure that we don’t get an index that runs off the end of the $file_list array. Arrays in PHP are indexed from 0, so trying to access $file_list[4] when count($file_list) is equal to 4 will return nothing useful. Note that if your server is running a version of PHP earlier than 4.2.0, you may need to seed your random number generator with srand() before calling rand(). The most common convention is to seed it using the current time by calling srand( time() ).

That’s pretty much it. Now you just print out the path in $file_list at the random index you selected and you’re all set. You can just keep adding images to your header directory and they’ll keep being displayed in a random rotation whenever someone visits your site.

Check here for more articles in this series: Web Development
  • Language

  • Sponsored Links

  • Rob's Portfolio

    Games Writing Contributions

No comments yet.

Leave a comment