The last post i wrote was about getting started with GD library, today i want to show you practical use of this wonderful library. I want to protect all images on my server, so i need to create watermarks for all my images, but let’s pretend i want to sell this images at the same time so there cannot be any watermarks on it. There are two solutions to this problem: create images with and without watermarks or use GD library and create watermarks on the fly. Obviously we will take advantage of the second method because it is more professional and requires less work in the end.

First we will need two images one quite big (for “sell” image) and second small image which will be our watermark, of course you can use transparent big image as a watermark as well, but we will use small image because it is a bit harder.

One more note before we go to coding you do not have to use second image at all, using techniques i showed you in the last post you can put text or some figures on your picture instead of image. There will be one change in code however, you will need to use imagecreatefromjpeg($imgname); not imagecreate($x, $y);.

If you have two JPEG images then we can get started. You can use images in other formats but you will need to make some changes in the code as well, so i advice you to use JPEGs this time.

We will start with sending headers like we did last time but we will add one more header.

You are familiar with the first line so i will explain the second line. Creating watermark is a process that requires a little work from the server, so we set up Cache-Control to store watermarked images on client computer for 3600 seconds (1 hour). You can change it to days or even months if you want to, but again max-age has to be value in seconds.

Now we need to create function which will load our images and add watermarks to them.

function LoadJpeg($imgname)
{
    $mark = imagecreatefromjpeg("watermark.jpg");
    list($mwidth, $mheight) = getimagesize("watermark.jpg");
    $img = imagecreatefromjpeg($imgname);
    list($iwidth, $iheight) = getimagesize($imgname);
    imagecopy($img, $mark, $iwidth-$mwidth-5, $iheight-$mheight-5,
        0, 0, $mwidth, $mheight);
    imagejpeg($img);
 
}

This is actually quite simple. First we load watermark.jpg and get width and height of it. Then we do the same for image on which we want to put this watermark.
Next we use imagecopy(…) function to copy watermark.jpg on the other JPEG. First two parameters are references to images, third and fourth parameters are x and y coordinates, which we use to place image in certain place. In this example our watermark will be at the bottom on the right side width 5px margin. The last two parameters are width and height of our watermark image, we set them to this values because we want to copy whole watermark image not just part of it, if you wish to copy only part of your watermark image then you need to change them.
The last thing we need to do is load our image:

LoadJpeg("58.jpg");
 
?>;

and we are done.
As you see creating watermarks and manipulating images in PHP isn’t hard at all with GD library, now you can also do the same with PNG and GIF pictures but remember to change headers and use other functions for loading PNGs and GIFs.