Using ColdFusion 8's ImageGetBlob Function with GIF's
I responded to a question yesterday on the adobe forums that reminded me of a problem I had with ImageGetBlob a few weeks ago. I was surprised to discover the function does not seem to work with GIF images. Now perhaps I overlooked it, but I do not remember the documentation mentioning that ImageGetBlob does not support GIF's. Anyway, when I tried using ImageGetBlob on a GIF image ColdFusion returned this error
An error occured in converting the image to base64 in the required image encoding. Base64 images can be created with ColdFusion in BMP,JPEG,PNG,PNM,and TIFF formats
The solution I came up with was to use the ImageIO class to write the data to a ByteArrayOutputStream and then use the toByteArray() method to grab the image bytes.
<cfscript>
// read in a sample image
pathToImage = ExpandPath("MyGIFImage.gif");
imageExtension = listLast(pathToImage, ".");
img = ImageNew( pathToImage );
// extract underlying BufferedImage
buffered = ImageGetBufferedImage(img);
// write out image bytes
ImageIO = createObject("java", "javax.imageio.ImageIO");
outStream = createObject("java", "java.io.ByteArrayOutputStream").init();
ImageIO.write( buffered, imageExtension, outStream );
imageBytes = outStream.toByteArray();
</cfscript>
Note, this method requires that a compatible image writer is registered. You can view the informal format names understood by the registered writers using static methods of the ImageIO class.
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<cfdump var="#ImageIO.getWriterFormatNames()#">
<cfdump var="#ImageIO.getWriterFileSuffixes()#">
While this works, I would be interested to hear if anyone else has come up with a different or simpler method.
8 comments:
I don't think it's a GIF issue. I bet you made the image 'virtually'. Ie, you made a new image with imageNew and did not point to an original image file.
When you do that - you can't get the binary crap out.
It is a known bug. Your work around is just like the one Jason Delmore told me about, and I blogged about it today.
Well, I did use ImageNew() but passed in the path to an original image file. Is that what you mean?
What is strange is it does work for a jpg, just not for GIF's. I am not sure about PNG's.
<!--- this works --->
<cfset jpg = ImageNew( ExpandPath("example.jpg") )>
<cfdump var="#ImageGetBlob(jpg)#">
<!--- this does not --->
<cfset gif = ImageNew( ExpandPath("googleLogo.gif") )>
<cfdump var="#ImageGetBlob(gif)#">
Damn! I just read your most recent blog entry, now that you pointed it out. Someone is always beating me to the punch ;) I actually submitted a question about this to livedocs a few weeks ago, and again this week, but did not get a response or approval yet. So I let it slide. I am really going to have to start writing this stuff up as soon as it comes to mind ;)!
Heh, I hear ya.
By the way, it still seems to me like it is related to GIF's. I say that because ImageGetBlob works for me with jpg, png, bmp just not GIF's.
Though I suppose using <cffile action="readBinary" ..> like the adobe forum poster did would work too.
Did you ever get this to work with gif. The ImageGetBlob works with jpg but not gif.
@bj,
No. While I cannot definitively say the problem was ImageGetBlob and not my source images, I did have problems with a _lot_ of gif's, which makes me suspicious ;-)
I ended up using the work-around posted above -or- the simpler option:
<cffile action="readBinary" ..>
To extract the image bytes.
-Leigh
You're my hero. I've been dealing with this problem for a few hours and just now realized that Gifs didn't fail until the ImageGetBlob() part.
Great find and great workaround!
To confirm your problem just look at the special notes on writeToBrowser. It is in fact an issue with gif support as gifs used with writeToBrowser are output as PNGs not gif.
"You cannot display a GIF image in a browser. GIF images are displayed in PNG format." http://www.cfquickdocs.com/cf8/#cfimage
@Drew,
Glad it helped.
Yes, it does have a several issues with GIF's. Especially in the area of transparency. But it has always seemed a little strange to me that the image functions can do the conversion of GIF's to PNG format, but not grab the bytes? Oh well. At least there is a work-around ;)
-Leigh
Post a Comment