How to add a Watermark to an image with Imagick in PHP

Basically, all that you need to add a watermark to an image is the compositeImage method from an image object. This method allows you to easily composite one image onto another.

<?php
// Create instance of the original image
$image = new Imagick();
$image->readImage("image.jpg");
// Create instance of the Watermark image
$watermark = new Imagick();
$watermark->readImage("watermark.png");
// The start coordinates where the file should be printed
$x = 0;
$y = 0;
// Draw watermark on the image file with the given coordinates
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
// Save image
$image->writeImage("image_watermark." . $image->getImageFormat()); 

 The coordinates given in the method are totally up to you, as your wish of watermark could be different (on the entire image or just in a corner of the image). The Watermark file should have obviously transparency with format .png otherwise the watermark will clearly overlay your original image.

Examples

In the following examples we’ll show examples of how of how to add a watermark to an image using the following watermark (the watermark could be logo of Our Code World too):

Watermark Draft Imagick

For the image we’ll use a copyright free happy goat:

Goat Imagick without Watermark

Nice isn’t ?

Full size watermark
Note

We use the getcwd function of PHP to provide an absolute path (retrieve current working directory) to Imagick as it usually doesn’t work with relative paths like ../file.png. According to the way you work (using a framework or plain PHP), the way in which you provide an absolute path to a file may be vary so heads up !

<?php
// Open the image to draw a watermark
$image = new Imagick();
$image->readImage(getcwd(). "/goat.jpg");
// Open the watermark image
// Important: the image should be obviously transparent with .png format
$watermark = new Imagick();
$watermark->readImage(getcwd(). "/draft_watermark.png");
// Retrieve size of the Images to verify how to print the watermark on the image
$img_Width = $image->getImageWidth();
$img_Height = $image->getImageHeight();
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Check if the dimensions of the image are less than the dimensions of the watermark
// In case it is, then proceed to
if ($img_Height < $watermark_Height || $img_Width < $watermark_Width) {
    // Resize the watermark to be of the same size of the image
    $watermark->scaleImage($img_Width, $img_Height);
    // Update size of the watermark
    $watermark_Width = $watermark->getImageWidth();
    $watermark_Height = $watermark->getImageHeight();
}
// Calculate the position
$x = ($img_Width - $watermark_Width) / 2;
$y = ($img_Height - $watermark_Height) / 2;
// Draw the watermark on your image
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
// From now on depends on you what you want to do with the image
// for example save it in some directory etc.
// In this example we'll Send the img data to the browser as response
// with Plain PHP
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
// Or if you prefer to save the image on some directory
// Take care of the extension and the path !
// $image->writeImage(getcwd(). "/goat_watermark." . $image->getImageFormat()); 

Which would produce with our images the following output in the browser (or if you decided to save it into a file):

Goat Watermark Image PHP Imagick

Right bottom corner watermark
<?php
// Open the image to draw a watermark
$image = new Imagick();
$image->readImage(getcwd(). "/goat.jpg");
// Open the watermark image
// Important: the image should be obviously transparent with .png format
$watermark = new Imagick();
$watermark->readImage(getcwd(). "/watermark_file.png");
// The resize factor can depend on the size of your watermark, so heads up with dynamic size watermarks !
$watermarkResizeFactor = 6;
// Retrieve size of the Images to verify how to print the watermark on the image
$img_Width = $image->getImageWidth();
$img_Height = $image->getImageHeight();
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Resize the watermark with the resize factor value
$watermark->scaleImage($watermark_Width / $watermarkResizeFactor, $watermark_Height / $watermarkResizeFactor);
// Update watermark dimensions
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Draw on the bottom right corner of the original image
$x = ($img_Width - $watermark_Width);
$y = ($img_Height - $watermark_Height);
// Draw the watermark on your image
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
// From now on depends on you what you want to do with the image
// for example save it in some directory etc.
// In this example we'll Send the img data to the browser as response
// with Plain PHP
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
// Or if you prefer to save the image on some directory
// Take care of the extension and the path !
// $image->writeImage(getcwd(). "/goat_watermark." . $image->getImageFormat()); 

COPY SNIPPET

Which should produce the following image:

Bottom Right Watermark

Right top corner watermark
<?php
// Open the image to draw a watermark
$image = new Imagick();
$image->readImage(getcwd(). "/goat.jpg");
// Open the watermark image
// Important: the image should be obviously transparent with .png format
$watermark = new Imagick();
$watermark->readImage(getcwd(). "/watermark_file.png");
// The resize factor can depend on the size of your watermark, so heads up with dynamic size watermarks !
$watermarkResizeFactor = 6;
// Retrieve size of the Images to verify how to print the watermark on the image
$img_Width = $image->getImageWidth();
$img_Height = $image->getImageHeight();
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Resize the watermark with the resize factor value
$watermark->scaleImage($watermark_Width / $watermarkResizeFactor, $watermark_Height / $watermarkResizeFactor);
// Update watermark dimensions
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Draw on the top right corner of the original image
$x = ($img_Width - $watermark_Width);
$y = 0;
// Draw the watermark on your image
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
// From now on depends on you what you want to do with the image
// for example save it in some directory etc.
// In this example we'll Send the img data to the browser as response
// with Plain PHP
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
// Or if you prefer to save the image on some directory
// Take care of the extension and the path !
// $image->writeImage(getcwd(). "/goat_watermark." . $image->getImageFormat()); 

 Which should produce the following image:

Right Top Corner Watermark Imagick

Bottom left corner watermark
<?php
// Open the image to draw a watermark
$image = new Imagick();
$image->readImage(getcwd(). "/goat.jpg");
// Open the watermark image
// Important: the image should be obviously transparent with .png format
$watermark = new Imagick();
$watermark->readImage(getcwd(). "/watermark_file.png");
// The resize factor can depend on the size of your watermark, so heads up with dynamic size watermarks !
$watermarkResizeFactor = 6;
// Retrieve size of the Images to verify how to print the watermark on the image
$img_Width = $image->getImageWidth();
$img_Height = $image->getImageHeight();
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Resize the watermark with the resize factor value
$watermark->scaleImage($watermark_Width / $watermarkResizeFactor, $watermark_Height / $watermarkResizeFactor);
// Update watermark dimensions
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Draw on the bottom left corner of the original image
$x = 0;
$y = ($img_Height - $watermark_Height);
// Draw the watermark on your image
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
// From now on depends on you what you want to do with the image
// for example save it in some directory etc.
// In this example we'll Send the img data to the browser as response
// with Plain PHP
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
// Or if you prefer to save the image on some directory
// Take care of the extension and the path !
// $image->writeImage(getcwd(). "/goat_watermark." . $image->getImageFormat()); 

 Which should produce the following image:

Watermark Image Imagick PHP

Top left corner watermark
<?php
// Open the image to draw a watermark
$image = new Imagick();
$image->readImage(getcwd(). "/goat.jpg");
// Open the watermark image
// Important: the image should be obviously transparent with .png format
$watermark = new Imagick();
$watermark->readImage(getcwd(). "/watermark_file.png");
// The resize factor can depend on the size of your watermark, so heads up with dynamic size watermarks !
$watermarkResizeFactor = 6;
// Retrieve size of the Images to verify how to print the watermark on the image
$img_Width = $image->getImageWidth();
$img_Height = $image->getImageHeight();
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Resize the watermark with the resize factor value
$watermark->scaleImage($watermark_Width / $watermarkResizeFactor, $watermark_Height / $watermarkResizeFactor);
// Update watermark dimensions
$watermark_Width = $watermark->getImageWidth();
$watermark_Height = $watermark->getImageHeight();
// Draw the watermark on your image (top left corner)
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, 0, 0);
// From now on depends on you what you want to do with the image
// for example save it in some directory etc.
// In this example we'll Send the img data to the browser as response
// with Plain PHP
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
// Or if you prefer to save the image on some directory
// Take care of the extension and the path !
// $image->writeImage(getcwd(). "/goat_watermark." . $image->getImageFormat()); 

Which should produce the following image:

Top Left Watermark Imagick PHP

Happy coding !