Image Manipulation CFC

I was tired of putting so much work to get pictures posted, and my options for third party tools are somewhat limited cause I’m just a wee customer on this host. (I still like them, I just like to do stuff for myself.)

So I went looking for a cfc that would allow for image manipulation. I figured someone must have figured out how to get Java objects to do it. I found Alagad Image Component, which seemed like both functional and cost overkill. But after more digging I found tmt_img.cfc.

Tmt_img does basically everything you need to crop and resize images, so I built a little picture display interface around it so I never have to crop or shrink an image for preview ever again. It reads in an image from one location, checks to see if a preview image has been created for it, and if not create it. It handles the linking, and the sizing, and the writing display html for it. I’ve included my image display cfc below.

<cfcomponent hint=”Handles displaying images.”>

<cfinvoke method=”init”>

<cffunction access=”private” name=”init” output=”false” returntype=”void” hint=”Sets the initial variables. “>
<cfset image_folder=”d:path_to_your_fileswwwrootimages”>
<cfset tn_folder= image_folder & “tn”>
<cfset image_url=””&gt;
<cfset tn_url= image_url & “/tn”>
<cfobject component=”tmt_img” name=”imageObj”>

<cffunction access=”public” name=”linkImage” output=”true” returntype=”void” hint=”Show a preview image and link of an image.”>
<cfargument name=”image” required=”yes” type=”string” hint=”The name of the image to preview and link to.”>

<cfset file_type=Right(arguments.image, 3)>
<cfset file_name=Left(arguments.image, Len(arguments.image)-4)>
<cfset tn=Replace(arguments.image, “.#file_type#”, “_tn.#file_type#”, “ALL”)>

<cfdirectory directory=”#tn_folder#” action=”list” name=”directoryList” recurse=”no”>

<cfquery name=”tnFile” dbtype=”query”>
select count(*) as tnExists
from directoryList
where name=’#tn#’

<cfif Len(tnFile.tnExists) lt 1 or tnFile.tnExists eq 0>
<cfinvoke component=”#imageObj#” method=”resize” returnvariable=”imageProperties”>
<cfinvokeargument name=”source” value=”#image_folder##arguments.image#”>
<cfinvokeargument name=”destination” value=”#tn_folder##tn#”>
<cfinvokeargument name=”newWidth” value=”200″>

<cfinvoke component=”#imageObj#” method=”getDimensions” returnvariable=”imageProperties”>
<cfinvokeargument name=”imgPath” value=”#tn_folder##tn#”>

<a href=”#image_url#/#arguments.image#”>
<img src=”#tn_url#/#tn#” alt=”#file_name#” width=”#imageProperties.width#” height=”#imageProperties.height#”>



2 thoughts on “Image Manipulation CFC

  1. I hade a copy of TMT_IMAGE for a while but i cold never figure out how to make it to work using a input field of type file. I had a look at your code, its nice, but how do you go about calling your component from a form page?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s