Verify remote images exist - prevent missing images with CFHTTP

I am creating a photo gallery where the images actually reside on a third-party service. To prevent 'blank' or missing images from showing up in my html gallery markup, I needed a way to verify the remote image actually exists before including it into the dynamic markup.

This seems to work quite nicely:



<cfoutput query="myImageQuery">

<cfset imgURL = "http://remotesite.com/images/#imageFilename#">
<!--- check for the image via cfhttp:
Note:'head' method gets only headers, which is all we need (thanks @ScottP) --->

<cfhttp url="#imgURL#" method="head">    
<!--- isolate the 'mimeType' value from the cfhttp results --->
<cfset myStatus = cfhttp.mimeType>
<!--- if the mimeType is jpg (jpeg), include the image in our output --->
<cfif myStatus eq "image/jpeg">
<img src="#imgURL#" alt="#imageTitle#">
</cfif>

</cfoutput>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Since you are only wanting to verify the url, you might consider changing method="get" to method="head". That will cause it to only retrieve the headers instead of actually getting the image.
# Author Scott P | 8/23/09 1:26 AM
@Scott: thanks, I did not know this! (obviously...)
# Author Michael Evangelista | 8/23/09 10:33 PM
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista