Rotating Banner for BlogCFC - Hack Warning!
Posted At : June 18, 2008 11:15 AM | Posted By : Bob Silverberg
Related Categories: ColdFusion, BlogCFC
So after making my site look pretty, and creating my banner image, I decided that I'd like to create a few images and have them rotate randomly on my blog. The first complication I discovered is that all of the pages are cached by BlogCFC, so simply introducing code into layout.cfm to rotate the image wouldn't work.
Having spent a few hours last night messing with BlogCFC code, I didn't really want to delve into it again to figure out a way around the caching issue, so I came up with a solution that is probably the "hackiest" thing I've done in ages.
I set up a scheduled task in CF that will randomly select one of 5 images that I've created for banner and then copy that image overtop of an image called MyBanner.jpg. MyBanner.jpg is what my img tag points to in layout.cfm. I'm running that task hourly, so now, even though the page stays cached, the image rotates randomly every hour.
Here's the code for RotateBanner.cfm:
2<cfset ImageList = "chile_banner_2,chile_banner_3,chile_banner_5,chile_banner_6,chile_banner_7" />
3<cffile action="copy" source="#dir##ListGetAt(ImageList,RandRange(1,ListLen(ImageList)))#.jpg" destination="#dir#MyBanner.jpg">
I'm not particularly proud of the code, but hey, I got the job done in about 10 minutes.
http://www.javascriptworld.com/chap4-7.html
i did something similar (was gonna blog it, but you beat me to it) :)
for the image on top of my blog, i specify:
background:#FFFFFF url(../images/randomPhoto.cfm) repeat scroll 0% 0%;
randomPhoto.cfm looks like this:
<cfscript>
photosArray = arrayNew(1);
photosArray[1] = "logo_blue_photo.jpg";
photosArray[2] = "logo_blue_photo_2.jpg";
photosArray[3] = "logo_blue_photo_3.jpg";
photosArray[4] = "logo_blue_photo_4.jpg";
randIdx = randRange(1, arrayLen(photosArray));
</cfscript>
<cfheader name="content-disposition" value="inline; filename=#photosArray[randIdx]#" />
<cfcontent type="image/jpeg" file="#expandPath('#photosArray[randIdx]#')#" reset="no" />
it's a bit more overhead as the calculations are done at runtime, but it's not like anyone reads my blog anyway :D
Thanks!