How to watermark an image with phMagick

August 9, 2009 by nuno costa 3 comments

phMagick :: Watermarks

It's easy to add watermarks to an image, you can set the postion and opacity of the overlaped image

I've seen a lot of confusion with this function, you need specify 3 files :

  • The source and destination in phMagick constructor
  • The watermark image (the first parameter in the function) this will be the image that will be placed above the source imace

Function reference

$phMagick->watermark($image_to_use_as_watermark, $watermark_placement, $opacity);

 

Watermarks examples

 

From an existing image

$phMagick = &new phMagick('original.png', 'destination.png');
$phMagick->watermark('watermark-source.png', phMagickGravity::Center, 60);
Original Image Watermark Source Result
Original Image Watermark Source  Result

 

Draw text and use it as watermark

//adding text format
$format = new phMagickTextObject();
$format->fontSize(10)
 ->font('Arial.ttf')
 ->color('#000')
 ->background('#f0f0f0');
 
//creating an image that will serve as watermak source
$phMagick = &new phMagick('', 'wm_text.png');
$phMagick->fromString(html_entity_decode('© francodacosta.com'), $format);
 
 
 
//watermarking image
$phMagick = &new phMagick('original.png', 'watermark.png');
$phMagick->watermark('wm_text.png', phMagickGravity::NorthEast, 50);

 

Original Image Watermark Source Result
Original Image  

 

 

A more complex example

$format = new phMagickTextObject();
$format->fontSize(12)
 ->font('Arial.ttf')
 ->color('#000')
 ->background('none')
;
 
$phMagick = &new phMagick('', 'wm_text1.png');
$phMagick->debug = true ;
$phMagick->fromString(html_entity_decode('© francodacosta.com '), $format)
 ->rotate(-45); //rotating text 45 degrees
 
$phMagick = &new phMagick('resize_porp_wh.png', 'watermark3.png');
$phMagick->watermark('wm_text1.png', phMagickGravity::Center,70);

 

Original Image Watermark Source Result
Original Image Watermark Source  Result

 

3 comments so far Add Your Comment

  1. by James on November 23 2010 at 11:30

    Hi,

    I want to be able to create a watermarked image like above but i don’t want to save it i just want to display it to the screen in the same way gd2 allows you to. also gd2 allows you to display the final image before saving it, using

    imagejpeg($image);

    is there a way to do this with phMagick….? gd2 is too slow when merging the images but phmagick is much faster, but we don’t want to save the final image. we just want to display it to the screen….

  2. by James on November 23 2010 at 13:07

    hmmm i have just tried to display the converted image in the browser and it is not seeing the image it just shows a red X…??? i have tried displaying other images and they work fine it’s only the converted images that wont display????

  3. by nuno costa on November 23 2010 at 18:01

    @James,

    ImageMagick, and therefor phMagick needs a physical file to exist, so unfortunately the answer is no, you can not work with images directly in memory.

    You might eventually create a temporary file and have a cron job remove those files every x minutes

More from francodacosta.com

© francodacosta.com - All rights reserved