Skip to main content
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Cameron Childress
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Cameron Childress ( @cameronc )

ColdFusion 8 ImageResize() / ImageWrite() Bug?

Published in Comments (45)

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. --->
	source="#ExpandPath( './100x100.gif' )#"

<!--- Resize image. --->
<cfset ImageResize(
	) />

<!--- Try to write the new image to disk. --->

	<cfset ImageWrite(
		ExpandPath( "./200x200.gif" )
		) />


		<!--- Dump out error. --->
			label="ImageWrite() Error"


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

	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.

Want to use code from this post? Check out the license.

Reader Comments


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...


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!



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.


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.


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?



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.


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

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


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

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


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

FileWrite("pictures\#cffile.serverfile#", "#b#");

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



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.



@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.



> 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)


@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.


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.


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



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.


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!



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.



I've heard 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.



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?


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.



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?



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?



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.



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.



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


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.


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">


somthing like this code nothing special.

any idea?




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?


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 )
doesn't help.

Using imageResize() instead of ScaleToFit works fine though.


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!



<!--- Get the image info from the existing image. --->

<cfdump var="#objImageInfo#">


Kindly share your thoughts.

Thanks & Regards,



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


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?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel