Skip to main content
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Angela Buraglia and Dan Short
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Angela Buraglia Dan Short

ColdFusion 8 ImageResize() / ImageWrite() Bug?

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

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

Reader Comments

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

17 Comments

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!

15,811 Comments

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

2 Comments

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.

2 Comments

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

2 Comments

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.

2 Comments

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>

17 Comments

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

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

23 Comments

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

23 Comments

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

9 Comments

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

17 Comments

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

9 Comments

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

2 Comments

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.

2 Comments

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

15,811 Comments

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

6 Comments

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!

15,811 Comments

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

6 Comments

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

15,811 Comments

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

6 Comments

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.

15,811 Comments

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

6 Comments

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

15,811 Comments

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

6 Comments

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

15,811 Comments

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

1 Comments

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.

1 Comments

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

15,811 Comments

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

26 Comments

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.

2 Comments

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

23 Comments

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

1 Comments

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