Using csImageFile with Cold Fusion

If Cold Fusion is running on Windows NT Server/2000/2003 it can use COM objects. The DLL file for the object must be registered on the server and any appropriate permissions set.

The object is created inside a <cfobject> tag. Properties are set and methods called either inside <cfset> tags or inside a <cfscript> block. Cold Fusion does not directly support binary streaming, except from a file, so any dynamically created image must be saved to disk as a temporary file. This is not a problem because the <cfcontent> tag which controls streaming has an automatic delete option.

*** Note *** There is an alternative streaming method available in Cold Fusion MX which uses the PageContext Java Object and this can stream images directly to the browser without saving to a temporary file first. For more information - Click Here.

The code fragment below shows how to create a temporary file with a unique name by using the CreateUUID function. To delete the file after streaming the directory permissions must give the Internet Guest User "Modify" permission.

<cfcache action="flush">
<cfobject action="create" name="Image" class="csImageFile.Manage">
<cfset Image.NewImage(150, 50, "00ff00")>
<cfset Image.TextOpaque=false>
<cfset Image.TextSize=22>
<cfset Image.Text(5, 10, "Sample image")>
<cfset tempfile=ExpandPath(".") & "\" & CreateUUID() & ".gif">
<cfset Image.WriteFile(#tempfile#)>
<cfcontent type="image/gif" deletefile="yes" file=#tempfile#>

Sample Image

This is the sample image produced:

The instructions to csImageFile are largely aimed at ASP users and assume that VBScript is used. There are some syntax differences when using Cold Fusion, the most important being the use of brackets when calling functions.

VBScript:       Image.NewImage 150, 50, "00ff00"

Cold Fusion:  <cfset Image.NewImage(150, 50, "00ff00")>

Several commands can be added inside a <cfscript> tag, for example:

<cfscript>
Image.NewImage(150, 50, "00ff00");
Image.TextOpaque=false;
Image.TextSize=22;
Image.Text(5, 10, "Sample image");
</cfscript>

There are some working examples of using csImageFile with Cold Fusion in our demonstration area.

For a full list of available Cold Fusion examples and tutorials - Click Here