Posted in jQuery, ColdFusion | Posted on 02-17-2009 | 20,463 views
Ok, is it just me, or has JavaScript spoiled me? It seems like almost daily I come across a web page with a long list of image thumbnails. I click on one and the entire page reloads. I click back and get a whole other large page load. I click on another image, and, well, you get the idea. I hate this. I've blogged before about the jQuery Thickbox plugin. It is a great solution to this problem, especially when you team it up with ColdFusion. Here is a great example.
I want to build a page which will make use of the Thickbox plugin, but I don't want to hard code the images. Instead, I'll let ColdFusion scan a folder and create the list for me. Since I'm using ColdFusion 8, I can also use it to create the thumbnails as well. This means I can copy images right off my camera, dump them in a folder, and leave it at that. I like easy. Let's look at the code.
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5<title>Untitled Document</title>
6
7<script type="text/javascript" src="http://www.coldfusionjedi.com/js/jquery.js"></script>
8<script type="text/javascript" src="http://www.coldfusionjedi.com/js/thickbox/thickbox.js"></script>
9<link rel="stylesheet" href="http://www.coldfusionjedi.com/js/thickbox/thickbox.css" type="text/css" media="screen" />
10</head>
11
12<body>
This is the header of the page. Most of it comes right from the default template Dreamweaver made for me. What you really want to care about are the 2 JavaScript includes and the CSS file. These are all the requirements needed for Thickbox.
2<cfset imageDir = expandPath("./#imageFolder#")>
3<cfdirectory directory="#imageDir#" name="images">
4
5<!--- make thumbs --->
6<cfif not directoryExists("#imageDir#/thumbs")>
7 <cfdirectory action="create" directory="#imageDir#/thumbs">
8</cfif>
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.
2
3 <!--- valid image? --->
4 <cfif isImageFile("#directory#/#name#")>
5 <!--- check for thumbnail --->
6 <cfif not fileExists("#directory#/thumbs/#name#")>
7 <cfimage action="read" source="#directory#/#name#" name="image">
8 <cfset imageScaleToFit(image, 125, 125)>
9 <cfset imageWrite(image, "#directory#/thumbs/#name#",true)>
10 </cfif>
11
12 <cfoutput>
13 <a href="#imageFolder#/#name#" title="#name#" class="thickbox" rel="gallery-ss"><img src="#imageFolder#/thumbs/#name#" alt="#name#" /></a>
14 </cfoutput>
15 </cfif>
16</cfloop>
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.


If you want to use the writetobrowser action and resize each image on the fly you can with your imageUtils. <a href="http://imageutils.riaforge.org/">http://im...; using the getURL() method. Just a thought so you don't have to write all the thumbs to disk. Might be expensive depending on the application.
The only difference is I used Lightbox (http://plugins.jquery.com/project/jquerylightbox_b...) instead of Thickbox. No particular reason why I chose one over the other, I think it was just the first one I found.
As for getURL, that is for converting a URL into an image, ie, taking a screen shot of a web page. It is not for resizing images.
So you might do
<cfparam name="url.gogogadgetreset" default="">
<cfswitch expression="url.gogogadgetreset">
<cfcase value="rebuild"> Dump the thumb folder and start over</cfcase>
<cfcase value="refresh">simply refresh the missing thumbs</cfcase>
<cfdefaultcase>do nothing</cfdefaultcase>
</cfswitch>
Just a thought
Chris
That gives me some concerns about using jquery. I guess nothing is perfect. I am just trying to convince others at my company of its benefits, and this didn't help.
I know there is something I'm probably missing. I'm just going to go back to CF.
@Wade: Again, my local server was using 1.3.1. Not sure what you mean by saying you will just go back to CF - as this IS CF, CF and JavaScript. :) I wouldn't give up yet. Can you put this online where we can look?
http://www.bray.com/tboxtest/index.cfm?test=works
http://www.bray.com/tboxtest/index.cfm?test=fail
What I meant by "back to using CF" I left off and actionscript.
http://www.bray.com/tboxtest/macFFBgHack.png
On the docs for Thickbox, they talk about the fixes you have to do in code for this.
I've been meaning to install Firebug, but I always put it off. Now, I will do it while it is fresh on my mind.
http://fancy.klade.lv/
<cflayout type="border">
<cflayoutarea name="whatever" position="center">
<script type="text/javascript" src="http://www.coldfusionjedi.com/js/jquery.js"&g...;
<script type="text/javascript" src="http://www.coldfusionjedi.com/js/thickbox/thickbox...;
<link rel="stylesheet" href="http://www.coldfusionjedi.com/js/thickbox/thickbox...; type="text/css" media="screen" />
</head>
<body>
<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>
<cfloop query="images">
<!--- 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>
</cflayoutarea>
</cflayout>
Any help would be appreciated!
http://codylindley.com/thickboxforum/comments.php?...
I replace the line mentioned and it worked for me.
Thanks again Jedi!!!
A_Picture_Of_My_Dog.jpg
But a caption that was a bit longer would be ugly. I'd maybe consider storing the captions in a db table that related file names to captions.
[Add Comment] [Subscribe to Comments]