The ImageRead function performs the same operation as the cfimage read action . However, you cannot use the cfimage tag to read and create. You can use the cfimage tag to perform common image manipulation operations as a border; captcha; convert; info; read; resize; rotate; write; writeToBrowser. The other day, I needed to do some on-the-fly conversion of PNG image files to JPG format in ColdFusion. This was part of stop-gap measure at.
|Published (Last):||25 December 2005|
|PDF File Size:||8.89 Mb|
|ePub File Size:||4.35 Mb|
|Price:||Free* [*Free Regsitration Required]|
Finally, ColdFusion 8 has incorporated image manipulation directly into the ColdFusion tag and scripting language. No more are we, as developers, chained to third-party products. These products, while excellent in quality, just mean adding one more level of complexity to any application that requires server-side image manipulation.
Now, with CFImage and an abundance of image-related functions, ColdFusion has done to image manipulation what it has done to almost every other aspect of Web applications development – it has made it simple.
Due to the wide array of image functionality in ColdFusion 8, I am going to try and break this tutorial up into several small and manageable parts. The first part will cover basic reading and writing of images using CFImage and the related image manipulation functions. But, before we get into that, let’s just quickly touch upon these new features as a whole. ColdFusion 8 has given us CFImage.
CFImage provides us with tag-based access to only a small subset of the image functionality including:. In addition to the CFImage tag, ColdFusion has introduced dozens of new image manipulation functions including:.
Most of you are not going to care about this one all that much, but for those of you comvert are interested in the underlying Java methods of the coldfusion. Image object, I have listed them below skip past this if you have no idea what I’m vfimage about. If you choose to use them, you do so at your own risk and discretion.
For those of you who took a look at the underlying Java methods, you can see that there’s a lot of stuff wrapped up in these image objects. The ColdFusion 8 CFImage tag and the related image manipulation functions probably provide some sort of facade that wraps around the underlying methods.
For more information about what these cnovert as they are undocumentedtry exploring the java. Ben Forta stated at the New York ColdFusion Users group that Adobe has built the ColdFusion image manipulation around this library, but that they have also greatly extended it to work around the limits that this library holds.
One of the most exciting features of the new ColdFusion 8 image manipulation is the large number of file formats that can be read in and written out. To give you insight into what is available, ColdFusion 8 has provided two methods: These return cfimags lists of the file types that ColdFusion can deal with.
I don’t know how they handled that one since I thought Prodigy owned the patent on that or something sillybut it’s good to see that we have a huge variety of image formats that we can read vfimage write to. Now that we have a general overview of what kind of new and ccfimage image manipulation features are available in ColdFusion 8 and how many image types can be utilized, let’s get into the meat of this part of the tutorial: To start with, let’s examine reading images using the CFImage tag.
When reading in convsrt image, the source of the image can be any one of cfmage following:. Did anyone else see “URL” and get mentally turned on? In the first example we are using the CFImage tag in conjunction with an absolute path name to the image.
This will read in the binary image into an object of coldfusion. Dump out this struct. This reads color information, source value, and dimensions into a struct.
Converting an image to grayscale using the CFIMAGE tag in ColdFusion | ColdFusion Examples
Running the above code, we get the following CFDump output shown in Figure 1. The other input types give a similar output, so I won’t convwrt the CFDump, however, I will walk through the code. The next one on the list is the Web-relative path:.
This gives us the same output, but the source attribute is “http: It’s not just important that the resultant data is image data, it’s also important that the last thing in the URL be the file name. For example, using a URL with this query string:. The com image format is not supported on this operating system. This will only work to direct cimage URL access. This cfi,age like a bug to me as I would have assumed it was just doing a binary grab via CFHttp or something like thatbut I guess there is more to it than I can understand.
I would rather it try to grab the resultant data and throw an exception if the read fails. This will work quite nicely. A bit of a hack, but it works if you need it to. The next input type is a ColdFusion image object. We don’t have to perform actions just on new images; we can perform them on existing image objects:.
I am grouping these two together because I am sure what the difference really is. To me, they are both binary objects I think. I don’t generally cifmage with these sorts of objects:. The next input type is a Base64 encoded string.
This doesn’t mean the target file is a Base64 data text file – this means the actual input value is Base64 image data:. To get Base64 image data, we can convert the binary read we did above. I don’t deal with Base64 encoded images all that often, but this seems like something that could be very cfimqge when put into clever hands. When you xonvert in an image in this fashion, the source attribute is empty.
The Base64 data does not require headers to be used. For all the above “Read” demos, we were really just getting the info about the image. If you want to actually read the image into a ColdFusion variable in the form of the coldfusion. Image data typeuse the action “READ”:. While this reads the image into memory, if you CFDump it out, you’ll get the same output as if you were dumping out the INFO action result.
Now that we have covered the tag-based reading, let’s examine reading in images using the new ColdFusion 8 convet functions. To start with, let’s look at ImageRead.
When it comes to functions, if you want to read in Confert image data, you have to use the ImageReadBase64 function:. In addition to these straightforward methods, ColdFusion 8 can also use the ImageNew function to read in images. Like convrt methods and tags above, ImageNew can also take a variety of source types:.
We have looked at most of these types above. The only one here that stands out is the Java buffered image. Convett 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:. This kind of functionality convsrt be super useful if you are interfacing with another Java component that handles image manipulation using the java.
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 cfimagf source attribute is so flexible. Here, we are going to read an image cfimag a URL and save it to disk:.
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. 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 conver.
This also demonstrates our next mode of image writing: Notice that to do this, I am specifying the source of the image the image object we created and the output format of JPG.
Now, when I first saw this action, I had assumed it was working the same way CFContent worked – by streaming the conert to the browser as the only returned content. However, WriteToBrowser actually returns the image inline to the page. If you cfimagee at the source of the page with the rendered inline image, you will see something like this:. ColdFusion is actually writing your file to some sort of temporary image storage and then serving it up the way any other image or file would be served up.
But what is CFFileServlet? If I look in the root of my ColdFusion 8 test account, there is no such directory. This is some sort of public mapping, but realize this – this is not a ColdFusion request; this is an image file request.
Image file requests go the Web server, not the ColdFusion application server. This makes me nervous. I think it’s an awesome feature, but I’m not sure I like having to rely on mappings and tying in with settings external to the ColdFusion application server.
On the flip side, however, I do like this for the very reason that the image request is not going to the ColdFusion server. Storing an image to a cfmiage directly and then serving it using the Web server is going to be 10 times more efficient. I would like to know more about how CFFileServlet works and specifically how often that directory is cleaned out.
I don’t want to start writing a lot of images to the browser only to find out that it is clogging up this temp directory. When it comes to writing images using ColdFusion 8 image functions, there are basically two options: ImageWrite take three arguments potentially:.
As you can see, since we are grabbing the image via a URL, we must include the destination value. I have also included the JPG quality value, but this was not necessary as that value defaults to. It takes four arguments:. Just a final note on paths; cfimagf Web-relative path e. Therefore, if you are in an included template, you might get files stored in unexpected places if you don’t fully grasp this concept.