Description of "sold.cfm"

This script is called from the <img> tag and generates a compound JPG image. The listing is shown below:

<cfcache action="flush">
<cfobject action="create" name="image" class="csImageFile.Manage">
<cfset currentdir=ExpandPath("./")>
<cfset image.ReadFile(currentdir & "images\car.jpg")>
<cfset image.Transparent=true>
<cfset image.TransparentColor="ffffff">
<cfset image.MergeBack(currentdir & "images\sold.png", 0, 0)>
<cfset tempfile=currentdir & CreateUUID() & ".jpg">
<cfset image.WriteFile(#tempfile#)>
<cfcontent type="image/jpeg" deletefile="yes" file=#tempfile# >

The first line prevents the page from caching. This is recommended when creating images dynamically.

Then we create an instance of the csImageFile object. If you are using the trial component you must replace "csImageFile" with "csImageFileTrial" ("csImageFile64" or "csImageFile64Trial" for the 64 bit versions).

Next we find the physical path to the directory containing the script. It does this by using uses the ExpandPath function. This path could be hard coded instead.

The original image is read into csImageFile using the ReadFile method. The parameter is the physical path and file name of the jpg image. The images are in a sub directory called "Images".

Then we use the MergeBack command to put the "stamp" over the image. In this case the stamp is a PNG image with a white background. By setting Transparent to true and TransparentColor to white this background will be transparent. The MergeBack command can detect the transparency settings of a PNG or GIF if they have been saved in the file. This includes alpha transparency of a PNG.

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 JPG 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/jpeg".

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.

The stamp could have been drawn instead of using the Merge command because it is simply two lines and a piece of text. The technique shown here will work with a more complex foreground image, including a PNG with alpha transparency.