Tuesday, February 09, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Feb 2010 >>
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28            

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (88) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (215) [RSS]
 • Appearances (190) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1371) [RSS]
 • Data Services (33) [RSS]
 • Fish Tank (4) [RSS]
 • Flash (179) [RSS]
 • Flex (493) [RSS]
 • Home Automation (4) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (109) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (19) [RSS]
 • SQL (40) [RSS]
 • Stuff (532) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (161) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
June 6, 2005

Serving Images From Databases

When images are associated with database records, those images are usually stored on disk. The images are related to their respective records by storing their names in a table or perhaps by using the record's identifier as part of the file name. The advantages of storing images as files are performance (no database access needed to retrieve the file, and likely nothing but straight HTTP access), and flexibility.

But sometimes you may indeed need to store images within database tables (perhaps as blobs). Storing the images is easy enough, serving them dynamically is a little more complex. You can't simply use the column name in the IMG SRC attribute as you would static image files, although ColdFusion would indeed embed the image data within the tag, the browser would not display the image as intended.

When browsers render pages containing images they do so by making multiple requests, first for the page itself, and then for each image, making an HTTP request to obtain the URL named in the IMG SRC tag. But that URL need not be an actual file, it can also be a dynamic URL that returns an image.

Therefore, you can create a .cfm file that does nothing more than serve images. The code would work like this:

<!--- Determine image to retrieve --->
<cfset ...>

<!--- Get image --->
<cfquery datasource="mydsn" name="image">
SELECT imageData
FROM table
WHERE ...
</cfquery>

<!--- Set MIME type to GIF or JPEG or ... --->
<cfcontent variable="image.imageData" ... >

This file would be saved on the server, and the URL to it (with the image identifier passed as a URL parameter) can now be used as an IMG SRC like this:

<img src="/getImage.cfm?imageid=123">

And that'll do it.

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
Sorry to necro this thread from the grave, but...

<img src="..."> is not working for me.
I have my main file 'testdhs.cfm':
<cfquery name="rs1" datasource="dhsdb">
SELECT IMAGE
FROM dbo.CASEIMG
WHERE IMAGE_ID = #URL.imageid#</cfquery>
<cfcontent type="image/tiff" variable="#rs1.IMAGE#">

Then, in the page that will display the tiff,
<img src="/cms_temp/testdhs.cfm?imageid=0" />

This page displays the broken image icon "red X". However, when I check the properties of the broken image, it displays the proper URL, which when copied and pasted into the browser's address bar, opens a dialog box asking if I want to open or save the .tif . If I open it, a viewer pops up and displays the image I wanted.

So, why doesn't the img src work? Do I need to include something else on that page?
# Posted By Tony | 7/22/09 3:40 PM
Nevermind, just read that IE won't support TIFF without a plugin.

I have decided to use OBJECT tags to utilize QuickTime. The TIFF now displays in my browser.

If anyone else has a cleaner, better way to display TIFF's inside a browser, post away. thanks
# Posted By Tony | 7/22/09 4:29 PM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved