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"
destination="c:\images\logo_sm.gif">

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")>
<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 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")>
<cfset y = ImageNew(x)>

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
result to a variable. --->
<cffile action = "readBinary" file = "jeff05.jpg" variable = "aBinaryObj">
<!--- Use the ImageNew function to create a ColdFusion image from the variable. --->
<cfset myImage=ImageNew(aBinaryObj)>

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. --->
<cfquery
name="GetBLOBs" datasource="myblobdata">
SELECT LastName,Image
FROM Employees
</cfquery>
<cfset i = 0>
<table border=1>
<cfoutput query="GetBLOBs">
<tr>
<td>
#LastName#
</td>
<td>
<cfset i = i+1>
<!--- Use the cfimage tag to write the images to PNG files. --->
<cfimage source="#GetBLOBs.Image#" destination="employeeImage#i#.png"
action="write">
<img src="employeeImage#i#.png"/>
</td>
</tr>
</cfoutput>
</table>

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. --->
<cfquery name="GetBLOBs" datasource="myBlobData">
SELECT EMLPOYEEID, PHOTO FROM Employees
</cfquery>
<!--- Use the ImageNew function to create a ColdFusion images from the BLOB data that was
retrieved from the database. --->
<cfset myImage = ImageNew(GetBLOBs.PHOTO)>
<!--- Create thumbnail versions of the images by resizing them to fit in a 100-pixel square,
while maintaining the aspect ratio of the source image. --->
<cfset ImageScaleToFit(myImage,100,100)>
<!--- Convert the images to JPEG format and save them to files in the thumbnails subdirectory,
using the employee ID as the filename. --->
<cfimage source="#myImage#" action="write"
destination="images/thumbnails/#GetBLOBs.EMPLOYEID#.jpg">

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
(used for display in HTML). --->
<cfimage source="data:image/jpg;base64,/9j/4AAQSkZJRgABAQA.............."
destination="test_my64.jpeg" action="write" isBase64="yes">

<!--- This example shows how to use the cfimage tag to write a Base64 string without headers. --->
<cfimage source="/9j/4AAQSkZJRgABAQA.............." destination="test_my64.jpeg"
action="write" isBase64="yes">

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
with headers. --->
<cfset myImage=ImageReadBase64("data:image/jpg;base64,/9j/4AAQSkZJRgABAQA..............")>

<!--- This example shows how to read a Base64 string without headers. --->
<cfset myImage=ImageReadBase64("/9j/4AAQSkZJRgABAQA..............")>

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.
--->
<cfimage source="../cfdocs/images/artgallery/lori05.jpg" name="myImage">
<!--- Turn on antialiasing to improve image quality. --->
<cfset ImageSetAntialiasing(myImage)>
<!--- Copy the rectangular area specified by the coordinates (25,25,50,50) in the image to
the rectangle beginning at (75,75), and return this copied rectangle as a new ColdFusion
image. --->
<cfset dupArea = ImageCopy(myImage,25,25,50,50,75,75)>
<!--- Write the new ColdFusion image (dupArea) to a PNG file. --->
<cfimage source="#dupArea#" action="write" destination="test_myImage.png" overwrite="yes">

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. --->
<cfset myImage=ImageNew("../cfdocs/images/artgallery/paul01.jpg")>
<!--- Turn on antialiasing to improve image quality. --->
<cfset ImageSetAntialiasing(myImage)>
<!--- Use the Duplicate function to create three clones of the ColdFusion image. --->
<cfset cloneA=Duplicate(myImage)>
<cfset cloneB=Duplicate(myImage)>
<cfset cloneC=Duplicate(myImage)>
<!--- Create a grayscale version of the image. --->
<cfset ImageGrayscale(cloneA)>
<!--- Create a thumbnail version of the image. --->
<cfset ImageScaleToFit(cloneB,50,"")>
<!--- Create an enlarged version of the image. --->
<cfset ImageResize(cloneC,"150%","")>
<!--- Write the images to files. --->
<cfset ImageWrite(myImage,"paul01.jpg","yes")>
<cfset ImageWrite(cloneA,"paul01_bw.jpg","yes")>
<cfset ImageWrite(cloneB,"paul01_sm.jpg","yes")>
<cfset ImageWrite(cloneC,"paul01_lg.jpg","yes")>
<!--- Display the images. --->
<img src="paul01.jpg">
<img src="paul01_bw.jpg">
<img src="paul01_sm.jpg">
<img src="paul01_lg.jpg">

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). --->
<cfimage source="../cfdocs/images/artgallery/paul01.jpg" name="myImage">
<cfimage source="#myImage#" name="myImageCopy">
<!-- Use the ImageNew function to make a copy of myImage called myImageCopy2. --->
<cfset myImageCopy2 = ImageNew(myImage)>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy