Watimage Watermark Class Usage

Reading time ~9 minutes

The Watermark class extends Image so you’ll be able to apply any filter or action (like crop or resize) to your watermark as you do with your images.

To start adding Watermarks you’ll need to use both classes:

use Elboletaire\Watimage\Image;
use Elboletaire\Watimage\Watermark;

$image = new Image('test.png');
$watermark = new Watermark('watermark.png');

After you’ve loaded your files you can treat both images at your like:

$image->flip()->negate();
$watermark->pixelate(3, true);

When you want to apply your watermark to your image, simply do apply:

$watermark->apply($image)->generate();

Watermark’s apply method returns the Image instance so you can directly generate the resulting image.

You can use a unique watermark to watermark multiple images or you can even use the same watermark to apply it multiple times to the same image!

// Apply watermark to image
$watermark->apply($image);
// Filter image after applying the watermark (pixelating the first watermark too)
$image->pixelate(4, true);
// Apply watermark again but changing position
$watermark->setPosition('top left')->apply($image);
// This will generate a pixelated image with two watermarks, one of them
// pixelated due to the filter applied to the image after adding the first watermark.
$image->generate();

Everything together:

use Elboletaire\Watimage\Image;
use Elboletaire\Watimage\Watermark;

$image = new Image('test.png');
$watermark = new Watermark('watermark.png');

$image->flip()->negate();
$watermark->pixelate(3, true);
// Apply watermark to image
$watermark->apply($image);
// Filter image after applying the watermark (this will pixelate the first watermark too)
$image->pixelate(4, true);
// Apply watermark again but changing position
$watermark->setPosition('top left')->apply($image);
$image->generate();

An advanced example, applying the same watermark in each border of the image:

use Elboletaire\Watimage\Image;
use Elboletaire\Watimage\Watermark;

$image = new Image('test.png');
$watermark = new Watermark('watermark.png');

// Pixelate the image
$image->pixelate(4, true);
// Apply all the watermarks
$watermark->setPosition('top left')->apply($image);
$watermark->setPosition('top right')->apply($image);
$watermark->setPosition('bottom right')->apply($image);
$watermark->setPosition('bottom left')->apply($image);
$image
    // Add a vignette effect with watermarks included
    ->vignette()
    // And save to file
    ->generate('my-saved-image.png');

← Go back to Watimage Classes Usage

About the header image

For the header image the following code has been executed:

$image = new Image('el-carmel-viewpoint.jpg');
$image
  ->contrast(-5)
  ->brightness(-60)
  ->colorize(['r' => 100, 'g' => 70, 'b' => 50, 'a' => 0])
  ->brightness(-30)
  ->contrast(-5)
  ->generate()
;

Both the image and the original can be found under gh-pages branch, inside images folder.