Wish List

My Amazon.com Wish List

Tags

adobe air ajax cf community cfml coldfusion examples ext flex javascript max2007 misc programming technology ui

Recent Entries

One Bourbon, One Scotch and One Beer
Getting Closures: The Groovy Way
Adobe Photoshop Elements 7 Announced
Getting Started with Flex 3
Adobe Munich Sponsors Girl Geek Dinner

Currently Reading


Search

RSS


Subscribe

Enter your email address to subscribe to this blog.

Blogroll

An Architect's View
Ben Forta
CFSilence
Coldfusion Jedi
Rey Bango
TalkingTree



CFIMAGE Part 1

I was looking through the Coldfusion docs today, specifically at the cfimage tags and all the goodness that they bring to CF. I'm continually amazed at the level of integration that CF8 has with other Adobe products, specifically in the area of PDF's and Images.

So what exactly is the deal with cfimage anyway? and what can I do with it? were the questions that I thought needed answering. I posted earlier about how to create a captcha image using the image tag but now I decided it would be good to get back to basics, start at the beginning and go from there.

So lets start at the beginning.

How do we create an image?

Well as it turns out it's dead easy.

<cfset myImage =ImageNew("",100,100,"rgb","000000")/>
<cfimage source="#myImage#" action="writetobrowser">

Ok so that's useful we have a black 100x100 square...so what now.

Writing Text to an Image

We write text to an image using the ImageDrawText function which takes a few parameters. ImageDrawText(name, str, x, y, attributeCollection). Our previous code gets extended a bit.

<cfset myImage =ImageNew("",100,100,"rgb","000000")/>
<cfset ImageSetDrawingColor(myImage,"white")>
<cfset attr=StructNew()>
<cfset attr.size=14>
<cfset attr.style="bold">
<cfset attr.font="Arial">
<cfset ImageSetAntialiasing(myImage)>
<cfset ImageDrawText(myImage,"Hello World!",1,50,attr)>
<cfimage source="#myImage#" action="writetobrowser">
So now we set the drawing color to be white, what better color to show against black eh?

And then we create a structure to hold the text style, set anti aliasing on or our text gets pretty pixelated. We then call the ImageDrawText function followed by the cfimage with the "WriteToBrowser" action and voila we have a lovely black square with the words Hello World as seen below.

Note: We can also use the ImageNew function to read an existing image or use the cfimage tag to do the same thing and use the optional name attribute to save it to a variable.

Happy Coding...

Related Blog Entries

Comments