Repeating Watermark with ColdFusion & Java

Let's make some watermarks with Java. This one's been in the backlog of my mind for a good while now. It's really a one situation tool, but it was cool as hell to dig into and figure out how to do it.

I had to apply a watermark on a pretty large sum of images. ColdFusion makes this fairly easy. In the given scenario though, it wasn't just a watermark; but a repeating watermark on a 45 degree. Still fairly simple with CFML I'm sure (?), but I didn't go that route. I called on some Java classes instead. In the past, I've had questionable experiences with ColdFusion's image functions when doing heavy lifting on a large amount of images that might need to be resized and altered in more than one way. To be honest, I'm not sure how today's engine performs. I use Lucee these days and I couldn't speak for how it's functions stand up either. Something to compare in the future I suppose.

So here's a component that spins up some magic...

In a nutshell, it takes a absolute path to a given image, the text to be layered over said image and a destination path (and name) for the result image, processes and creates a newly watermarked image.

So let's break this bad boy down.

For separation, I put all of my Java class calls in the init() function to be passed through the variables scope. File and ImageIO are used to create the Graphics2D class object that will be used to create and apply the watermark to. AlphaComposite, Color and Font are used with the result object from ImageIO to actually make the watermark. AffineTransform will help get the 45 degree turn to the watermark text.

Once I have the image object to work with, I create a alpha channel with a low opacity so it gets that faded appearance on the image. Set it as the composite and make the color white. With the AffineTransform object, pass in a radian value of the degrees I want to turn the text into the rotate() function. Finallym creating the font object and applying the rotation to the object.

I get the dimensions of the text and set the X and Y factors from our angle, then loop over the various coordinates generated from those and the dimensions of the actual image; drawing the text on each pass. That loop was honestly the hardest part for me. I'm not a trig guy by any means. Thankfully there is Google.

The last bit writes the image to the supplied location and clears out our watermark object.

In the end you get something like this...

Watermarked Image

Cool stuff. Here's some references I used to put it all together.

[ The example code can be found on GitHub here... ]

Happy coding.