| By Ben Nadel | Article Rating: |
|
| July 31, 2007 08:00 PM EDT | Reads: |
28,646 |
Unlike the CFImage tag, ImageRead() does not have a boolean flag for reading in Base64 data. When it comes to functions, if you want to read in Base64 image data, you have to use the ImageReadBase64() function:
<!---
Read in the binary image data. This will read
the data into a binary object, NOT the ColdFusion
image object.
--->
<cffile
action="READBINARY"
file="#ExpandPath( 'lady.jpg' )#"
variable="binImage"
/>
<!---
Read in the image by converting the binary image
data to a Base64 encoding.
--->
<cfset objImage = ImageReadBase64(
ToBase64( binImage )
) />
In addition to these straightforward methods, ColdFusion 8 can also use the ImageNew() function to read in images. ImageNew() has other features (optional arguments), but for our read/write tutorial, let's just concentrate on the first argument - the image source. Like the methods and tags above, ImageNew() can also take a variety of source types:
- Absolute path name.
- Web relative path name.
- URL.
- ColdFusion image variable.
- A BLOB / byte array.
- A Java buffered images.
We have looked at most of these types above. The only one here that stands out is the Java buffered image. This is the object that the ColdFusion image is wrapped around and is exposed through the function ImageGetBufferedImage(). To demonstrate this, we can grab the buffered image of one image object and use it to instantiate a new image:
<!--- Read in the image. --->
<cfset objImage = ImageNew(
"./lady.jpg"
) />
<!---
Using the buffered image data from the
first image, create a new image.
--->
<cfset objImage2 = ImageNew(
ImageGetBufferedImage( objImage )
) />
This kind of functionality can be super useful if you are interfacing with another Java component that handles image manipulation using the java.awt library and all you have access to is the underlying buffered image.
That just about wraps up reading in image files. Let's quickly cover writing images. As with reading in images, writing them can be done by using the CFImage tag as well as the image functions. The CFImage tag makes writing files especially easy since the source attribute is so flexible. Here, we are going to read an image from a URL and save it to disk:
<!---
Grab the image from the give source URL and save
it to disk (at the relative web path).
--->
<cfimage
action="WRITE"
source="http://farm1.static.flickr.com/240/458712628_2d6eff71e2.jpg"
destination="funny.gif"
overwrite="true"
/>
Notice that the destination path is not an absolute server path - it's a path relative to the current Web page. Also notice that the destination image was a GIF file format. This will automatically convert the image from the JPG that was served at the URL to the GIF image format that we save onto the disk. By default ColdFusion will throw an error if there are naming conflicts. To overcome this, you can set the Overwrite attribute to true (defaults to false) - this will overwrite any existing files of the same name. Now, not only is it super easy to grab images, it's super easy to convert image types.
We used the WRITE action here to grab the image and convert it to a new file format. You can also use the CONVERT action to accomplish just about the same thing. The only real difference (that I can see) between using a straight up WRITE versus CONVERT is that the CONVERT action allows you to also save the image data into a ColdFusion image object:
<!---
Grab the image from the give source URL and save
it to disk (at the relative web path).
--->
<cfimage
action="CONVERT"
source="http://farm1.static.flickr.com/240/458712628_2d6eff71e2.jpg"
destination="funny.gif"
overwrite="true"
name="objImage"
/>
<!--- Write the image to the browser. --->
<cfimage
action="WRITETOBROWSER"
source="#objImage#"
format="jpg"
/>
Published July 31, 2007 Reads 28,646
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Ben Nadel
Ben Nadel has worked with ColdFusion for eight years and is a super ColdFusion enthusiast. He blogs regularly about all aspects of Web development on his personal site, http://www.bennadel.com, and does his best to give back to the ColdFusion community through online code demos and his "Ask Ben" blog posts. He is also a Certified Advanced ColdFusion MX7 developer and is one of the lead programmers at Nylon Technology.
![]() |
Michael 07/12/08 01:43:25 AM EDT | |||
The noisy ad is beyond annoying. |
||||
![]() |
Mike Ritchie 08/01/07 06:08:54 PM EDT | |||
I wonder if the CF team consulted with the developer of ImageCFC? It also was basically a wrapper for the underlying Java image functionality. Either way, this looks like an exceptionally useful tool. Do you need to instantiate the object using cfimage? Or is it possible to manipulate an image completely within cfscript? |
||||
![]() |
Sanjeev 07/15/07 10:07:34 PM EDT | |||
rename the CodFusion as ColdFusion ! |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?










































