Harsh J

Memoirs of a QWERTY Keyboard

Rotating Header Images

8 comments

This post is another small tutorial on how you can get random rotating images for your WordPress headers or even just random images simply.

An example :

Random Image
Reload this page to see the images change below, you might need to force reload too (Usually Ctrl + F5)

Ok, now onto how you can achieve this…

First create a directory/folder on your PC of any name, I chose rotate.

Second, visit this site and get the random image code. Its by Matt (Yeah, the photomatt! or automattic!)

Here’s the code anyways:

[Start]

/*
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!
?>

[End]

So copy this code and save it in a file called rotate.php (or any filename of your choice) inside the directory we made (rotate in my case).

Now place the images you want to see rotating randomly in the rotate folder.

Finally, upload this somewhere on your webhost or any web space. And now, to use the feature, use the code http://.com/rotate/rotate.php in any image tag to watch images rotate.

Example code, from Matt's site:
A Random Image

Thats it. Now to use it in the headers, if your theme supports a remote web image header, you can point it to your rotate.php address and it'll automatically resolve it as an image address.

Else, look under the CSS of your theme and search out the line for the header image. Replace the link there with your rotate.php address.

[ratings]

Ps. If you want to use my Spider-Man images, link the images to http://www.harshj.com/rotation-example/rotate.php

Written by Harsh

March 13th, 2007 at 5:07 am

8 Responses to 'Rotating Header Images'

Subscribe to comments with RSS or TrackBack to 'Rotating Header Images'.

  1. Honestly, this is something like what I was looking for since many days – a php to change images at random on page reload.
    Thanks for the info, dude! :)

    Anurag Bhandari

    13 Mar 07 at 1:38 pm

  2. I see the same Spidey image on reload :|

    YCR

    14 Mar 07 at 12:38 pm

  3. Well, its your cache! Try a Ctrl F5!

    Harsh J

    14 Mar 07 at 4:26 pm

  4. Why not use these plugins instead ?
    http://mhough.com/wordpress/2007/header-image-rotator-plugin/
    http://pdavid.sytes.net/blog/archives/21/

    Installing and using them is less complicated.

    Pallab

    15 Mar 07 at 11:50 am

  5. Good question, but I also intended this tutorial to be useful for other purposes as well. Than just blogs!

    Harsh J

    15 Mar 07 at 4:27 pm

  6. Worked!

    But it’s still the same ol’ sipdey :| Diff Images tho :mrgreen:

    YCR

    16 Mar 07 at 1:34 pm

  7. That worked like a charm! Thanks!

    Nathan

    3 Apr 07 at 6:25 am

  8. Hello there !

    I am using Matt’s great plugin allowing to automatically changer the header image. I have added it to a new install of WP 2.1.3

    I have noticed just a little bug which I cannot to manage to solve : if you use images in the header-images like the default kubrickheader.jpg image, then that header image seems to be very slightly shifted by 1 pixel on the right, thus not falling right on top of the body.

    Would you have any ideas on solving that ?

    [To see what happens, just put kubrickheader.jpg in the header-images folder.]

    Thanks a lot !

    seb

    21 Apr 07 at 12:34 pm

Leave a Reply