Add a watermark to your images

August 10, 2009 by nuno costa 3 comments

Watermarking

This is another highly requested feature.
To watermark an image you must have already an watermark source, so phMagick can overlay the source image and the watermark image.

1
2
    $p= new phMagick('original.png', 'destination.png');
    $p->watermark('watermark-source.png', phMagickGravity::Center, 60);

This will create an image with watermark-source.png overlaid over source.png, centered and with  60% opacity

Let’s see a more complex example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    $format = new phMagickTextObject();
    $format->fontSize(14)
           ->font('Arial.ttf')
           ->color('#000')
           ->background('none');
 
    $p = new phMagick('', 'watermark-source.png');
    $p->fromString(html_entity_decode('© 2009 my-site.com ',null, 'utf-8'),$format)
      ->rotate(-45);
 
 
    $p->setSource('source.png');
    $p->setDestination('destination.png');
    $p->watermark('watermark-source.png', phMagickGravity::Center, 90);

This example creates an image named watermark-source.png containing the text “© 2009 my-site.com” rotated 45 degrees
And then watermarks source.jpg with the rotated text image

 

watermakr.png

 

Another cool watermark effect

 

1
2
3
4
5
6
7
8
9
10
11
12
    $format = new phMagickTextObject();
    $format->fontSize(14)
           ->font('Arial.ttf')
           ->color('#000')
           ->background('white');
 
    $p = new phMagick('', 'watermark-source.png');
    $p->fromString(html_entity_decode('© 2009 my-site.com ',null, 'utf-8'),$format)
 
    $p->setSource('lime.jpg');
    $p->setDestination('destination.png');
    $p->watermark('watermark-source.png', phMagickGravity::NorthEast, 60);
 
watermark2

 

Please note that in a real life web site you can skip the code to create the watermark source image has you only need to do it once, it’s included here so you can try it out
Your real code should be similar to this

1
2
    $p= new phMagick('original.png', 'destination.png');
    $p->watermark('watermark-source.png', phMagickGravity::NorthEast, 60);

 

3 comments so far Add Your Comment

  1. by Matthew Fries on March 16 2010 at 22:18

    Is there a way to designate where this script is looking for the font? Apparently my server is not keeping fonts in the default location.

    Thanks for your excellent php class!! Wow!

  2. by nuno costa on March 16 2010 at 22:30

    Hi Matthew,

    You can pass the full font file path in the phMagickTextObject::font() function

    please see http://www.francodacosta.com/blog/phmagick/examples/drawingtext (“Using a custom format” section) for more details

    hope this helps

  3. by Fioner on April 21 2010 at 16:00

    Hy, (from France ;-) )

    Wonderfull works my mannnnnnnn….

    Just one thing :

    I try to resize and have a watermark on the same image :

    it’s works for 80% of my ‘photos tests’..but 2 of them have never the watermark….

    I try to understand why…. but no answers….

    Finally : I do this :

    $p= new phmagick($lien, $lien);
    $p->resize($tab_size[0],$tab_size[1]);

    usleep(20000);

    $p= new phmagick($lien, $lien);
    $p->watermark(‘../../rep/picts/watermark.jpg’,phMagickGravity::SouthEast, 60);

    And everything all right ;-)

    (this is one of my photos test witch doesn’t want to be resize and watermark in the sme time :
    http://oseb79.free.fr/images/Nature,%20animeaux/monuments%2005.jpg … but i don’t think that error is coming from the picture…)

    THanks a lot for this LIB … again …. PEACE

More from francodacosta.com

© francodacosta.com - All rights reserved