Skip to main content
Participant
July 19, 2008
Question

Generate image with CFIMAGE - but need to use img tag to serve the content

  • July 19, 2008
  • 1 reply
  • 555 views
I create a new image with cfimage, that already works without any problems.

But instead of using the action="writetobrowser" I need to be able to use the script inside a img tag:

Example: <img src="myscript.cfm?somedata=iwanttheimagetocreate>

I found one webpage that deals with that problem ( http://www.webdevelopernews.com/2007/09/14/serving-up-cfimages-via-image-tags/) but the image served to the browser, when using the code below, is of a very low quality and another point is that you can't serve .gif's to the browser.

<cfcontent type="image/jpg" variable="#ImageGetBlob(backgroundimage)#">

Any suggestions/solutions?

Thanks,
Phil
    This topic has been closed for replies.

    1 reply

    Inspiring
    July 19, 2008
    In that entry Raymond mentions:

    "The WriteToBrowser action actually generates HTML that points to a ‘magic’ URL to serve the image.". ie <img src="/CFFileServlet/_cf_image/_cfimg-12345036853711072.PNG" alt="" />

    Perhaps you could create a function that captures the generated html by using <cfsavecontent>.

    <cfsavecontent variable="imageTagHTML">
    <cfimage action="writeToBrower" ..>
    </cfsavecontent>

    Then uses a regular expression to extract and return the url portion only. ie "/CFFileServlet/_cf_image/_cfimg-12345036853711072.PNG"
    ...
    <cfset ExtractedURLHere = SomeFunction(arguments...)>
    <cfoutput><img src="#ExtractedURLHere#"></cfoutput>

    Though you could also use the entire html string directly

    <cfoutput>#imageTagHTML#</cfoutput>