Trimming An Image Canvas With ImageUtils.cfc ColdFusion Image Component

Posted February 27, 2008 at 9:17 AM by Ben Nadel

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:

  • <!--- 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.




Reader Comments

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

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


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
Jun 19, 2013 at 9:31 PM
Filter vs. ngHide With ngRepeat In AngularJS
@Mike, Honestly, in the majority of cases, I would say there isn't going to be a difference. Both approaches have trade-offs. If you use the filter, then you have fewer DOM elements and fewer $scop ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools