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.

Categories

Database / SQL
Images

Comments

comments powered by Disqus