Marko Mikulić Marko Mikulić - 26 days ago 29
PHP Question

Image watermark in Yii2

I am using yii\imagine\Image extension and want to add a watermark to my image.

Here's my code:

$watermarkImage = '@webroot/../images/watermark.png';
$image = '@webroot/../slike/img-4.jpg';
Image::watermark($image, $watermarkImage);


After this code is executed, nothing happens. What am I missing here?

Answer

The ::watermark() function creates the new image but doesn't automatically saves it. The function returns a Imagine\Gd\Image object. This object can be used to save the new files.

$watermarkImage = '@webroot/../images/watermark.png';
$image = '@webroot/../slike/img-4.jpg';
// Store the Image object in a variable
$newImage = Image::watermark($image, $watermarkImage);
// Call the save function to write the file to the disk.
$newImage->save(Yii::getAlias('@webroot/../slike/img-4-watermark.jpg'));
Comments