Avg. Rating 5.0
Tags:



Problem

How can I use ColdFusion to draw a complex shape and then add text to that shape.

Solution

Using ColdFusion built in functions we will take a set of x,y coordinates and create an image and then place text on that image. We will use ImageNew, ImageDrawLines, and ImageDrawText amongst others. Keep in mind the image functions are available in ColdFusion version 8.0 or higher.

Detailed explanation

To be able to draw a polygon or complex shape with ColdFusion, you first need a canvas to draw on.  In this example we will create a 21X23 pixel canvas using the ImageNew function built into ColdFusion.

<cfset myImage = ImageNew("", 21,23,"argb") />

The arguments that are available to the ImageNew function look like this.

ImageNew([ source, width, height, imageType, canvasColor])

In this example we created an image called "myImage" with a source of an empty string "", a width of 21, a height of 23, and an imageType of "argb".  The "argb" image type will give us a white canvas and let us draw any "RGB" colors on it.

Next, we will set the Anti-Aliasing option on our image so we can draw nice smooth lines on our image using the ImageSetAntialiasing function.

<cfset ImageSetAntialiasing(myImage) />

Then we will set our drawing color using the ImageSetDrawingColor function.  In this example, we will set it to black.

<cfset ImageSetDrawingColor(myImage,"000000") />

Great, we now have a canvas and are all setup to draw.  In this example, I want to take a list of x,y coordinates that you might get from any software that allows you to draw an image map and use those to draw our image.  This is nice because it allows you to start with any complex shape, get the coordinate list easily, and then be able to adjust that shape dynamically using ColdFusion.  To do the drawing, we will use the built in function ImageDrawLines.

ImageDrawLines( name, xcoords, ycoords [, isPolygon, filled])

The ImageDrawLines function expects the x and y coordinates as an array of coordinates, so to get this we will need to "massage" our list of image map coordinates and convert them into two arrays.  Here is the code we will use to do that.

        <cfset coordlist = "2,1,20,1,21,2,21,17,20,18,16,18,11,23,6,18,2,18,1,17,1,2,2,1" />
<cfset arXcoords = ArrayNew(1) />
<cfset arYcoords = ArrayNew(1) />
<cfset xpos = 0 />
<cfset ypos = 0 />
<cfset i = 0 />
<cfloop list="#coordlist#" index="cord">
   <cfset i = i + 1 />
   <cfif i mod 2 eq 1>
      <cfset xpos = xpos+1 />
      <cfset arXcoords[xpos] = cord />
   <cfelse>
      <cfset ypos = ypos+1 />
      <cfset arYcoords[ypos] = cord />
   </cfif>
</cfloop>
 
  
 
Now that we have our arrays of coordinates, we can draw our shape on the canvas we created.
 
        <cfset ImageDrawLines(myImage,arXcoords,arYcoords,"yes","yes") />
 
  
 
Next, we want to be able to add some text to our shape.  To begin, we need to first switch our drawing color to a different color, we will use white for our example.
 
        <cfset ImageSetDrawingColor(myImage,"FFFFFF") />
 
  
 
To do the drawing, we will use the ImageDrawText function.
 
ImageDrawText( name, str, x, y [, attributeCollection])

Before we draw the text, lets setup a struct to set some attributes we want to use for our text such as font face, size, and style.

        <cfset attributes = {Font = "Verdana",Size = "10",Style = "bold"} />
 
  

Then, lets draw some text on our image.  In this example we will draw "Hi" since the ColdFusion community is always so friendly.

        <cfset ImageDrawText(myImage, "Hi",5,13, attributes) />
 
  

You can see we placed the text on our "myImage" at the x coordinate of 5 and the y coordinate of 13.  This is to "roughly" center the text on the image.

Now, all that is left is to output our image to the browser.

        <cfimage action="writeToBrowser" source="#myImage#" />
 
  

Here is all the code used in this example.

        <!--- Create the image canvas and set our drawing attributes --->

<cfset myImage = ImageNew("", 21,23,"argb") />

<cfset ImageSetAntialiasing(myImage) />

<cfset ImageSetDrawingColor(myImage,"000000") />

<!--- Set our list of x,y coordinates and covert them to arrays of coordinates --->

<cfset coordlist = "2,1,20,1,21,2,21,17,20,18,16,18,11,23,6,18,2,18,1,17,1,2,2,1" />

<cfset arXcoords = ArrayNew(1) />

<cfset arYcoords = ArrayNew(1) />

<cfset xpos = 0 />

<cfset ypos = 0 />

<cfset i = 0 />

<cfloop list="#coordlist#" index="cord">

   <cfset i = i + 1 />

   <cfif i mod 2 eq 1>

      <cfset xpos = xpos+1 />

      <cfset arXcoords[xpos] = cord />

   <cfelse>

      <cfset ypos = ypos+1 />

      <cfset arYcoords[ypos] = cord />

   </cfif>

</cfloop>

<!--- Draw the shape --->

<cfset ImageDrawLines(myImage,arXcoords,arYcoords,"yes","yes") />

<!--- Set our drawing attributes for drawing some text --->

<cfset ImageSetDrawingColor(myImage,"FFFFFF") />

<cfset attributes = {Font = "Verdana",Size = "10",Style = "bold"} />

<!--- Draw the text --->

<cfset ImageDrawText(myImage, "Hi",5,13, attributes) />

<!--- Output the image to the browser --->

<cfimage action="writeToBrowser" source="#myImage#" />
               
 
  

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes