Search code examples
phpimage-processingimagick

watermark an image with rotation and opacity using imagick in php


How can i apply a watermark onto an image with rotation and opacity using imagick in php

this is my code

<?php
$image = new Imagick();
$image->readImage(realpath('C:\xampp\htdocs\imagick\3.jpg'));

$watermark = new Imagick();
$watermark->readImage(realpath('C:\xampp\htdocs\imagick\1.png'));

$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);

header("Content-Type: image/" . $image->getImageFormat());
echo $image;
?>

Solution

  • Get your watermark image and the image you want to add it to .

    Them merge them using PHP ImageCopy function

    <?php
    // Load the stamp and the photo to apply the watermark to
    $watermark= imagecreatefrompng('stamp.png');
    $im = imagecreatefromjpeg('photo.jpeg');
    
    // Set the margins for the stamp and get the height/width of the stamp image
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($watermark);
    $sy = imagesy($watermark);
    
    // Copy the stamp image onto our photo using the margin offsets and the photo 
    // width to calculate positioning of the stamp. 
    imagecopy($im, $watermark, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark));
    
    // Output and free memory
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
    ?>
    

    Should do the trick..

    In your example you are only missing the rotate image.

    $imagick->rotateImage(new ImagickPixel('transparent'), -13.55); 
    

    Imagick.rotateImage