I am a web developer, born in PERU and now living in PDX. My platform of choice is CFML but I'm always open to learn anything. I currently work for Mentor Graphics.

I also do freelance web development and have a few open source projects that I maintain.

Where To Find Me:

CFImage and PJPEG Images

January 26, 2008

I am currently porting a website from classic ASP into CF8 and one of the features that this site had was an automatic generation of thumbnails; so I used the wonders of CFImage (and some caching know-how to decrease the performance hit) to implement this, but then I ran into a very annoying limitation of CFImage, namely the inability to handle certain jpeg images.

For some images, whenever I attempted to read them from the file system using CFImage I would get the following error back:

Not a JPEG file: starts with 0x42 0x4d
An exception occurred while trying to read the image.

The weird thing was that I could see these images in the browser without a problem, and they even appeared as regular jpeg images. Then I remembered about progressive jpeg images, which is a particular type of JPGs but are designed to be displayed as if the image quality increases as it is being loaded.

However pjpeg is not one of the readable types by ColdFusion, so after some tinkering around, I found an interesting thing and I'm not sure if this is a bug in CFImage or not: Reading one of these images using will throw the error mentioned above, BUT if I created the image using imageNew() passing the binary contents of the image file then CF creates the image object without complain!

So I modified my thumbnail generation script so that whenever it finds one of these unreadable JPG images, it will then read the binary contents of the image, create a new image using the image data and then overwrite the source image with the newly created image, resulting in a normal JPEG image that can now be read without problems.

The relevant part of the script is this:

<cfif not isImageFile(expandPath(href))>
   <!--- image is not a valid image file (for CF), may be a pjpeg file,
      so we will attempt to convert it to a readable type --->

   <cffile action="readbinary" file="#expandPath(href)#" variable="strFile">
   <cfset oImage = imageNew(strFile)>         
   <cfimage action="write" destination="#expandPath(href)#" source="#oImage#" overwrite="yes">

href is the location of the image in the site. Hopefully someone will find this useful.

Posted on January 26, 2008      11 Comments

Aaron Longnion's Gravatar Aaron Longnion ...
Oscar! You're my hero! I was pulling my hair out trying to figure out how this could be happening... my issue was with GIF images implicitly converted to JPG during cffile UPLOAD, but your code worked for me, too. Thank you!
February 20, 2008 04:31 PM
Simon Bingham's Gravatar Simon Bingham ...
Oscar, Thank you very much for your code. I have modified it slightly so that it can be dropped into a CFC.

<cffunction name="fixProgressiveJPEG" access="public" returntype="void">
<cfargument name="source" type="string" required="true" />
<cfargument name="filename" type="string" required="true" />

<cfset var oImage="" />

<cffile action="readbinary" file="#arguments.source##arguments.filename#" variable="oImage">

<cfimage action="write" destination="#arguments.source##arguments.filename#" source="#ImageNew(oImage)#" overwrite="yes" />
August 14, 2008 12:16 PM
Scott's Gravatar Scott ...
Thanks to you, I still have some hairs in my head. I was getting down there. Thanks for your help.
October 25, 2008 03:37 PM
neal's Gravatar neal ...
Nice fix...thanks so much!
November 21, 2008 01:01 PM
Greg Feenstra's Gravatar Greg Feenstra ...
Thanks, Oscar. This solution worked great for pjeg files. Appreciate you taking the time to document.
October 12, 2009 12:46 PM
shane's Gravatar shane ...
Oscar, thanks for the fix - I am running into this issue with images that have CMYK color profiles a well. I have a client site which allows users to upload photos which I then resize and it's bombing on these. I did try your fix for those but no luck.

Has anyone run into this, any suggestions?
February 03, 2010 04:11 PM
Paul's Gravatar Paul ...
@Shane Try using cfimage to save the CMYK file as a .png, then from png back to jpg. It's slow, but it worked here.
February 09, 2010 01:01 PM
Sam's Gravatar Sam ...
Nice post Oscar.

Although this was posted early 2008, I still found this useful when caching images from an XML feed in CF8.

Super stuff.
July 15, 2010 08:24 AM
Oscar's Gravatar Oscar ...
Thanks Sam, glad to be of use.
July 15, 2010 11:28 AM
Wouter De Bruycker's Gravatar Wouter De Bruycker ...
Old post but still usefull. Thanks!
July 29, 2013 03:08 AM
Alan Mackenzie's Gravatar Alan Mackenzie ...
Great post
Solved all my photo load problems in cf.
Thanks for the help!
January 28, 2014 07:58 AM

All comments are moderated to prevent spam
Leave a comment