Adobe ColdFusion 8

ImageNew

Description

Creates a ColdFusion image.

Returns

A ColdFusion image.

Category

Image functions

Function syntax

ImageNew([source, width, height, imageType, canvasColor])

See also

cfimage, ImageCopy, ImageRead, ImageReadBase64, ImageSetDrawingColor, IsImageFile

History

ColdFusion 8: Added this function.

Parameters

Parameter

Description

source

Optional. The source image pathname, URL, a ColdFusion variable that is read into the new ColdFusion image, or a Java buffered image.

width

Optional. The width of the new ColdFusion image. Valid when the height is specified and the source is not.

height

Optional. The height of the new ColdFusion image. Valid when the width is specified and the source is not.

imageType

Optional. The type of the ColdFusion image to create:

  • rgb
  • argb
  • grayscale

Valid only when width and height are specified.

canvasColor

Optional. Color of the image canvas:

  • Hexadecimal value of RGB color. For example, specify the color white as ##FFFFFF or FFFFFF.
  • String value of color (for example, "black", "red", "green"). The default value of the drawing color is "black".
  • List of three numbers for (R,G,B) values. Each value must be in the range 0-255.

Usage

You can pass the ImageNew function any of the following parameters:

  • Absolute or relative pathname: The image file located at the specified pathname on a disk is read and returned as a ColdFusion image.
  • URL: The image from the specified URL is read and returned as a ColdFusion image.
  • Width and height (imageType is optional): A blank ColdFusion image with the specified attributes is returned.
  • ColdFusion image variable: An image variable in memory; for example, #myImage#.
  • A BLOB from a database that contains image data.
  • A byte array that contains Base64 image data.
  • A Java buffered image.

ColdFusion generates an error when the passed attributes cannot create a valid ColdFusion image.

The ImageNew function and the cfimage read action support the SQL Server Image Column data type.

To read Base64 images, use the ImageReadBase64 function.

If the color value is a string, specify a supported named color; see the name list in Valid HTML named colors. For a hexadecimal value, use the form "##xxxxxx" or "xxxxxx", where x = 0-9 or A-F; use two number signs or none.

Note: If you specify the ARGB image type, the image is white; however, if you specify RGB or grayscale, the image is black. To create blank images consistently, use the canvasColor parameter.

Example

Example 1
<!--- Use the ImageNew function to create a 200x200-pixel image in ARGB format. --->
<cfset myImage = ImageNew("",200,200,"argb")>
<cfimage action="writeTobrowser" source="#myImage#">

Example 2

<!--- This example shows how to create a ColdFusion image from a BLOB in a database. --->
<cfquery 
name="GetBLOBs" datasource="myblobdata">
SELECT LastName,Photo
FROM Employees
</cfquery>
<cfset i = 0>
<table border=1>
    <cfoutput query="GetBLOBS">
        <tr>
            <td>
            #LastName#
            </td>
            <td>
            <cfset i = i+1>
            <cfset myImage=ImageNew("#GetBLOBS.Photo#")>
            <cfset ImageWrite(myImage,"photo#i#.png")>
            </td>
        </tr>
    </cfoutput>
</table>

Example 3

<!--- This example shows how to create a ColdFusion image from a URL. --->
<cfset myImage = ImageNew("http://www.google.com/images/logo_sm.gif")>
<cfset ImageWrite(myImage,"google_via_imagenew.png")>
<img src="google_via_imagenew.png">

Example 4

<!--- This example shows how to use the cffile tag to convert an image file to binary format and pass it as a variable to the ImageNew function. --->
<!---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 = apple.jpg" 
    variable = "aBinaryObj">
<!--- Use the ImageNew function to create a ColdFusion image from the variable. --->
<cfset myImage = ImageNew(aBinaryObj)>

Example 5

<!--- This example shows how to use the cffile tag to write a ColdFusion image to a file. --->
<!--- Use the ImageNew function to create a ColdFusion image from a JPEG file. --->
<cfset myImage = ImageNew("../cfdocs/images/artgallery/aiden01.jpg")>
<!--- Turn on antialiasing to improve image quality. --->
<cfset ImageSetAntialiasing(myImage,"on")>
<!--- Resize the image. --->
<cfset ImageResize(myImage,"50%","")>
<!--- Pass the image object to the cffile tag and write the result to a file on the local drive. --->
<cffile file="#myImage#" action="write" output="c:\test_myImage.jpg">
<cfimage action="writeToBrowser" source="#myImage#">

Example 6

<!--- This example uses cfscript to pass a Java buffered image to the ImageNew function. --->
<cfscript>
    bufferedImage = createObject("java", "java.awt.image.BufferedImage");
    bufferedImage.init(JavaCast("int", 100), JavaCast("int", 100), BufferedImage.TYPE_4BYTE_ABGR);
    myImage = imageNew(bufferedImage);
</cfscript>