Description of "specialcounter.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 second part of the script works with the image. It loads the blank original image and then sets the text properties. The word "HITS" and the count total both use the Tahoma font, but the count is bigger and uses a bold style.

<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>
<cffile action="delete" file=#filename#>
<cffile action="write" file=#filename# output=#count#>
<cfloop condition="Len(count) LESS THAN 4">
  <cfset count="0" & count>
<!-- Count incremented now create the image -->
<cfobject action="create" name="image" class="csImageFile.Manage">
<cfset image.ReadFile(currentdir & "\images\specialcount.png")>
<cfset image.TextOpaque = false>
<cfset image.TextSize = 20>
<cfset image.TextColor = "000000">
<cfset image.TextFont = "Tahoma">
<cfset image.Text(200, 115, "HITS")>
<cfset image.TextBold = true>
<cfset image.TextSize = 38>
<cfset image.Text(215, 145, Count)>
<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 "csImageFile" must be replaced with "csImageFileTrial".

The text file containing the count total is in the same directory as the scripts. The physical path of this directory is found by 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 an alternative method of streaming a file without using CFCONTENT or saving 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 Internet Guest Account "Modify" permission. If it only has "Write" permission the cfcontent tag will not delete the file.