Art site: Where to put the images

CFIMAGE!! Woooo!!

I about 6 months behind on my ColdFusion, this was my first time to really use cfimage. So imagine my excitement. First, very simple form for selecting and uploading an image.

<cfform enctype="multipart/form-data">
   Image File: <cfinput type="file" name="ArtImage"/><br/>
   <cfinput type="submit" name="submit" value="Update Art"/>   
</cfform>

The action to be taken...

<cfif form.ArtImage neq "">
   <cffile action="upload"
         destination="#path#\"
         filefield="ArtImage"
         nameconflict="makeunique">
   
   
   <cfimage action="read" source="#path#\#cffile.serverfile#" name="ThisPicture"/>
   
   <cfif ThisPicture.width gt 800>
      <cfscript>
         ImageResize(ThisPicture,'800','');
      </cfscript>
   </cfif>
   
   <cfset myImage=ImageNew(ThisPicture)>
   
   <cffile action="delete" file="#path#\#cffile.serverfile#"/>

</cfif>

Step 1: Get the image into memory. This is done by using cffile, then cfimage to read it back out.

Step 2: Resize. A 1700 pixel wide image might get uploaded. But since it will never display 1700 pixels wide anywhere in the site, there is no need to save a 1700 pixel wide image.

At the end of this, the image is resized and sitting in variables.myImage, ready to be saved to the database. The field type in SQL Server 2k5 is "image"

<cfscript>
   newart = CreateObject("component","jwa.model.art.Art").init();
   if(isDefined('myImage') AND isImage(myImage)) newart.setArtImage(ImageGetBlob(myImage));
   
   variables.newartSaved = CreateObject("component","jwa.model.art.ArtGateway").save(newart);
</cfscript>

Then through some CFC magic, this object ends up at a cfquery, where the image is defined as a CF_SQL_BLOB.

<cfquery name="qCreate" datasource="#request.dsn#">
   insert into dbo.jwaArt(ArtImage)
   values (
      <cfqueryparam value="#getArtImage()#" cfsqltype="CF_SQL_BLOB"/>
   )
   select @@identity as ArtID
</cfquery>

And there you go, the images are in the database. It looked a lot harder than it turned out to be.

Next: Getting the images out

Related Blog Entries

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner