If you are using the ColdFusion image resize tag to size down images, you may occasionally run into an error during resizing. Although your web browser can render an image, ColdFusion may not be able to read the same image format (it may have an unrecognized color profile for example).

That being the case, it's always wise to wrap <cfimage> methods in <cftry>/<cfcatch>.

It may seem obvious, but in the event that an image didn't resize properly, be sure to provide the <img> width and height attributes in your HTML source. This will ensure that in the event <cfimage> isn't able to resize the image, the browser will do the resizing. Not an optimal solution as browser resizing is never as "clean", but at least your page layout will remain consistent.

It often makes sense to define a constant for image width and height, which can be used in both the <cfimage> resize operation, as well as the <img> tag attributes:

<cftry>
    <cfimage action="resize" source="myimage.jpg"
             width="#application.strConfig.imageThumbWidth#" height="#application.strConfig.imageThumbHeight#">
<cfcatch>
    <!--- do nothing, we don't want this to halt processing, the browser will handle the resize --->
</cfcatch>
</cftry>

<cfoutput>
<img src="myimage.jpg" alt=""
  width="#application.strConfig.imageThumbWidth#"
  height="#application.strConfig.imageThumbHeight#" />
</cfoutput>

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *