troubleshooting Question

coldfusion-Image tag - check external image file

Avatar of Panos
PanosFlag for Germany asked on
ColdFusion Language
6 Comments1 Solution500 ViewsLast Modified:
Hello experts.
I want to show images using "external" images (images not on my server).
f.e the experts-exchange logo.
<img src="https://www.experts-exchange.com/images/experts-exchange/experts-exchange-logo.png"/>

This is OK. But the problem appears if f.e the image don't exist.
I thought that it would be nice to use this tutorial:
http://www.bennadel.com/blog/712-Using-ColdFusion-s-CFLocation-Tag-For-Inline-Image-SRC-Attributes.htm with a little change.
My image tag:
<img src="showimage.cfm?imageurl=https://www.experts-exchange.com/images/experts-exchange/experts-exchange-logo.png">

My showimage.cfm page:
(i added the urlexists function from this tutorial:http://www.forta.com/blog/index.cfm?mode=day&day=2&month=1&year=2006)
<cfsilent>
  <cffunction name="URLExists" output="no" returntype="boolean">
    <!--- Accepts a URL --->
    <cfargument name="u" type="string" required="yes">
    <!--- Initialize result --->
    <cfset var result=true>
    <!--- Attempt to retrieve the URL --->
    <cfhttp method="head" url="#ARGUMENTS.u#"
            resolveurl="no" throwonerror="no" />
    <!--- Check if no status code or status code 404 --->
    <cfif NOT IsDefined("cfhttp.responseheader.status_code")
            OR cfhttp.responseheader.status_code EQ "404">
        <!--- Does not exist, return FALSE --->
        <cfset result=false>
    </cfif>
   <cfreturn result>
</cffunction>
      <cfparam name="URL.imageurl" type="string" default="" />
    <cfset urlexist = urlExists(URL.imageurl) />
    <cfif urlexist EQ true>
      <cfset strURL = URL.imageurl />
    <cfelse>
      <cfset strURL = "image_not_found.gif" />
    </cfif>
    <!--- Forward image request to actual image file. --->
      <cflocation url="#strURL#" addtoken="false" />
</cfsilent>

That is checking if the requested page is throwing any error.
1.Well i don't know if this is the best way to do this.
2.It doesn't ensure the image output. The requested page could be a page like"This image don't exists" and i will not get any error.

Any help?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros