Avg. Rating 5.0

Problem

You need to store an image, but perhaps only have access to a database and not a file system. There are many examples of this now with cloud architecture becoming more popular.

Solution

Turn the image in to a ColdFusion Image object so you can manipulate the image if needed, and then turn it back into a BLOB using ImageGetBlob().

Detailed explanation

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.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes