Raymond Camden's Blog Rss

Simple image slide show built in ColdFusion 8

15

Posted in ColdFusion | Posted on 08-29-2007 | 6,867 views

I wrote up a quick and dirty slide show application in ColdFusion 8 and thought I'd share the code. While it isn't very pretty (I have a second version to show you tomorrow), it gets the job done. First take a look at the demo, and then I'll describe the code:

Demo

The demo makes use of two ColdFusion 8 features - image support and and the cflayout tag. Let me talk first about how the images are handled. I begin by getting a list of all the files in a folder:

view plain print about
1<!--- get my images --->
2<cfdirectory action="list" directory="#folder#" name="images" type="file">

Note the new type attribute. This lets you filter a directory listing to just files or directories. Next I run a query of query to filter out just the images. Why not use the filter attribute? The cfdirectory tag only lets you filter by one extension. I wanted to support both GIFs and JPGs so I used the following:

view plain print about
1<!--- filter out jpg and gif and _thumb_* --->    
2<cfquery name="images" dbtype="query">
3select    name
4from        images
5where     lowerr(name) like '%.jpg'
6or        lower(name) like '%.gif')
7and        lower(name) not like '_thumb_%'
8</cfquery>

Notice the last condition. I'm also going to filter out any file named _thumb_*. Why? I'm getting there.

Next I create a simple layout using the cflayoutarea tag, type=border. I'm not going to bother showing that code here, you can see it at the end. In my left hand menu I want to show all my pictures, but I want to show thumbnails and not a scaled down version of the full image. How do I do that?

view plain print about
1<cfloop query="images">
2    <cfif not fileExists(folder & "_thumb_" & name)>
3        <cfimage source="#folder##name#" action="read" name="newimage">
4        <cfset imageScaleToFit(newimage, 100, 100)>
5        <cfimage action="write" source="#newimage#" destination="#folder#/_thumb_#name#" overwrite="true">
6    </cfif>
7            
8    <cfoutput><a href="javaScript:setImage('#jsStringFormat(name)#');"><img src="#folderurl#/_thumb_#name#" border="0" vspace="5" /></a></cfoutput>
9</cfloop>

I begin by looping over my images query. For each one, I check for the existence of a thumbnail version. If it doesn't exist, I read in the file using cfimage. I then use the scaleToFit function. This will resize and keep the proportions of my thumbnail. Lastly I write out the scaled down image. That last bit of JavaScript just sets the image for my main display.

Pretty simple, eh? As I said, I got a slightly sexier version to show tomorrow. Here is the complete code for the demo. Note that I abstracted out the URL and folder for images. In theory you could turn this into a simple custom tag.

view plain print about
1<cfsetting showdebugoutput=false>
2
3<!--- what url is the folder, relative from me --->
4<cfset folderurl = "images2">
5<!--- full path to folder --->
6<cfset folder = expandPath("./images2/")>
7
8<!--- get my images --->
9<cfdirectory action="list" directory="#folder#" name="images" type="file">
10
11<!--- filter out jpg and gif and _thumb_* --->    
12<cfquery name="images" dbtype="query">
13select    name
14from        images
15where     lowerr(name) like '%.jpg'
16or        lower(name) like '%.gif')
17and        lower(name) not like '_thumb_%'
18</cfquery>
19        
20
21<cflayout type="border">
22
23<cflayoutarea position="left" title="Pictures" size="150" align="center" collapsible="true">
24
25<script>
26function setImage(i) {
27    var mImage = document.getElementById('mainImage');
28    <cfoutput>
29    mImage.src='#folderurl#/'+i;
30    </cfoutput>
31}        
32</script>
33
34<cfloop query="images">
35    <cfif not fileExists(folder & "_thumb_" & name)>
36        <cfimage source="#folder##name#" action="read" name="newimage">
37        <cfset imageScaleToFit(newimage, 100, 100)>
38        <cfimage action="write" source="#newimage#" destination="#folder#/_thumb_#name#" overwrite="true">
39    </cfif>
40            
41    <cfoutput><a href="javaScript:setImage('#jsStringFormat(name)#');"><img src="#folderurl#/_thumb_#name#" border="0" vspace="5" /></a></cfoutput>
42</cfloop>        
43
44</cflayoutarea>
45
46<cflayoutarea position="center" align="center">
47
48<cfoutput>
49<p>
50<img src="#folderurl#/#images.name[1]#" id="mainImage">
51</p>
52</cfoutput>
53
54</cflayoutarea>
55
56</cflayout>

Comments

[Add Comment] [Subscribe to Comments]

imageScaleToFit??? I can't believe I did not see that in the docs. Ugh! I've been using a bit of 9th grade algebra and then cfimage's resize function to maintain proportions when only a width is specified.

My algebra went like this:
<cfset resizedHeight = round((resizedWidth * objImage.Height) / objImage.Width) />

imageScaleToFit will be a lot easier. I knew there had to be a function for this.
Better now than never Che. :)
What really concerns me is how well that alligator gets along with those kittens. Your reptile training skills must be amazing!
You know, I wonder why proportional resizing with only the width or height was not built in to the <cfimage> tag?

Anyone have any ideas why a separate CF function had to be created to accomplish this?
Che:
I do not propose to speak for Adobe, but this is my take.

The cfimage tag was built to do "common" functions. Not everything. So adobe made choices on what would make sense in there.

Outside of that - everything (*) cfimage can do can be done in image functions.

When I say everything, I mean everything but writeToBrowswer, and while CAPTCHA can be done with functions, it is a heck of a lot easier with cfimage.
Ray,

I think this is why your web site is so great - you provide lots of examples - please keep the CF8 examples coming.

I would love to see what Coldfusion can do with an image uploading interface.

I am waiting for your and Ben's new book to come out.
Thanks for the compliments. In regards to your second paragraph, are you asking for a simple image upload demo? Ie, how to handle someone uploading an image - ensuring it is an image - checking size - etc.
KITTENS!
Ray,

Yes, I would love to see a simple image upload demo in CF8!

I created my own image uploader for my rental site in CF7 using Doug Hugh's CFC but I would love to see what the new image tag can do.
Dave, please find my blog entry after my last presentation on cfimage. I have sample code in there just for that.
Ray,

I can't seem to locate it. If it is in your new book, i will see it there.

Thanks,
Dave
Thanks for posting this code. Though simple, it's just what I needed to post some photos of a local crime story. Thank you, thank you.
Crime story? Now ou have to share the link.
Now you need to make it cycle automatically. Or is that in the "sexier" version? (Which I guess I should go look for)

[Add Comment] [Subscribe to Comments]