<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<cfset imageFolder = "folder2">
<cfset imageDir = expandPath("./#imageFolder#")>
<cfdirectory directory="#imageDir#" name="images">
<!— make thumbs —> <cfif not directoryExists(“#imageDir#/thumbs”)> <cfdirectory action=”create” directory=”#imageDir#/thumbs”> </cfif> </code>
The variable imageFolder, simply refers to the subdirectory where I’m storing the images. The variable imageDir is a full path version of imageFolder. I need both a real full folder for ColdFusion and a relative one for my HTML later on. The cfdirectory tag simply grabs the files in the folder.
I check for a subdirectory named thumbs. If it doesn’t exist, I create it. This should only be run once. If you needed to regenerate the thumbnails for whatever reason you can simply delete the folder.
<!--- valid image? ---> <cfif isImageFile("#directory#/#name#")> <!--- check for thumbnail ---> <cfif not fileExists("#directory#/thumbs/#name#")> <cfimage action="read" source="#directory#/#name#" name="image"> <cfset imageScaleToFit(image, 125, 125)> <cfset imageWrite(image, "#directory#/thumbs/#name#",true)> </cfif> <cfoutput> <a href="#imageFolder#/#name#" title="#name#" class="thickbox" rel="gallery-ss"><img src="#imageFolder#/thumbs/#name#" alt="#name#" /></a> </cfoutput> </cfif> </cfloop> </code>
Now for the complex part (and it really isn’t that complex). I loop over the images query returned from the cfdirectory tag. For each file, I check to see if it is a valid image. If so, I then see if the thumbnail exists. If it does not, I read the image in, resize it, and save it in the thumbnails folder.
The last thing to do is output the HTML. I have to use the proper class/rel attributes for Thickbox but the only real dynamic part is the URL used for the link and image tag.
And that’s it. You can see this code in action here. I’ve also zipped the entire thing up and attached it to the blog post. Please download the Thickbox JS code yourself though.<p>Download attached file.</p>