Trimming An Image Canvas With ImageUtils.cfc ColdFusion Image Component

Posted February 27, 2008 at 9:17 AM

Tags: ColdFusion

I'm not gonna bother posting the code here, since it's in the ImageUtils.cfc RIA Forge project, but I created two new methods for trimming a ColdFusion image canvas:

TrimCanvas( Image, BackgroundColor [, Tolerance ] )

This will take a ColdFusion image object and, given the background color, it will reduce the size of the canvas to the smallest possible rectangle without hurting any of the primary image data. You can pass in an optional tolerance value. The tolerance allows some flexibility when it comes to matching the current pixel color to the given background color. The tolerance is the allowable delta in any given color channel (0 to 255).

TrimCanvasSide( Image, Side, BackgroundColor [, Tolerance ] )

This will trim just the given side of the canvas. In reality, TrimCanvas() internally calls this method four times - once per side.

These are SLOW functions. This should not be something that is done on the fly, but rather something that is done to prepare a future-static image. That being said, let's take a look at some sample code:

 Launch code in new window » Download code as text file »

  • <!--- Create the image utils object. --->
  • <cfset objImageUtils = CreateObject(
  • "component",
  • "imageutilsroot.imageUtils"
  • ).Init()
  • />
  •  
  •  
  • <!--- Create a large, off-white canvas. --->
  • <cfset objImage = ImageNew(
  • "",
  • 300,
  • 300,
  • "argb",
  • "##F5F5F5"
  • ) />
  •  
  • <!--- Set the drawing color. --->
  • <cfset ImageSetDrawingColor(
  • objImage,
  • "##FF9900"
  • ) />
  •  
  • <!--- Draw a circle in the middle of the canvas. --->
  • <cfset ImageDrawOval(
  • objImage,
  • 100,
  • 100,
  • 100,
  • 100,
  • "yes"
  • ) />
  •  
  • <!--- Set up our font properties. --->
  • <cfset objFont = {
  • Size = "20",
  • Font = "Arial Black"
  • } />
  •  
  • <!--- Draw text. --->
  • <cfset ImageDrawText(
  • objImage,
  • "MY FIRST CIRCLE",
  • 45,
  • 250,
  • objFont
  • ) />
  •  
  •  
  • <p>
  • <!--- Write image to browser. --->
  • <cfimage
  • action="writetobrowser"
  • source="#objImage#"
  • />
  • </p>
  •  
  •  
  •  
  •  
  •  
  • <!--- Trim the canvas. --->
  • <cfset objImage = objImageUtils.TrimCanvas(
  • objImage,
  • "##F5F5F5"
  • ) />
  •  
  • <p>
  • <!--- Write image to browser. --->
  • <cfimage
  • action="writetobrowser"
  • source="#objImage#"
  • />
  • </p>

In this demo, we are creating a canvas with a centered circle and some text. Then we are outputting it. Then, we are trimming the canvas and outputting the smaller canvas. Running the above code, we get the following output:


 
 
 

 
Trimming ColdFusion Image Canvas - Before Trimming  
 
 
 

 
 
 

 
Trimming ColdFusion Image Canvas - After Trimming  
 
 
 

As you can see, the excess "background color" was trimmed away from our original image leaving the smallest possible canvas that contained all of the primary image data. Again, this is a very SLOW function. This demo took several seconds to run and the image that we are working on is rather small. This is not something you are going to want to run all the time. Hopefully, I can think of a better, faster way to do this.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page





Reader Comments

Aug 17, 2009 at 2:38 PM // reply »
14 Comments

I haven't been able to get this method to work. I've posted an issue regarding it in Riaforge.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 7:43 AM
Terms Of Service / Privacy Policy Document Generator
Thankyou for this very helpful form. You've made my life much easier today. I'll have a look around your site... I'm sure there's some more good stuff here..Thanks Dave ... read »
Mar 22, 2010 at 7:21 AM
Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct.
I got this exception now. In case you're using var-es local struct, CF gives you couple of "new" exceptions: Encountered "local. and Encountered "id. Incorrect Select List, Incorrect select colum ... read »
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »