ColdFusion 8 ImageResize() / ImageWrite() Bug?

Posted August 16, 2007 at 2:21 PM 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

Gov
Aug 28, 2007 at 5:35 PM // reply »
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...


Aug 28, 2007 at 5:46 PM // reply »
11,246 Comments

Very interesting. Might be related.


Gov
Aug 29, 2007 at 7:12 AM // reply »
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!


Aug 29, 2007 at 7:16 AM // reply »
11,246 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.


Feb 24, 2008 at 5:31 PM // reply »
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.


Mar 4, 2008 at 2:47 PM // reply »
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


Mar 6, 2008 at 8:53 AM // reply »
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.


Jun 25, 2008 at 6:26 PM // reply »
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>


Oct 2, 2008 at 1:44 PM // reply »
17 Comments

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

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


Apr 2, 2009 at 5:19 PM // reply »
1 Comments

@Steve

We had applied the cfimage 8.0.1 patch (kb403411), but the transparent gif problem wasn't resolved yet.

Does anyone have any news on this issue?


May 8, 2009 at 12:15 AM // reply »
19 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


May 8, 2009 at 8:19 AM // reply »
11,246 Comments

@Gavy,

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


May 8, 2009 at 10:47 AM // reply »
19 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


Aug 12, 2009 at 3:33 PM // reply »
1 Comments

@Gavy

Thanks for the tip, it helped me with my image write issues!


tim
Jan 13, 2010 at 5:44 PM // reply »
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.


Jan 15, 2010 at 11:40 AM // reply »
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)


tim
Jan 15, 2010 at 12:25 PM // reply »
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.


Dan
Jan 28, 2010 at 1:02 PM // reply »
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.


Dan
Jan 28, 2010 at 1:06 PM // reply »
2 Comments

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


Jan 28, 2010 at 10:24 PM // reply »
11,246 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.


Feb 2, 2010 at 4:04 PM // reply »
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!


Feb 2, 2010 at 6:56 PM // reply »
11,246 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.


Feb 2, 2010 at 7:00 PM // reply »
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.


Feb 2, 2010 at 7:02 PM // reply »
11,246 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?


Feb 2, 2010 at 7:15 PM // reply »
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.


Feb 2, 2010 at 7:17 PM // reply »
11,246 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?


Feb 2, 2010 at 7:17 PM // reply »
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?


Feb 2, 2010 at 7:20 PM // reply »
11,246 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.


Feb 2, 2010 at 7:22 PM // reply »
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.


Feb 2, 2010 at 7:25 PM // reply »
11,246 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.


Feb 2, 2010 at 7:54 PM // reply »
6 Comments

@Ben,

I added an isImageFile() function to the code and turns out the images that are bombing are not being registered as images in CF.

In photoshop they seem to have a CMYK profile as well, hmm.. I did run across this page, but didn't seem to help me out.http://www.oscararevalo.com/index.cfm/2008/1/26/CFImage-and-PJPEG-Images


JP
Feb 23, 2010 at 10:16 AM // reply »
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.


Feb 23, 2010 at 9:06 PM // reply »
11,246 Comments

@JP,

Oh cool - thanks for sharing that with us.


Leo
Jun 22, 2010 at 7:15 AM // reply »
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


Jun 29, 2010 at 10:58 AM // reply »
11,246 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?


Jul 15, 2010 at 3:20 PM // reply »
1 Comments

The writes occur, but then the process hangs in my case.


Dan
Jul 15, 2010 at 3:29 PM // reply »
5 Comments

@Marc

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


Nov 16, 2010 at 10:51 AM // reply »
8 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.


Jun 1, 2011 at 4:02 PM // reply »
3 Comments

Ok, my issue is not with GIFS but I was getting an equivalent error.

Steps to solve issue:
1. obtain "chf8010002.jar"
http://www.adobe.com/support/coldfusion/ts/documents/kb403781/chf8010002.zip

2. drop the jar in C:\ColdFusion8\lib\updates

3. restart cf services.

worked for me

twitter @cfdatabase


Dan
Jun 2, 2011 at 11:03 AM // reply »
5 Comments

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


Jun 3, 2011 at 10:49 PM // reply »
11,246 Comments

@George,

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


Aug 30, 2011 at 3:43 AM // reply »
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


Jun 20, 2012 at 10:30 AM // reply »
19 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


Jun 20, 2012 at 10:33 AM // reply »
19 Comments

i think i resolved, i used RGB mode and it worked, but it failed with "ARGB"


Jul 21, 2012 at 3:49 AM // reply »
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?



Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools