Description of "counter.cfm"

This script has two sections. The first part reads the current count from a text file and increments it. The old text file is deleted and replaced with a new one containing the new value.

The count is reset to zero at 10000 and it is converted to a string with leading zeros where appropriate. The values of thousands, hundreds, tens and units are extracted using the Mid string function.

The second part of the script works builds the image. It creates a blank original image and uses one of the csImageFile merge functions to join the 4 digits together using existing images.

<cfcache action="flush">
<cfset currentdir=ExpandPath("./")>
<cfset filename=currentdir & "count.txt">
<cffile action="read" file=#filename# variable=count>
<cfset count=IncrementValue(count>
<cfif (count GREATER THAN 9999)>
  <cfset count=0>
</cfif>
<cffile action="delete" file=#filename#>
<cffile action="write" file=#filename# output=#count#>
<cfloop condition="Len(count) LESS THAN 4">
  <cfset count="0" & count>
</cfloop>
<cfset units=Mid(Count, 4, 1)>
<cfset tens=Mid(Count, 3, 1)>
<cfset hundreds=Mid(Count, 2, 1)>
<cfset thousands=Mid(Count, 1, 1)>
<!-- Count incremented now create the image -->
<cfobject action="create" name="image" class="csImageFile.Manage">
<cfset image.NewImage(120, 40, "ffffff")>
<cfset image.MergeBack(currentdir & "images\" & thousands & ".bmp", 0, 0)>
<cfset image.MergeBack(currentdir & "images\" & Hundreds & ".bmp", 30, 0)>
<cfset Image.MergeBack(currentdir & "images\" & Tens & ".bmp", 60, 0)>
<cfset image.MergeBack(currentdir & "images\" & Units & ".bmp", 90, 0)>
<cfset image.UseLZW=false>
<cfset tempfile=currentdir & CreateUUID() & ".gif">
<cfset image.WriteFile(#tempfile#)>
<cfcontent type="image/gif" deletefile="yes" file=#tempfile#>

Additional notes:

If this script is used with the trial component you must replace "csImageFile" with "csImageFileTrial" ("csImageFile64" or "csImageFile64Trial" for the 64 bit versions).

The text file containing the count total is in the same directory as the scripts. The physical path of this directory is found using the ExpandPath function.

We are using cfcontent to stream the file to the browser so we save it in a temporary file first. To ensure the file name is unique we use the CreateUUID() function to produce a name. For more on streaming a file, including the PageContext method which does not require a temporary file - Click Here.

Finally, we stream the image to the browser in GIF format. The cfcontent tag is used with deletefile="yes". This takes care of removing the temporary file. It is also important to set the content type to "image/gif".

The permissions on the directory containing the script must give the Windows account that runs Cold Fusion must be at least "Modify". If it only has "Write" permission the cfcontent tag will not delete the file.