Tuesday, May 6, 2008

ColdFusion 8.0.1: Post updater Image Function Issues

Once again, adobe forum member hans blix seems to be the unfortunate winner in the highly unofficial "Image function issue contest" ;-) He posted about two exceptions that seem to occur with certain JPEG images: Quantization table 0x02 was not defined and Missing Huffman code table entry . Another related issue was that image files seemed to be locked on error.

The good news is a hotfix should be released soon according to Kiran Sakhare of Adobe. So keep an eye out for it.

UPDATE: The hotfix was released on 05/07/2008. See Patch for CFImage and Image functions in ColdFusion 8.0.1

Note: The image hotfix was updated on 08/15/2008. The file name is exactly the same as the one released on 05/07/2008. So check the file timestamp to make sure you have the latest fix.

In the mean time, thanks to a posting by another forum member markthickpaddy, we came up with the following as a work-around. It is a combination of code from myself, markthickpaddy, framework from Rick Root's great ImageCFC and a Tech Tip from sun.com.

<cffunction name="ImageWriteReplacement" returns="void" access="public" output="false">
<cfargument name="image" type="any" required="true" hint="ColdFusion image object to save">
<cfargument name="destination" type="string" required="true" hint="Absolute path where you want to save the file">
<cfargument name="quality" type="numeric" required="false" default="0.75"
hint="Image quality for jpg,jpeg images only. Fractional value between 0 and 1 (highest quality). Default is 0.75">

<cfset var Local = {} >

<cfset Local.extension = trim( listLast( arguments.destination, "." ) )>
<cfset Local.buffered = imageGetBufferedImage( arguments.image )>
<cfset Local.outFile = createObject("java", "java.io.File").init( arguments.destination )>
<cfset Local.imageIO = createObject("java", "javax.imageio.ImageIO")>

<!--- confirm there is at least one registered writer for this image "format" --->
<cfset Local.iter = Local.imageIO.getImageWritersByFormatName( Local.extension )>
<cfif Local.iter.hasNext()>
<cfset Local.writer = Local.iter.next()>
<cfthrow message="No suitable image writers found for format: #Local.extension#">

<!--- If this is a JPEG file ... --->
<cfif listFindNoCase("jpg,jpeg", Local.extension)>
<!--- set the image quality --->
<cfset Local.param = Local.writer.getDefaultWriteParam()>
<cfset Local.param.setCompressionMode( Local.param.MODE_EXPLICIT )>
<cfset Local.param.setCompressionQuality( javacast("float", arguments.quality) )>

<!--- create a stream for the output image file --->
<cfset Local.outStream = createObject("java", "javax.imageio.stream.FileImageOutputStream").init( Local.outFile )>
<cfset Local.writer.setOutput( Local.outStream )>

<!--- convert the image to an IIOImage we can pass to the writer --->
<cfset Local.image = createObject("java", "javax.imageio.IIOImage").init( Local.buffered , javacast("null", ""), javacast("null", "") )>
<!--- save the image to disk and close the stream --->
<cfset Local.writer.write( javacast("null", ""), Local.image, Local.param)>
<cfset Local.outStream.close()>

<cfset Local.imageIO.write( Local.buffered, Local.extension, Local.outFile )>



  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep