Learning ColdFusion 8: CFImage Part II - Tag Based Image Manipulation

<cffunction
	name="AddImageInfo"
	access="public"
	returntype="any"
	output="false"
	hint="Takes the image, duplicates it, adds info text to it (graphically), and then returns the resultant image.">
 
	<!--- Define arguments. --->
	<cfargument
		name="Image"
		type="any"
		required="true"
		/>
 
	<!--- Define the local scope. --->
	<cfset var LOCAL = {} />
 
	<!---
		Copy the image. We don't want to mess up the original
		since the text will ruin it. Here, we have to use the
		ImageCopy() function since Duplicate() will not copy
		the underlying buffered image (at least that's what
		I assume is why Duplicate() doesn't work).
	--->
	<cfset ARGUMENTS.Image = ImageCopy(
		ARGUMENTS.Image,
		0,
		0,
		ARGUMENTS.Image.GetWidth(),
		ARGUMENTS.Image.GetHeight()
		) />
 
 
	<!---
		ASSERT: At this point, we have copied the actual
		graphical data into our new image with ImageCopy().
		This has NOT copied over any of the environmental
		setting (color, antialiasing, etc). We have a clean
		slate in which we must set our own properties.
	--->
 
 
	<!---
		Now that we have a fresh image, let's build up the
		attribute collection that we are going to use when
		writing the text. This can define the font family,
		size, and style. This CANNOT define the font color.
		That must be set using ImageSetDrawingColor() which
		is done farther below.
	--->
	<cfset LOCAL.Attributes = {
		font = "Courier New",
		size = "14",
		style = "plain"
		} />
 
	<!---
		Since our text might be on crazy colored images, we
		are going to write the text first in black for a drop
		shadow, and then we are going to write it in white
		sligtly above that.
	--->
 
	<!--- Create an offset value for the text. --->
	<cfset LOCAL.Offset = 0 />
 
	<!--- Loop over the two color choices. --->
	<cfloop
		index="LOCAL.Color"
		list="##000000,##FFFFFF"
		delimiters=",">
 
		<!--- Set the current color. --->
		<cfset ImageSetDrawingColor(
			ARGUMENTS.Image,
			"#LOCAL.Color#"
			) />
 
 
		<!---
			We want the drop shadow text to be a little bit
			fuzzy (it's a drop shadow). Therefore, if we have
			a zero offset (first go), turn antialiasing on.
		--->
		<cfif LOCAL.Offset>
 
			<!--- Clean text. --->
			<cfset ImageSetAntialiasing(
				ARGUMENTS.Image,
				"off"
				) />
 
		<cfelse>
 
			<!--- Fuzzy text. --->
			<cfset ImageSetAntialiasing(
				ARGUMENTS.Image,
				"on"
				) />
 
		</cfif>
 
 
		<!--- Write the width overlayed on the image. --->
		<cfset ImageDrawText(
			ARGUMENTS.Image,
			("Width: " & (ARGUMENTS.Image.GetWidth() & "px")),
			(10 - LOCAL.Offset),
			(20 - LOCAL.Offset),
			LOCAL.Attributes
			) />
 
		<!--- Write the height overlayed on the image. --->
		<cfset ImageDrawText(
			ARGUMENTS.Image,
			("Height: " & (ARGUMENTS.Image.GetHeight() & "px")),
			(10 - LOCAL.Offset),
			(38 - LOCAL.Offset),
			LOCAL.Attributes
			) />
 
		<!---
			Increment the offset so that the white text
			will be up and left one of the drop shadow.
		--->
		<cfset LOCAL.Offset++ />
 
	</cfloop>
 
 
	<!--- Return the updated image. --->
	<cfreturn ARGUMENTS.Image />
</cffunction>

For Cut-and-Paste