Quickie ColdFusion Image Viewer

Need a quick way to add a slide show to a directory of images? I have many times in the past. Here is a simple block of code. It needs to be dropped in the same folder as the images, and it doesn’t do anything fancy like thumbnails, but it does let you quickly add an image browser. This is much easier than giving someone an index of images and asking them to go back and forth to view all the images. Comments are inline with the code, but let me know if you have any questions. And yes, obviously, it could be done a lot more fancier. Remember KISS. <!--- param the image to show ---> <cfparam name="url.image" default="1">

<!— directory —> <cfset dir = expandPath(“.”)>

<!— get all images —> <cfdirectory directory=”#dir#” filter=”*.jpg” name=”images”>

<!— quick sanity checks —> <cfif not isNumeric(url.image) or url.image lte 0 or url.image gt images.recordcount or round(url.image) neq url.image> <cfset url.image = 1> </cfif>

<!— do we even have images? —> <cfif images.recordCount> <cfoutput> <table align=”center”> <tr> <td colspan=”2”><img src=”#images.name[url.image]#”></td> </tr> <!— links —> <tr> <td> <cfif url.image gt 1> <a href=”#listLast(getCurrentTemplatePath(),”\/”)#?image=#url.image-1#”>Previous</a> <cfelse> Previous </cfif> </td> <td align=”right”> <cfif url.image lt images.recordCount> <a href=”#listLast(getCurrentTemplatePath(),”\/”)#?image=#url.image+1#”>Next</a> <cfelse> Next </cfif> </td> </tr> </table> </cfoutput> </cfif> </code>

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments