I was tasked a few months ago to rotate an image on a site randomly. Ordinarily this would not be an issue but the image was a css background image and I really didn’t have the ambition to rewrite the css and page html to do this. Thankfully my laziness paid off, because I found a solution while looking through some php code for wordpress.

I ran into this template that I liked, but wanted to change the header image. I changed it, uploaded it and refreshed the page, but NOTHING changed. I spent a good 5 minutes on this before checking the css to see what was going on. Instead of finding a path to an image in the css for the background, I found a path to a file named rotate.php. The script was written by Matt Mullenweg (http://photomatt.net/scripts/randomimage) and seemed simple enough to me. What I liked about this script is that it didn’t require managing a bunch of images. If you want an image you simply stick it in a predefined directory and point to that directory.

I decided to rewrite this in coldfusion and within a few minutes I had a working. It is rather simple. Define your folder, define your extensions and define the url path to the folder and you are done. You will find the code below.