php-imagine: how can i make the watermark semi-transparent (reduce the opacity)?

43 Views Asked by At

I'm using https://github.com/php-imagine/Imagine (as use Imagine\Imagick\Imagine) to handle my images. I'm applying a transparent (alpha-channel) PNG image as watermark, following the docs here https://imagine.readthedocs.io/en/stable/usage/introduction.html#image-watermarking :

$watermark = $imagine->open('/my/watermark.png');
$image     = $imagine->open('/path/to/image.jpg');
$size      = $image->getSize();
$wSize     = $watermark->getSize();

$bottomRight = new Imagine\Image\Point($size->getWidth() - $wSize->getWidth(), $size->getHeight() - $wSize->getHeight());

$image->paste($watermark, $bottomRight);

It works, but I'd like to reduce the opacity of the watermark, so it's less in-your-face.

I tried to search the docs via https://imagine.readthedocs.io/en/stable/search.html , but it doesn't seem to work.

2

There are 2 best solutions below

2
Alex Howansky On

Looking at the code, it appears that the paste() method has an alpha argument built right into it:

public function paste(ImageInterface $image, PointInterface $start, $alpha = 100)

Since the argument is optional, and you're not overriding in your example, you're getting the default value of 100. To override, just explicitly provide a value less than 100 for the third parameter:

$image->paste($watermark, $bottomRight, 25);

Note if you use a modern IDE, you'll get autocompletion on method arguments, and this sort of feature will become trivially easy to discover:

enter image description here

1
Dhanushka Perera On

Assuming you are using the PHP library Imagine for image manipulation, you can apply transparency to a watermark by adjusting the alpha channel of the image. Here's an example using Imagine library with GD:

<?php

require 'vendor/autoload.php'; // Include the autoloader for Imagine library

use Imagine\Image\Box;
use Imagine\Image\Point;
use Imagine\Gd\Imagine;

// Create an Imagine object
$imagine = new Imagine();

// Open the original image
$image = $imagine->open('path/to/your/image.jpg');

// Open the watermark image
$watermark = $imagine->open('path/to/your/watermark.png');

// Set the opacity level (0 fully transparent, 100 fully opaque)
$opacity = 50; // Change this value as needed

// Apply transparency to the watermark
$watermark = $watermark->applyMask($watermark->mask(), $opacity);

// Calculate the position to place the watermark (e.g., bottom right corner)
$position = new Point(
    $image->getSize()->getWidth() - $watermark->getSize()->getWidth(),
    $image->getSize()->getHeight() - $watermark->getSize()->getHeight()
);

// Paste the watermark onto the original image
$image->paste($watermark, $position);

// Save the result
$image->save('path/to/save/result.jpg');

?>

In this example, the apply mask method is used to adjust the opacity of the watermark. The $opacity variable determines the level of transparency, where 0 is fully transparent, and 100 is fully opaque. You can modify the $opacity value to achieve the desired level of transparency for your watermark.

Make sure to replace 'path/to/your/image.jpg' and 'path/to/your/watermark.png' with the actual paths to your original image and watermark image. Additionally, adjust the positioning and save paths as needed for your specific use case.