Description of the Multipage TIFF demo

There are two scripts used in this demo. "tifdemo.cfm" is the HTML page that controls the navigation and acts as a placeholder for the image. "showtif.cfm" loads the image and streams it to the browser.

tifdemo.cfm

Most of the scripting code is at the start of the page where it finds the number of pages and sets some variables:

<cfcache action="flush">
<cfobject action="create" name="Image" class="csImageFileTrial.Manage">
<cfset TIF = "multipage.tif">
<cfset TIFPath=ExpandPath(TIF)>
<cfset PageCount = Image.ImageCount(TIFPath)>
<cfparam name="Url.Page" default="1">

<cfset CurrentPage = Url.Page>
<cfif NOT IsNumeric(CurrentPage)>
  <cfset CurrentPage = 1>
<cfelse>
  <cfif CurrentPage LT 1>
    <cfset CurrentPage = 1>
  </cfif>
  <cfif CurrentPage GT PageCount>
    <cfset CurrentPage = PageCount>
  </cfif>
</cfif>

<cfset Previous = CurrentPage - 1>
<cfset Next = CurrentPage + 1>

The ImageCount method in csImageFile is used to read the number of pages in the TIFF. It needs the physical path to the file, which is found from the relative path using ExpandPath. The rest of the code sets the CurrentPage variable by reading the URL variable that is passed by the links later in the page. If the URL variable is an empty string, not a number or outside the range of valid numbers, the CurrentPage variable is set accordingly.

The next working part of the script is in the body of the page. The formatting has been kept to a minimum and there is no attempt to hide invalid links, such as the Previous link when the first page is displayed. The listing does not show the line breaks.

Page Number: <cfoutput>#CurrentPage#</cfoutput> of <cfoutput>#PageCount#</cfoutput>
<a href="tifdemo.cfm?Page=1">First</a>
<a href="tifdemo.cfm?Page=<cfoutput>#Previous#</cfoutput>">Previous</a>
<a href="tifdemo.cfm?Page=<cfoutput>#Next#</cfoutput>">Next</a>
<a href="tifdemo.cfm?Page=<cfoutput>#PageCount#</cfoutput>">Last</a>
<img src="showtif.cfm?Page=<cfoutput>#CurrentPage#</cfoutput>&Path=<cfoutput>#TIF#</cfoutput>">

The current page and the relative path to the TIFF are passed to the image generation script as URL parameters.

showtif.cfm

This code reads the URL parameters passed from "tifdemo.cfm". It sets the ReadImageNumber property and then loads the file. Only the page specified by ReadImageNumber is loaded. The ColorDepth property is checked to determine which format to use when streaming the image to the browser. For colour depths of 8 bits or less a GIF can display the image without losing any image quality. For higher colour depths the JPG format is used. This conditional code could be left out and one or other format used regardless of the colour depth.

Earlier versions of Cold Fusion, prior to MX, could not stream the image directly to the browser and the JPGData and GIFData methods in csImageFile could not be used. To work around this we have saved the file to disk and the cfcontent tag is used to stream and delete the file. This example shows the use of the CreateUUID function to generate a unique temporary file name, although other methods could be used such as using a session ID variable. For an alternative method of streaming a file in MX without using cfcontent or saving a temporary file Click Here.

<cfcache action="flush">
<cfobject action="create" name="Image" class="csImageFileTrial.Manage">
<cfset TempDir=ExpandPath("./temp/")>
<cfset TempName=CreateUUID()>

<cfset Image.ReadImageNumber = Url.Page>
<cfset Image.ReadFile(ExpandPath(Url.Path))>
<cfif Image.ColorDepth GT 8>
  <cfset ContentType = "image/jpeg">
  <cfset FilePath=TempDir & TempName & ".jpg">
<cfelse>
  <cfset ContentType = "image/gif">
  <cfset FilePath=TempDir & TempName & ".gif">
</cfif>
<cfset Image.WriteFile(FilePath)>
<cfcontent deletefile="yes" type=#ContentType# file=#FilePath#>

Click Here to return to the demo.