Not yet rated

Problem

I want to create a simple gallery from images I have uploaded in a particular directory.

Solution

This can be done using CFDIRECTORY and CFIMAGE. You can read from the directory and using CFIMAGE, resize then display. See simple example below.

Detailed explanation

First set some variables, these will tell us where the image folder is and the height and width of the thumbnails we want to create. You can declare these setting either in your template or if you prefer globally in your application.  In this example no scope is  assumed, but its generally good practice to declare one.

<!--- change to your image directory --->
<cfset imagesFolder = "./img/gallery/">
<!--- image width in px --->
<cfset varWidth = "100">
<!--- image height in px --->
<cfset varHeight = "100">
<!--- get expanded path --->
<cfset imagesFolderPath = ExpandPath(imagesFolder)>

Next we get all images from the specified directory

<cfdirectory action="list"
  directory="#imagesFolderPath#"
  name="gallery" 
  filter="*.jpg">

Resize and output all images to the browser and display. In this case using lightbox change as needed.

<cfoutput query="gallery">

  <cfimage action="resize"
    source="#imagesFolderPath##gallery.name#"
    width="#varWidth#" 
    height="#varHeight#" 
    name="ImageObject">
  <!---
  rel=lightbox[xyz] is my light box group change to yours
  --->
  <a href="img/gallery/#gallery.name#" rel="lightbox[xyz]">
    <cfimage action="writetobrowser"
      source="#ImageObject#">
  </a>

</cfoutput>

Note: this example will only work using ColdFusion 8+ and only gets jpg images. If you would like other file formats see filters for cfdirectory.

 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes