Adding watermark on image or PDF with PHP

Watermarking with PHP is not a complex thing. Google will list hundreds of solutions using GD or ImageMagick. But the complexity starts when I need custom positioning, rotation, repetition etc. Though all those things are just a matter of tweaking some options in ImageMagick or GD, discovering them every time is a pain. So I just encapsulated all those tweakings in a PHP library.

Check it – ajaxray/php-watermark

It supports watermarking on Image and PDF. You may watermark with a text or an image logo. Also it have self-explanatory API for customizing font, font-size, position, tileing, opacity, rotation etc.

How to use?

<?php
    // Initiate with source image or pdf
    $watermark = new Watermark('/path/to/source.jpg');
    
    // Customize some options
    $watermark->setFontSize(48)
       ->setRotation(30)
       ->setOpacity(.4);
    
    // Watermark with Text
    $watermark->withText('ajaxray.com', 'path/to/output.jpg');
    
    // Watermark with Image
    $watermark->withImage('path/to/logo.png', 'path/to/output.jpg');

Example Output

Here are a few example watermarked files –

If you think a commonly required customization option is missing, please let me know or just submit a PR. Thanks!

Leave a Comment

Your email address will not be published. Required fields are marked *