I have been working to put a watermark to an image to the og:image tag for Facebook, after several attempts finally decided to create it with certain size (10% of the original) and in a certain position (you can play with coordinates and put it when it’s convenient for you controlling the 1.91:1 scale Facebook recommends, so they will treat your watermark nicer).

It’s quite easy to create a watermark with a custom text in PHP, but It seemed like having a tutorial about it may be time saver for other people.

The first we need to do is to install Imagine and activate gd in our server (load the library gd.so in our php.ini), if you are using Symfony LiipImagineBundle might be a good idea for you.

In my case I import the needed classes:

```prettyprint lang-php <?php use Imagine\Gd\Image; use Imagine\Image\Point; use Imagine\Image\Box; …


And create the watermark with the custom text.

```prettyprint lang-php
    <?php
    $watermarkFilename = 'path/to/my/image.png';
    $watermarkImage = new Image(imagecreatefrompng($watermarkFilename);
    $x = 300;
    $y = 300;
    $font = '/path/to/.fonts/FreeSansBold.ttf';
    // We define the text colour as white
    $bg_colour = imagecolorallocate($watermarkImage->getGdResource(), 255, 255, 255);
    // x and y are the coordinates of the first character
    imagettftext(
        $watermarkImage->getGdResource(), self::FONT_SIZE,
        0, $x, $y, $bg_colour, $font,
        'The text of the watermark'
    );

    // Setting flags for the image
    // Check http://php.net/manual/en/book.image.php
    imagealphablending($watermarkImage->getGdResource(), false);
    imagesavealpha($watermarkImage->getGdResource(), true);

After getting the text inside the picture, I resize the watermark and just created the thumbnail with LiipImagine.

prettyprint lang-php <?php use Liip\ImagineBundle\Imagine\Filter\Loader\ThumbnailFilterLoader; ... $options = array( 'size' => array('500', '500'), 'mode' => 'outbound' ); $watermarkImage = $thumbnailFilter->load($watermarkImage, $options); // $image is our original image, It's an instance of Imagine\Gd\Image $finalImage = $image->paste($watermarkImage, new Point($x, $y));

But you could just use an Imagine filter and do the same.