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

So what happens if you don't have an application like Adobe Fireworks to make your watermark image? All is not lost. You can still create a watermark image manually and then paste it in using a similar technique. For this next demo, we're going to create a second image using ColdFusion 8 that has a gray box with the text, Kinky Solutions, centered on it. Then just as we did in the previous example, we're going to paste it over the original image:

<!--- Read in the original image. --->
<cfset objImage = ImageRead( "./blue_eyes.jpg" ) />

<!---

Create a new image for the watermark with the given dimensions and give it a very light gray canvas color.

--->
<cfset objWatermark = ImageNew(
     "",
     100,
     20,
     "rgb",
     "##F0F0F0"
     ) />

<!---

Set the drawing color. This will be the color for all image manipulations going forward.

--->
<cfset ImageSetDrawingColor(
     objWatermark,
     "##666666"
     ) />

<!--- Draw the rectangle. --->
<cfset ImageDrawRect(
     objWatermark,
     0,
     0,
     (objWatermark.GetWidth() - 1),
     (objWatermark.GetHeight() - 1)
     ) />

<!---

Set text drawing anti-aliasing. This will give the text a smoother rendered look (rather than the jagged text you'd see on a Web page).

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

<!--- Create text attributes. --->
<cfset objAttributes = {
     Font = "Verdana",
     Size = "10",
     Style = "Italic"
     } />

<!--- Draw the watermark text onto our watermark image. --->
<cfset ImageDrawText(
     objWatermark,
     "Kinky Solutions",
     11,
     14,
     objAttributes
     ) />

<!---

When we paste the watermark onto the photo, we don't want it to be fully visible. So let's set the drawing transparency to 50% before we paste.

--->
<cfset ImageSetDrawingTransparency(
     objImage,
     50
     ) />

<!---

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 an anti-aliased perimeter.

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

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

Now instead of reading in a watermark image, we're creating an entirely new image canvas using ImageNew(), which takes the target image (left blank since we're creating it from scratch), the width and height, the image type, and the canvas color. For this image we use an RGB color set. This method can also take an ARGB color set, but the documentation isn't very clear on what that is. I assume that the "A" stands for Alpha, but I couldn't figure out how to make a transparent canvas (we'll discuss this later on).


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.