Adobe ColdFusion 8

Creating ColdFusion images

The ColdFusion image contains image data in memory. Before you can manipulate images in ColdFusion, you create a ColdFusion image. The following table shows the ways to create a ColdFusion image:

Task

Functions and tags

Create a ColdFusion image from an existing image file.

cfimage tag or the ImageNew function

Create a blank image from scratch.

ImageNew function

Create a ColdFusion image from BLOB data in a database.

ImageNew function with the cfquery tag

Create a ColdFusion image from a binary object.

cffile tag with the ImageNew function

Create a ColdFusion image from a Base64 string.

ImageReadBase64 function and the ImageNew function or the cfimage tag

Create a ColdFusion image from another ColdFusion image.

ImageCopy function with the ImageWrite function or the Duplicate function, or by passing the image to the ImageNew function or the cfimage tag

Using the cfimage tag

The simplest way to create a ColdFusion image with the cfimage tag is to specify the source attribute, which is the file that is read by the ColdFusion image, and the name attribute, which is the variable that defines the image in memory:

<cfimage source="../cfdocs/images/artgallery/jeff01.jpg" name="myImage">

You do not have to specify the read action because it is the default action. You must specify the name attribute for the read action, which creates a variable that contains the ColdFusion image, for example, myImage.

You can pass the myImage variable to another cfimage tag or to Image functions. The following example shows how to specify a ColdFusion image variable as the source:

<cfimage source="#myImage#" action="write" destination="test_myImage.jpg">

The write action writes the file to the specified destination, which can be an absolute or relative pathname. The following example shows how to create a ColdFusion image from a URL and write it to a file on the local drive:

<cfimage source="http://www.google.com/images/logo_sm.gif" action="write"
     destination="c:\images\logo_sm.gif">

You must specify the destination for the write action.

When you specify a destination, set the overwrite attribute to "yes" to write to the same file more than once. Otherwise, ColdFusion generates an error:

<cfimage source="#myImage#" action="write" destination="images/jeff01.jpg" overwrite="yes">

Using the ImageNew function

You create a ColdFusion image with the ImageNew function the same way you define a ColdFusion variable. The following example creates a ColdFusion image variable named "myImage" from the jeff01.jpg source file:

<cfset myImage=ImageNew("../cfdocs/images/artgallery/jeff01.jpg")>

This produces the same result as the following code:

<cfimage source="../cfdocs/images/artgallery/jeff01.jpg" name="myImage">

As with the cfimage tag, you can specify an absolute or relative pathname, a URL, or another ColdFusion image as the source. In the following example, ColdFusion reads a file from the local drive and passes it to the ImageWrite function, which writes the image to a new file:

<cfset myImage=ImageNew("../cfdocs/images/artgallery/jeff01.jpg")>
<cfset ImageWrite(myImage,"myImageTest.png")>

The following code produces the same result:

<cfimage source="../cfdocs/images/artgallery/jeff01.jpg" name="myImage">
<cfimage source="#myImage#" action="write" destination="myImageTest.png">

Also, you can create a blank image. When using the ImageNew function, you do not specify the source to create a blank image. However, you can specify the width and height, respectively. The following example shows how to create a blank canvas that is 300 pixels wide and 200 pixels high:

<cfset myImage=ImageNew("",300,200)>

Optionally, you can specify the image type, as in the following example:

<cfset myImage=ImageNew("",200,300,"rgb")>

Other valid image types are argb and grayscale. You can use blank images as canvases for drawing functions in ColdFusion. For examples, see Creating watermarks.

Also, you can use the ImageNew function to create ColdFusion images from other sources, such as Bas64 byte arrays, file paths, and URLs. The following example creates a ColdFusion image from a JPEG file (x), and then creates another ColdFusion image (y) from the image in memory:

<cfset x = ImageNew("c:\abc.jpg")>
<cfset y = ImageNew(x)>

For more information about the ImageNew function, see the CFML Reference.