Image Rotator Script

I've finally gotten around to adding an image rotator script for the top header of this blog to rotate through a few different images. I'll be adding more once I've downloaded them off my mobile phone. For anyone who's interested in the PHP code:

$images = array(
   'paris-subway.jpg' => 'The Metro : Paris',
   'cube-houses.jpg' => 'Cube Houses : Rotterdam',
   'snow-trees.jpg' => 'Snow covered trees : Austria',
   'mountain-sunset.jpg' => 'Sunset over the mountains : Austria',
   'paris-sunset.jpg' => 'Sunset on the River : Paris',
   'wales-sunset.jpg' => 'Welsh Sunset'
);
// now shuffle the array items randomly
$filenames = array_keys($images);
shuffle($filenames);
// and pull out 3 images stored in the variables $image0, $image1 and $image2
for ($i = 0; $i < 3; $i++) {
   $thumb = "thumb".$i;
   $altxt = "altxt".$i;
   $$thumb = $filenames[$i];
   $$altxt = $images[$filenames[$i]];
}

Then all you need to do is reference these image variables whereever you want them i.e.

<img src="pics/<?php print $image0; ?>" alt="<?php print $altxt0; ?>" />

Thanks to JasonD on DDN for reminding me about the shuffle() function!

No Responses to “Image Rotator Script”

Leave a Comment

Please use your real name or nickname. Spam names will be changed.