4

I'm creating a website which requires the images in the site's Gallery to be watermarked. I'd prefer the watermark to get added programatically when the image gets uploaded to the application (via it's web interface) than to manually add it before we do the upload.

Are there any simple ways (perhaps an image library?) that will allow me to do this?

I'm building the site in Grails, so a Groovy or Java solution would be great.

Benny Hallett
  • 7,327
  • 4
  • 26
  • 26

5 Answers5

5

This is the code you want I think...

BufferedImage source = ...;
BufferedImage watermark = ...;
Graphics2D g = source.createGraphics();
g.drawImage(watermark, x, y, null);
g.dispose();
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
3

I'm not sure if copyright and your process allows but an alternative suggestion is to render the watermark when the images are displayed rather than altering the image on upload. This would allow you to change the watermark and have access to the raw image.

Dave Barker
  • 6,303
  • 2
  • 24
  • 25
  • 1
    Good point: don’t destroy the original. But you don’t want to superimpose the watermark on every download, you still want to do it when the image is uploaded. – Bombe Oct 21 '09 at 06:59
1

I've been working on an Grails application that does a lot of image manipulation and we chose to call out to ImageMagick. There is an Image Tools plugin for Grails which might be suitable for simple watermarking, however we needed the extra power that IM provides.

Groovy's Process additions make it pretty easy to call out to IM and then parse the err/out streams. If I had time I'd release an IM plugin, but I don't sorry!

cheers

Lee

leebutts
  • 4,882
  • 1
  • 23
  • 25
  • +1 for ImageMagick. I've evaluated multiple approaches on image manipulation and found IM to be the best one (performance, stability, quality). There is also a wrapper library for Java: http://im4java.sourceforge.net/ – Siegfried Puchbauer Oct 22 '09 at 09:55
  • It's kind of simple, so I'd rather avoid bringing in ImageMagick to do something I can do in < 10 LOC. But thanks for the idea. – Benny Hallett Oct 23 '09 at 01:04
1

Create a BufferedImage. Draw the uploaded image to the BufferedImage and then draw the watermark to the BufferedImage.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

If you are using grails, I think the easiest way to add a watermark or resize the image to use the burningimage plugin https://grails.org/plugin/burning-image

Its very easy to use and well documented. Find the documentation here: https://code.google.com/p/burningimage/

To the the watermark you simple have to do the following

burningImageService.doWith('path/to/my/file', 'path/to/output/dir')
               .execute {
                   it.watermark('path/to/watermark', ['top':10, 'bottom': 10])
                }
Aasiz
  • 647
  • 8
  • 18