Posted on: July 15, 2020 11:04 AM
Posted by: Renato
Views: 2545
Adicionar uma marca d'água a uma imagem com o Imagick no PHP
Basicamente, tudo o que você precisa para adicionar uma marca d'água a uma imagem é o método compositeImage de um objeto de imagem. Este método permite compor facilmente uma imagem em outra.
`compositeImage`
```
<?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());
```
As coordenadas fornecidas no método dependem totalmente de você, pois seu desejo de marca d'água pode ser diferente (em toda a imagem ou apenas em um canto da imagem). O arquivo de marca d'água deve ter obviamente transparência com o formato .png, caso contrário, a marca d'água cobrirá claramente sua imagem original.
Examples
Nos exemplos a seguir, mostraremos exemplos de como adicionar uma marca d'água a uma imagem usando a seguinte marca d'água (a marca d'água também pode ser o logotipo do Our Code World):
For the image we'll use a copyright free happy goat:
Nice isn't ?
Marca d'água em tamanho real
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 !
Usamos a função getcwd do PHP para fornecer um caminho absoluto (recuperar o diretório de trabalho atual) para o Imagick, pois geralmente não funciona com caminhos relativos como ../file.png.
```
<?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());
```
O que produziria com nossas imagens a seguinte saída no navegador (ou se você decidisse salvá-lo em um arquivo):
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());
```
Which should produce the following image:
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:
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:
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:
Happy coding !
Autor:
Carlos Delgado
Fonte: https://ourcodeworld.com/articles/read/442/how-to-add-a-watermark-to-an-image-with-imagick-in-php
- https://github.com/tpmanc/yii2-imagick
- https://imagemagick.org/index.php
Donate to Site
Renato
Developer
-
Renato de Oliveira Lucena - há 4 anos
https://github.com/lucenarenato/watermark-with-imagemagick-PHP