ImageWriteBase64

Description

Writes Base64 images to the specified on-disk or in-memory destination.

Returns

Base64 string.

Function syntax

ImageWriteBase64(name, destination, format [, inHTMLFormat, optional])

History

ColdFusion 8: Added this function.

Parameters

Parameter

Description

name

Required. The ColdFusion image on which this operation is performed.

destination

Required. The absolute or relative on-disk or in-memory pathname where you write the file.

format

Required. The format

inHTMLFormat

Optional. Specify whether Base64 output includes the headers used by the Base64 images in the HTML <img> tag ("data:image/<format>;base64,...") :

  • yes

  • no (default)

overwrite

Optional. If set to true, overwrites the destination file.

Usage

You use the ImageWriteBase64 function to encode image data as a string of printable characters. This is useful for several applications, including sending images by e-mail and storing images in database text fields.

Use the following syntax to specify an in-memory file, which is not written to disk. In-memory files speed processing of transient data.

ram:///filepath

The filepath can include directories, for example ram:///petStore/images/poodle.jpg. Create the directories in the path before you specify the file. For more information on using in-memory files, see Optimizing transient files in the Developing ColdFusion Applications.

If you do not specify a file format, ColdFusion cannot recognize the format required to encode the image before converting to Base64, and generates errors.

You can verify whether ColdFusion reads a Base64 string properly in the following ways:

  • Use the cfdump tag. For example: <cfdump var="#myImage#">

  • Use the ImageInfo function. For example: <cfset ImageInfo(myImage)>

  • Use the ImageWrite function and save the image as a JPEG file. Then open the JPEG file in a browser or imaging application.

Example

<!--- This example shows how to convert a JPEG image to Base64 format and save it to a file. ---> 
<!--- Create a new ColdFusion image. ---> 
<cfset myImage=ImageNew("../cfdocs/images/artgallery/jeff01.jpg")> 
<!--- Convert the image to Base64 format and write it to a file.---> 
<cfset ImageWriteBase64(myImage,"jeffBase64.txt","jpg","yes")>