Suppose you have to display images in your page that come from another server.

There’s a way to do with validation, I mean, checking if the image is available or showing a default image to avoid the broken icon on the image place holder.

So, instead of a regular image tag, use the following in your code:

{code type=coldfusion}<img src=”getImage.cfm?imageURL=http://myRemoteURL/#imageToGet#”  width=”#myWidth# alt=”#myImageDescription#” />{/code}

The getImage.cfm template should contain:

{code type=coldfusion}<cfprocessingdirective suppressWhitespace=”true”>
<cfparam name=”url.imageURL” value=”” />
<cftry>
<cfhttp url=”#url.imageURL#” result=”imageData”></cfhttp>
<cfcontent type=”#imageData.mimeType#” variable=”#imageData.fileContent.toByteArray()#” />
<cfcatch type=”any”>
<cfcontent type=”image/jpg” file=”#expandPath(‘./default.jpg’)#” deletefile=”false” />
</cfcatch>
</cftry>{/code}

The cfcatch block will deal with the exception and display the default image in case of any error.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.