Saturday 04 Jul 2009   
Add Comment

Random Images Tutorial

ColdFusion Tutorial #25

Have you ever wanted to simply pull a single random image from a specified folder on your site? This useful bit of code is just the thing.

demo.cfm

First, we use <cfdirectory> to list the images in the folder.
Then we pull out a single image by picking a random number, from 1 to the number of images found, and outputting that row of the cfdirectory.

In this example, the file name (without the extension) is used as the alt tag. You could easily substitute a fixed value, such as the name of your site, or add a prefix to the filename alt tag like "Photo of: #name#" .

To see all of the infomation gathered by the cfdirectory query, put this in your page after line 5 <cfdump var="#getPics#">.

With CF8, you could also get the dimensions of the image to create proper height/width attributes for the <img> tag.

 
 


Demo

See this code running!


Download

Download this code as a zip!


Comments

fhghfgh
rgh @ Tuesday 30 Jun 2009 - 11:39:56 PM

Click button to add a comment


Author

Michael Evangelista


Published

Tuesday 22 Jan 2008

Original

This tutorial has been modified and published with permission of the author. The original tutorial can be found here
http://www.miuaiga.com/index.cfm/2007/11/30/Show-a-random-image-with-cfdirectory 

Tags

cfdirectory  cfif  cfoutput  cfset