CFImage and PJPEG Images
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:
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
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:
<!--- 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">
</cfif>
href is the location of the image in the site. Hopefully someone will find this useful.
<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" />
</cffunction>
Has anyone run into this, any suggestions?