Home > Article > Backend Development > How to add watermark to image using php and Imagick
How to add watermarks to images using PHP and Imagick
Overview:
In website development, we often need to add watermarks to images to protect the copyright of the images or to add information to the images. In PHP, we can use the Imagick extension to achieve this functionality.
This article will introduce how to use PHP and Imagick to add watermarks to images, and provide code examples.
Install the Imagick extension:
Before we start, we need to confirm that the Imagick extension has been installed on the server. If it is not installed, you can choose different installation methods according to the server's operating system. Here, we take the Ubuntu operating system as an example, execute the following command to install the Imagick extension:
sudo apt-get install php-imagick
Add watermark to the image:
The following is a simple example , demonstrates how to use PHP and Imagick to add text watermarks to images.
// Create an Imagick object
$image = new Imagick('input.jpg');
// Create an ImagickDraw object for Draw watermark
$draw = new ImagickDraw();
$draw->setFillColor('#000'); // Set the color of the watermark
$draw->setFont('Arial'); //Set the font of the watermark
$draw->setFontSize(24); //Set the font size of the watermark
$draw->setGravity(Imagick::GRAVITY_SOUTH); //Set the position of the watermark
// Draw a watermark on the picture
$image->annotateImage($draw, 0, 0, 0, 'Watermark');
// Save the picture after adding the watermark
$image->writeImage('output.jpg');
// Destroy the object
$image->destroy();
?>
The above code first creates an Imagick object and specifies the image to be watermarked by passing in the image path. Then, an ImagickDraw object is created for drawing watermarks. You can customize the style of your watermark by setting the color, font and font size, as well as the watermark's position. Finally, use the annotateImage() method to draw the watermark onto the image. Then, save the watermarked image to the specified path through the writeImage() method. Finally, remember to destroy the Imagick object.
Also, you can add a watermark as a picture to another picture. The following is a sample code:
// Create an Imagick object for adding watermarked images
$image = new Imagick('input.jpg');
// Create an Imagick object to store watermark images
$watermark = new Imagick('watermark.png');
// Set the location of the watermark
$x = 0;
$y = 0;
//Add the watermark image to the image to be watermarked
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
// Save the watermarked image
$image->writeImage('output.jpg');
// Destroy the object
$image ->destroy();
$watermark->destroy();
?>
The above code creates an Imagick object for the image to be watermarked, and another Imagick Object used to store watermark images. By setting the position of the watermark, add the watermark image to the image to be watermarked at the appropriate location. Then, save the watermarked image and destroy the Imagick object.
Summary:
This article introduces how to use PHP and Imagick to add watermarks to images. By mastering the usage of Imagick extension, you can achieve more image processing functions and provide more possibilities for website development. I hope this article can be helpful to readers.
The above is the detailed content of How to add watermark to image using php and Imagick. For more information, please follow other related articles on the PHP Chinese website!