Using csImageFile with Cold Fusion

If Cold Fusion is running on Windows it can use COM objects. The DLL file for the object must be registered on the server and any appropriate permissions set. If it is the 64 bit version of Cold Fusion it must be at least Version 10 Update 11. Prior to this update, only the 32 bit version would support COM objects.

The object is created inside a <cfobject> tag. Properties are set and methods called either inside <cfset> tags or inside a <cfscript> block. With our components the class name will differ depending on whether the 32 bit or 64 bit, full or trial versions oare used. Take care to use the appropriate class name.

When a dynamically created image is to be streamed to the browser there are two methods. One is to create a temporary file and stream it using the <cfcontent> tag. Most of our examples show this method because it was the only method available with Cold Fusion 5. Modern versions of Cold Fusion can stream a file dynamically using the PageContext Java object, which requires more code but does not create a temporary file.

Both of these methods require the dynamic image to be created in a separate script, called as the src attribute in an image tag.

Creating an image with Cold Fusion - using a temporary file

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 "Modify" permission to the account which Cold Fusion is using (typically Local Service or Network Service).

<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:

Creating an image with Cold Fusion - using PageContext

The code fragment below shows how to create an image dynamically and stream it using the PageContext Java Object.

<cfcache action="flush">
<cfobject action="create" name="Image" class="csImageFile.Manage">
<cfscript>
  Image.NewImage(150, 50, "00ff00");
  Image.TextOpaque=false;
  Image.TextSize=22;
  Image.Text(5, 10, "Sample image");
  Context = GetPageContext();
  Context.SetFlushOutput(false);
  Response = Context.GetResponse().GetResponse();
  Out = Response.GetOutputStream();
  Response.SetContentType("image/gif");
  Out.Write(Image.GIFData);
  Out.Flush();
  Response.Reset();
  Out.Close();
</cfscript>

Note how several commands can be added inside a <cfscript> tag.

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")>

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