Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

CFImage Part 3

Watermarks and transparency

Resize the transparent image to be the size of the target canvas. This will result in a larger transparent canvas.

--->
<cfset ImageResize(
     objWatermark,
     190,
     30
     ) />

<!---

Set text drawing anti-aliasing to be on. We're going to be writing text and then blurring it, so we want to let it be a little fuzzy.

--->
<cfset ImageSetAntialiasing(
     objWatermark,
     "on"
     ) />

<!--- Set the drawing color to be white. --->
<cfset ImageSetDrawingColor(
     objWatermark,
     "##FFFFFF"
     ) />

<!--- Create text attributes. --->
<cfset objAttributes = {
     Font = "Arial Black",
     Size = "20"
     } />

<!--- Draw the water mark text. --->
<cfset ImageDrawText(
     objWatermark,
     "Kinky Solutions",
     11,
     20,
     objAttributes
     ) />

<!--- Blur the watermark image. --->
<cfset ImageBlur(
     objWatermark,
     6
     ) />

<!---

Set text drawing anti-aliasing to be on for the target image. Since the image we are pasting in, we want a slight blurring.

--->
<cfset ImageSetAntialiasing(
     objImage,
     "on"
     ) />

<!---

Paste the manually created watermark image onto the photo. This time, we don't need to turn on any anti aliasing since the watermark has solid borders. The anti-alisasing only helps us when the pasted image has anti-aliased perimeter.

--->
<cfset ImagePaste(
     objImage,
     objWatermark,
     (objImage.GetWidth() - objWatermark.GetWidth()),
     (objImage.GetHeight() - objWatermark.GetHeight())
     ) />

<!--- Write the image with the new watermark to the browser. --->
<cfimage
     action="writetobrowser"
     source="#objImage#"
     />

Notice that the image we are reading in is the Base64 data:

R0lGODlhAQABAIAAAP///////yH5BAEHAAEALAAAAAABAAEAAAICTAEAOw==

This is the encoded 1x1 transparent GIF. We're also including the headers in the Base64 data, but this isn't required (it works if you take it out); I left it in just for demonstration purposes. Because we're using Base64 data rather than an actual GIF image, it lets us create transparent canvases without having a physical image on hand.

Running the above code, we get:
Clearly this is a hack. I'm sure that ColdFusion 8 has a way to do this that's more elegant, but for now, until I can figure it out, this is a rather simple workaround. I hope, however, that you're beginning to see how amazing the ColdFusion 8 image manipulation functionality is.

•  •  •

This article was reprinted with permision from Ben Nadel's blog:
www.bennadel.com/blog/775-Learning-ColdFusion-8-CFImage-Part-III-Watermarks-And-Transparency.htm

More Stories By Ben Nadel

Ben Nadel has worked with ColdFusion for eight years and is a super ColdFusion enthusiast. He blogs regularly about all aspects of Web development on his personal site, http://www.bennadel.com, and does his best to give back to the ColdFusion community through online code demos and his "Ask Ben" blog posts. He is also a Certified Advanced ColdFusion MX7 developer and is one of the lead programmers at Nylon Technology.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.