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 image file that ColdFusion reads, 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. 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 path. The following example shows how to create a ColdFusion image from a URL and write it to a file on the local storage drive:
<cfimage source="http://www.google.com/images/logo_sm.gif" action="write" |
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 line 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 path, 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")> |
The following code produces the same result:
<cfimage source="../cfdocs/images/artgallery/jeff01.jpg" name="myImage"> |
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 canvasses 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 Base64 bytearrays, 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")> |
For more information about the ImageNew function, see the CFML Reference.
Creating an image from a binary object
You can use the cffile tag to write an image file to ColdFusion variable. Then, you can pass the variable to the ImageNew function to create a ColdFusion image from the binary object, as the following example shows:
<!--- Use the cffile tag to read an image file, convert it to binary format, and write the |
Creating images from BLOB data
Many databases store images as BLOB data. To extract BLOB data from a database, create a query with the cfquery tag. The following example shows how to extract BLOB data and use the cfimage tag to write them to files in PNG format:
<!--- Use the cfquery tag to retrieve employee photos and last names from the database. ---> |
The following example shows how to use the ImageNew function to generate ColdFusion images from BLOB data:
<!--- Use the cfquery tag to retrieve all employee photos and employee IDs from a database. ---> |
For information on converting a ColdFusion image to a BLOB, see Inserting an image as a BLOB in a database in Converting images.
Creating an image from a Base64 string
Base64 is a way to describe binary data as a string of ASCII characters. Some databases store images in Base64 format rather than as BLOB data. You can use the cfimage tag or the ImageReadBase64 function to read Base64 data directly from a database. Doing so eliminates the intermediary steps of binary encoding and decoding.
The following examples show how to use the cfimage tag to create a ColdFusion image from a Base64 string:
<!--- This example shows how to create a ColdFusion image from a Base64 string with headers |
The following examples show how to use the ImageReadBase64 function to create a ColdFusion image from a Base64 string:
<!--- This example shows how to use the ImageReadBase64 function to read a Base64 string |
For more information on Base64 strings, see Converting an image to a Base64 string in Converting images .
Copying an image
You use the ImageCopy function to copy a rectangular area of an existing image and generate a new ColdFusion image from it. You can paste the new ColdFusion image onto another image, or write it to a file, as the following example shows:
<!--- Use the cfimage tag to create a ColdFusion image from a JPEG file. |
Duplicating an image
Another way to create a ColdFusion image is to duplicate it. Duplicating an image creates a clone, which is a copy of an image that is independent of it: if the original image changes, those changes do not affect the clone, and the reverse. This technique is useful if you want to create several versions of the same image. Duplicating an image can improve processing time because you retrieve image data from a database or a file once to create the ColdFusion image. Then you can create several clones and manipulate them in memory before writing them to files. For example, you could create a thumbnail version, a grayscale version, and an enlarged version of an image uploaded to a server. To do so, you use the cfimage tag or the ImageNew function to create a ColdFusion image from the uploaded file. You use the Duplicate function to create three clones of the ColdFusion image.
To create a clone, you can pass a ColdFusion image variable to the Duplicate function:
<!--- Use the ImageNew function to create a ColdFusion image from a JPEG file. ---> |
Also, you can use the cfimage tag and the ImageNew function to duplicate images, as the following example shows:
<!--- Use the cfimage tag to create a ColdFusion image (myImage) and make a copy of it (myImageCopy). ---> |