How can ImageMagick be used to automatically add a watermark to images in PHP?

To automatically add a watermark to images using ImageMagick in PHP, you can use the "composite" function to overlay the watermark image onto the original image. You would need to have the ImageMagick PHP extension installed on your server.

// Path to original image
$originalImage = 'path/to/original/image.jpg';

// Path to watermark image
$watermarkImage = 'path/to/watermark.png';

// Create Imagick objects for original and watermark images
$original = new Imagick($originalImage);
$watermark = new Imagick($watermarkImage);

// Add watermark to original image
$original->compositeImage($watermark, Imagick::COMPOSITE_OVER, 0, 0);

// Save the modified image
$original->writeImage('path/to/save/final/image.jpg');

// Clear resources
$original->clear();
$watermark->clear();