| By Ben Nadel | Article Rating: |
|
| July 31, 2007 08:00 PM EDT | Reads: |
28,618 |
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,618
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 ! |
||||
- Instant Professionalism Online Despite Yourself...with Ulitzer
- Plone and Drupal: Different Approaches, Different Results
- New Virus Targets Facebook, MySpace and Twitter Users
- Developing APIs for the Cloud
- Oracle Enhances Primavera P6 Enterprise Project Portfolio Management
- Top 20 Cloud Services Providers That Are Gaining Mind Share
- Amazon's VPC Opens the Door for Innovation and Enterprise Cloud Adoption
- Adobe and Barnes & Noble Join Forces to Standardize eBook Technology
- Drupal Tip to Enable Clean URLs on Mac OS
- Beyond Social Business Software
- Amartya Sen: The Idea of Justice
- Terrific New Social Media Research Study Means It’s Time for Small Business to Get with the Program
- Microsoft Expression Web Has Got Game
- Instant Professionalism Online Despite Yourself...with Ulitzer
- Zimbra on Ulitzer Topic Launched
- Plone and Drupal: Different Approaches, Different Results
- New Virus Targets Facebook, MySpace and Twitter Users
- Developing APIs for the Cloud
- Oracle Enhances Primavera P6 Enterprise Project Portfolio Management
- Mezeo CEO to Speak at 4th Cloud Computing Expo
- Excuse Me But Is That a Gazebo On Your Site?!
- Prolifics Offers Easy Transition to IBM Lotus Web Content Management
- Top 20 Cloud Services Providers That Are Gaining Mind Share
- Amazon's VPC Opens the Door for Innovation and Enterprise Cloud Adoption
- Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
- Drupal Content Management Platform Has Been Chosen By Ulitzer
- Drupal Creator Forms Company
- Hot Banana Sponsors Gilbane White Paper On Using Web CMS
- Nastel to Exhibit at SYS-CON's SOA World Conference & Expo
- Drupal CMS to Move to a Whole New Level
- Hot Banana Is First To Add Web Site Optimization, Marketing Automation To Its Web CMS
- Hot Banana Unveils Multilingual Web CMS Module
- Ektron's CMS400.NET Now Supports Macromedia ColdFusion, Microsoft ASP, JSP, and PHP
- Ulitzer’s Amazing First 30 Days in Public Beta
- Magnolia 2.0 Becomes First CMS to Support the Java Content Repository
- Hot Banana Grows Web CMS Client Base






























