skip to Main Content

So i have imagemagick Version: ImageMagick 6.9.11-60

To make the process easier I created a ready png file with a repeating pattern at the right angle, with the right transparency which is always larger than input image to cover it. Unfortunately I am not able to make it work.I described what I want to achieve

1. I have an input image (image1) and a watermark (watermark.png). Watermark is a ready mesh with a repeating watermark with the appropriate transparency. Its length and width are always greater than the length and width of image1
enter image description here

2. Now I want to apply this watermark to the center of image1 and the center of watermark1
enter image description here

3. The next step is to be able to reduce the size of the watermark by for example 20% . The dimensions of image1 cannot be changed. The watermark should still be centered after reducing the dimensions.
enter image description here

4. the next step is to move the watermark by a certain number of pixels x and y
enter image description here

5. The last step is to cut out the unnecessary part of the watermark so that only the input image with the watermark remains.
enter image description here

Ideally, it would be possible to do all of this in one query where I can provide parameters such as:

  • input image
  • watermark image
  • output image
  • by how many percent to reduce the watermark
  • by how many pixels X and Y to move the watermark

[EDIT]
in reference to the question I am attaching an example watermark and an image to apply the watermark to.
The wtarmark is already prepared with the appropriate transparency, shadows. The images to which I will apply the watermark are of different sizes and in different aspects but they will always fit within the watermark grid as in the images above
ok
enter image description here

enter image description here

2

Answers


  1. I managed to cobble something together that more-or-less does the job. It reads the image files from the server, and stores the output on the server as well. It looks like this:

    function applyWatermark(string $imagePath,
                            string $watermarkPath,
                            string $outputPath,
                            int $reductionInPercent,
                            int $offsetX,
                            int $offsetY): void
    {
        // get both image and watermark
        $image     = new Imagick($imagePath);
        $watermark = new Imagick($watermarkPath);
        // resize the watermark
        $newWidth  = 0.01 * (100 - $reductionInPercent) * $watermark->getImageWidth();
        $newHeight = 0.01 * (100 - $reductionInPercent) * $watermark->getImageHeight();
        $watermark->resizeImage($newWidth, $newHeight, Imagick::FILTER_QUADRATIC, 1);
        // center the offsets
        $offsetX  -= 0.5 * ($watermark->getImageWidth()  - $image->getImageWidth());
        $offsetY  -= 0.5 * ($watermark->getImageHeight() - $image->getImageHeight());
        // composite watermark into image
        $image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $offsetX, $offsetY);
        // generate output
        $image->writeImage($outputPath);
    }
    

    I think you can look up what the various Imagick methods exactly do in the documentation, but I added some comments to clarify them.

    All paths have to be absolute paths, and the three other settings are integers. A use case could be:

    applyWatermark(__DIR__ . '/dog.jpg',
                   __DIR__ . '/watermark.png',
                   __DIR__ . '/output.jpg',
                   10.0,
                   rand(-25, 25),
                   rand(-25, 25));
    

    Here the images and output are stored in the same location as the PHP script. The watermark is reduced by 10% and the offset x and y randomly moved by up to 25 pixels in either direction. The result looks like this:

    enter image description here

    Login or Signup to reply.
  2. Here is a short method in Imagemagick using -distort SRT with a computed viewport to do the cropping. The viewport is a global artifact, so it is stored past the composite where the two input images have been removed and their dimensions lost.

    X=-50
    Y=-50
    magick 
    watermark.png image.jpg 
    -set option:distort:viewport "%[fx:v.w]x%[fx:v.h]+%[fx:0.5*(u.w-v.w)+$X]+%[fx:0.5*(u.h-v.h)+$Y]" 
    -geometry +$X+$Y -gravity center -compose dstover -composite +repage 
    -virtual-pixel White -distort SRT 0 +repage x.png
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search