Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at BFusion / BFLEX 2009 (Bloomington, Indiana) with:

ColdFusion 8 ImageResize() / ImageWrite() Bug?

Posted by Ben Nadel
Tags: ColdFusion

Boris just pointed out a peculiar problem to me. I have been playing around with it, and it appears to be a bug. When you use ColdFusion 8 to resize a COMPLETELY transparent GIF image and then try to write it back to the file system, ColdFusion throws the following error:

An exception occurred while trying to write the image. Ensure that the destination directory exists and that Coldfusion has permission to write to the given path or file. cause : java.lang.NullPointerException

Here is the code that produces this error:

  • <!--- Read in 100x100 transparent GIF image. --->
  • <cfimage
  • action="read"
  • source="#ExpandPath( './100x100.gif' )#"
  • name="objImage"
  • />
  •  
  • <!--- Resize image. --->
  • <cfset ImageResize(
  • objImage,
  • 200,
  • 200
  • ) />
  •  
  • <!--- Try to write the new image to disk. --->
  • <cftry>
  •  
  • <cfset ImageWrite(
  • objImage,
  • ExpandPath( "./200x200.gif" )
  • ) />
  •  
  • <cfcatch>
  •  
  • <!--- Dump out error. --->
  • <cfdump
  • var="#CFCATCH#"
  • label="ImageWrite() Error"
  • />
  •  
  • </cfcatch>
  • </cftry>

I started to narrow down the things that could be causing it. I replaced the ImageWrite() method with the CFImage write tag:

  • <cfimage
  • action="write"
  • source="#objImage#"
  • destination="#ExpandPath( './200x200b.gif' )#"
  • />

... but this gets the same error. The only thing that would fix it was actually removing the ImageResize() method. But why? What's going on?

Previously, I stated that the image was a COMPLETELY transparent GIF. This means that it was a transparent canvas that didn't have a single colored pixel on it - nothing, not one piece of non-transparent image. That seems to be what is breaking it. If I place even a single 1x1 black square in the middle of the vast, transparent canvas, the code works perfectly fine. It seems that when ColdFusion 8 goes to resize a completely transparent canvas, something goes funny.

Seems like a bug to me.



Reader Comments

Hi,
I'm resizing a gif image having a transparent index using cfimage action="resize"... , and it looks like that index disappears once resized. May be this is part of the same bug, or may be I'm missing something here to conserve the index...

Reply to this Comment

Well I was reviewing some issues on a project that will soon be in production. One of which I've titled "lost of transparency index on resizing gif image" and I came stright to your fantastic site while Googleing about it.
I've discovered your site just a couple of months ago when I just started to play with CF8 during Scorpio beta testings and I have to say, your site stands, together with CFjedi and Ben Forta on top 3 position of my favorite CF related bookmarks.
Thank you for sharing, and please continue the great work!

Reply to this Comment

@Gov,

That really means a lot to me to hear stuff like that :) Thank you very much for the kind words. Please do not hesitate to ask me anything if you get stuck somewhere.

Reply to this Comment

The imageresize has a additional bug:
If you resize a jpg of 443/600 (w/height) to new 73px height, you will get an image of 72 px !
the imagescaletofit does the same result.

Seams to be a rounding problem.
Actual the imageresize and imagescaletofit - functions aren´t useful.

hope for a hotfix on that and a bad problem on captcha - creations.

Reply to this Comment

Christian, I'm having a similar problem. When using ImageResize it sometimes is 1px less than what I want it to be (or tell it to be for that matter).

You said they weren't useful, is there another solution?

Thanks

Reply to this Comment

I now calculate the whole measurements by myself. Then do a normal imageresize; so it´s possible to get an image of correct measurements...

A bit ugly; the CF-documentation is also not correct; when you call the function with a string ("") as width or height you get an error message.

Reply to this Comment

I also noticed that when you resize a transparent gif you lose transparency, does this happen for you?

<code>
<cfimage action="resize" destination="thumbnail.gif" height="25%" overwrite="true" source="myLargeImage.gif" width="25%">
</code>

Reply to this Comment

After the newer cfimage related hotfixes is this still an issue?

either the completely transparent gif,
and or gif looses transparency?

Reply to this Comment

Although i am not that experienced as you guys are are.

but i made it to work by just undocumented feature getimagebytes to write the file to my folder:

i did something like this:

<cfset objimage = imagenew()>
<cfset a = imageresize(objImage,200,200)>

then using

<cfset b = a.getimagebytes("jpg")>

write using

<cfscript>
FileWrite("pictures\#cffile.serverfile#", "#b#");
</cfscript>

the above code may be little incorrect but it works as i have copied/pasted and it did make some changes. accordingly.

Cheers

Reply to this Comment

@Gavy,

Because you are writing the image as a JPG, you might be bypassing the underlying GIF issue.

Reply to this Comment

You might be right, but when i tried to write the image to the folder with the simple imagewrite function or cfimage action="write" functionality, i encountered many errors, so i did something like above with some help and it solved.

only issue remains, that when user upload gif file then how will it go. still guessing hope you have have a better alternative of doing this.

Cheers

Reply to this Comment

@Gavy.. I need to give your method a try as I experience the error "java.lang.NullPointerException" when attempting to save a resized image using ImageResize() or cfimage resize regardless of transparency. I have applied the cfimage hotfix on my localhost and my hosting company has confirmed they have installed it too. My ImageResize() code works great on my localhost on Mac OS X, but fails on the hosting provider. I can't seem to get around it. I'll see if i can get the FileWrite() method to work. Thanks again.

Reply to this Comment

@Tim,

> hosting company has confirmed they have installed it too. My ImageResize() code works great on my localhost...

Well I guess there's only 2 things to assume here...

Who's right and who's wrong :P

(Working = right)

Reply to this Comment

@Steve - Definitely. Code that works should just work everywhere. I think it's still a bug that didn't get fixed by the CFImage Hotfix. It appears that when I read the image into a variable it locks the file which prevents the image write from working.

Using @Gavvy's method of fileWrite() saved the day though.

This doesn't explain this weird issue that still persists.

Reply to this Comment

If I call imagenew to create an image object and then call imageresize to resize it, ColdFusion never releases the file lock on the original image.

I have to restart coldfusion to delete the image I passed in via absolute path to imagenew.

Its a jpeg, not a gif. Definitely seems like a bug to me.

Reply to this Comment

Quick follow up to my above comment, if I use the cfimage tag using action=read it works just fine.

Reply to this Comment

@Dan,

That's really frustrating. File locking issues are such a pain. Probably a bug, but glad you got it working with the tag-based implementation.

Reply to this Comment

I ran into this issue today, Code works fine locally but on my hosting provider I get the null pointer exception.

I've tried both functions and tag based implementations with the same result :(

The odd thing is, this just started happening. My client has been live for months with it working properly and suddenly one day it stops.

Can anyone recommend an alternate library to use for creating thumbnails?

Ben, great site!

Reply to this Comment

@Shane,

Is it possible they just updated their ColdFusion version or applied a patch / hotfix? That might cause the sudden change. Or, is it possible that the client recently started producing images in a different way (ie. using a different Graphics program to create images)?

I know these are not answers, but they might be questions that lead to a better answer.

Reply to this Comment

@Ben,

I've heard hosting.com is notorious for denying applying patches that affect the server in a negative way, so this is definitely a possibility.

It is CF8.02 so it is patched.

I thought it might be a specific type of JPEG issue as well but when images I have previously uploaded stopped working that was odd. So at this point, no GIF, JPG, TIFF or anything works. Just continue to get the Java null pointer exception. :(

It's frustrating as hell.

Reply to this Comment

@Shane,

That's crazy that none of those image types work - that is not a good sign. Do all image actions break? Or resizing specifically? Can you read images?

Reply to this Comment

Man this is strange, now it's back to working on most images (after sending 3 emails to hosting company and not getting a reply), but some JPEGS are still throwing an error.

Reply to this Comment

@Shane,

That is odd! If you can still read images, then you might be able to read an image in and paste it into a *new* image object before manipulating it. That might help?

Reply to this Comment

@Ben,

It appears if I open up said image in Photoshop and re-save it and re-upload it works. This leads me to believe CF is having problems with certain JPEG encoding.

Any recommendations?

Reply to this Comment

@Shane,

I've definitely had that problem before, both with CFImage and with 3rd party products. I never came up with a great solution to it; I ended up doing error checking in the processing and then outputting a message to the user like:

"There was a problem reading your image. Try re-saving it."

I know, not the greatest, but I was stumped.

Reply to this Comment

@Ben,

Did you ever try converting it to Binary first? I remember someone posting something about that. Yea I am doing error handling right now but that's not good enough for client :( which I understand.

Reply to this Comment

@Shane,

The image should already be a binary file. Yeah, it's a bad situation that I don't have a great fix for, sorry.

Reply to this Comment

Hi there.
I encountered some problems with images resizing.
When I tried this,

<cfset myImage = ImageNew("#WebPath#/myFile.jpg")>
<cfset ImageResize(myImage, theWidthIWant, "")>
<cfimage action="write" destination="#UNCPath#/myFile.jpg" source="#myImage#" overwrite="yes">

I had a Pretty NullPointerException, with only 20 percent of my jpeg files, and I tried this:

<cfset myImage = ImageNew("#WebPath#/myFile.jpg")>
<cfset myImage = imageCopy(myImage, 0, 0, ImageWidth, ImageHeight)><!--- cheating to avoid the bug --->
<cfset ImageResize(myImage, theWidthIWant, "")>
<cfimage action="write" destination="#UNCPath#/myFile.jpg" source="#myImage#" overwrite="yes">

And it works with all files.

I hope my contribution was useful.

Reply to this Comment

i have something another problem, which i have like 40 images around 5-7 MB each image for resizing. The problem come out when i batch resize those images, it is hang on 100MB images processed, and 5 mins between first image until the last one success processed. and I have kill the process in the monitor otherwise it run forever.

<cfloop ...... >

<cfimage source="#ImageSrc#" action="resize" width="" height="100" destination="#ImageDescr#_thumbnail.jpg" overwrite="yes">

</cfloop>

somthing like this code nothing special.

any idea?

Thanks

Reply to this Comment

@Leo,

Hmm, nothing great to suggest here. Do the images eventually get processed - is it just the request that hangs? Or the do the images fail to process completely?

Reply to this Comment

@Marc

I know in my case the tag based implementation resolved the file locking bug.

Reply to this Comment

Hi Ben; did you log this in the tracker ? Do you know if CF9 fixes it ?

We just got bit on CF8, and doing this:
myImage = imageCopy(pdfObj, 0, 0, imageInfo(pdfObj).width, imageInfo(pdfObj).height)
ImageScaleToFit( myImage, 350,"", 'highestPerformance', 1 )
imageWrite(myImage,"#name#.gif")
doesn't help.

Using imageResize() instead of ScaleToFit works fine though.

Reply to this Comment

@george interesting, i didnt see that listed for that hotfix. Does something similar apply to 9?

Reply to this Comment

@George,

Thanks for the update. I should probably check to see if I have the latest hotfixes in place.

Reply to this Comment

Hi Ben,

When I tried run the below code, I am getting error message like -

An exception occurred while trying to read the image.
javax.imageio.IIOException: Can't get input stream from URL!

Code

********************************
<cfimage
action="READ"
source="http://www.uqtr.ca/biblio/images/uqtr_logo_150.gif"
name="objImage"
/>

<!--- Get the image info from the existing image. --->
<cfimage
action="INFO"
source="#objImage#"
structname="objImageInfo"
/>

<cfdump var="#objImageInfo#">

************************************

Kindly share your thoughts.

Thanks & Regards,

Arvind.G

Reply to this Comment

Hi ben, tell me what is wrong with this Code,

<cfset imageName = "">
<cfset myNewLogo = ImageNew("",'25','4',"argb")>
<cfset ImageWrite(myNewLogo,"#Expandpath('s_logo\empty.gif')#")>
<cfset myLogo = 'empty.gif'>

It works on my Local with CF9 latest and in my Host Address CF8, it throws the error you discussed above, i am doing a resize or anything here

Reply to this Comment

We have a similar issue with CF 9.01. CFIMAGE worked pretty well for 6 month and now all of a sudden stopped working. I never saw a "a piece of code" getting lame. I would have sworn it is a digital world. But apparently not.

We haven't done anything to the server at all and all of a sudden all code with CFIMAGE crashes :( Anybody any idea?

Reply to this Comment

Post A Comment

?
You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.