fugee279 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to watermark jpegs on the fly without having to create a new watermarked image file I just want it to display on the users browser as a watermarked image Can I do that?

Replies are listed 'Best First'.
Re: watermarks with Image::Magick
by zentara (Cardinal) on Nov 26, 2011 at 10:17 UTC
    See Image and Text Watermarked Letters. For CGI purposes, just write to stdout as shown in the code below.
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $image = Image::Magick->new; my $wimage = Image::Magick->new; my $rc = $image->Read('2uni2.jpg'); # base image $rc = $wimage->Read(shift); # image to be used as watermark $rc = $image->Composite(gravity => "Center", compose=>'Dissolve', image => $wimage, opacity => '10%', tile => 1, ); binmode STDOUT; $image->Write('png:-'); # or to file for testing #$image->Write("$0.png"); exit;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: watermarks with Image::Magick
by jethro (Monsignor) on Nov 26, 2011 at 02:04 UTC

    There is a way if you are on linux. You would have to write a filesystem driver (which actually isn't that difficult, see http://fuse.sourceforge.net/) and have that filesystem transparently watermark the jpgs.

    But probably you were hoping for an easier solution. Here is an untested idea: You could use the apache module mod_rewrite (if you are using apache that is) to change all jpeg links to call a script instead, i.e.:

    http://yourserver/a/b/thing.jpg # gets rewritten as http://yourserver/watermarkscript.cgi?file=/a/b/thing.jpg

    Then just write the watermarkscript.cgi that outputs the right watermarked image to STDOUT

Re: watermarks with Image::Magick
by TJPride (Pilgrim) on Nov 26, 2011 at 06:22 UTC
    I think he wants the watermarked images to be generated as opposed to having to be manually created.

    To answer the OP: There seem to be a number of different ways to overlay or blend images. This is probably the most appropriate solution:
    http://www.imagemagick.org/Usage/compose/#blend_use

    Feed the images through a script, as suggested, but you only need to return the image location, not pass through the entire image, and have that location be to the watermarked version. If none can be found (-e $basepath), have the script blend one using the non-watermarked version + your watermark image.

    I'd supply specific code, but I'm not very familiar with this particular library, I generally do my image manipulation with GD in PHP (ironically, the ImageMagick site is also written in PHP...)