Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How can I store an image in a database?

To store an image in a database we must first turn the image in a BLOB or Binary Large OBject.

Basically this is just the binary data of the image in one entity that can then be stored nice and easily in one field of a DB.

Luckily CF8 comes with a little function called imageGetBlob() - below is a little example of how it can be used...

First we have to use <cfimage> to create a ColdFusion image variable..

<cfimage action="read"
name="myImage"
source="http://www.imagesite.site/myImage.jpg">

We can then use this variable with imageGetBlob()

<cfset binimage = imageGetBlob(myImage)>

The binImage variable can now be inserted into a DB to keep it nice and safe!

N.B. This is a binary variable, so any fields used to contain this sort of data should have a suitable data type.

To display this image have a look at this article - http://www.coldfusioncookbook.com/entry/130/How-to-serve-pictures-from-a-database


This question was written by Matt Evans.
It was last updated on July 8, 2008 at 9:11:42 AM EDT.

CFML Referenced

ImageGetBlob()
<cfimage>

Categories

Database / SQL, Images

Comments

Comment made by Chris H on July 8, 2008 at 9:22 AM
if all you want is to insert an image into your db you can skip a step and just use <cffile action="readbinary" file="path.to.file" variable="varName"> imagetGetBlob is just useful if you're already working with an image object and don't want to first write it to a file and then read it into a binary variable again.


Comment made by Mat Evans on July 8, 2008 at 9:25 AM
Yup, very true, as you say this gives you the ability to process an image and then turn the cf image variable into a binary object.


Comment made by Randy Johnson on July 8, 2008 at 12:52 PM
Are their benefits to storing the images in the DB rather than the file system?


Comment made by duncan on July 9, 2008 at 3:52 AM
I'd like to see a companion article: "Why would I store an image in a database?"


Comment made by ron on July 9, 2008 at 5:38 PM
- why? Couple of thoughts... The file system stores a limited number of attributes.. name, couple dates, archive status, size, that's about it. Not extensible. - db can store as many attribs as you want about that image. And they are all searchable and indexable and... and... and .... - this post has me thinking...


Comment made by Randy Johnson on July 15, 2008 at 9:44 AM
fyi, I did a search in google for why store images in a database and found an article from oracle that gives some valid reasons for storing images in a database: http://www.oracle.com/technology/products/intermedia/htdocs/why_images_in_database.html


Comment made by Jeff Knooren on August 8, 2008 at 11:37 PM
Yeah, you can store an image in a database, but should you? A lot of those Oracle arguments for the practice, aren't concrete enough to be convincing.

In what environment, or application, would you really need to set permissions on who can view images? Besides, no human searches binary data anyway. They search Keyword or Description fields.

Once the image is in a blob field, you can't edit it any more. I suppose that is the entire point. But somewhere, there is probably an original Photoshop image, with layers and whatnots. Whatever is in the database, is simply the JPG of this original document. So, it's not like storing images in blob fields eliminates the need for origianl documents, or backups of your file system.

Again, you CAN store images in blobs... so what? You can also drill woodscrews into your toes if you have nothing better to do.