Hey all, time for another GD tutorial. This time i am going to go through "watermarking" your images.
DEMO: HERE
Firstly create a watermark image and save it as png-24 for best quality.
Firstly of course we want to set the actual image we want to watermark.
PHP Code:
$file = $_GET["src"]; // set it to a variable
That will take the source of the image from a variable set in the url, which we will talk about later.
Next we want to create the watermark image and load it up into RAM.
PHP Code:
$watermark = imagecreatefrompng("watermark.png");
As i said PNG work best. We do have a problem with using PNG-24 as the new GD doesnt seem to like it. Usally it just gives the image a white background, but we can get around this by doing the following.
PHP Code:
imagealphablending($watermark, true);
Now we want to create the actual image we are adding the watermark to and load it into RAM.
PHP Code:
$image=imagecreatefromjpeg($file);
Now we just want to get the height and width of the image so we can place the watermark correctly.
PHP Code:
$imageWidth=imageSX($image);
$imageHeight=imageSY($image);
and we also want to do the same for the actual watermark image itself.
PHP Code:
$watermarkWidth=imageSX($watermark);
$watermarkHeight=imageSY($watermark);
Now we need to set up some variables that will tell us where to place the watermark on the image.
PHP Code:
$coordinate_X = ($imageWidth - 5) - ($watermarkWidth);
$coordinate_Y = ($imageHeight - 5) - ($watermarkHeight);
//we want to be at least 5 pixels from the edge, but to make sure
// we shall add abit more so also take away the watermark dimensions
Now its a matter of place the watermark on the actual image.
PHP Code:
imagecopy($image, $watermark, $coordinate_X, $coordinate_Y, 0, 0, $watermarkWidth, $watermarkHeight);
Now we need to set a header type as a image as we do with all GD scripts and also display the image, but also clear any information we may still have in the RAM.
PHP Code:
header('content-type: image/jpeg');
imagejpeg ($image);
imagedestroy($image);
imagedestroy($watermark);
Usage
This script can easily be used by using the following script.
PHP Code:
<img src=watermark.php?src=image.jpg>
//change the image.jpg with the location of the image you want to
//watermark
And there you go a nicely watermarked image
FULL CODE
PHP Code:
<?php
$file = $_GET["src"];
$watermark = imagecreatefrompng("watermark.png");
imagealphablending($watermark, true);
$image=imagecreatefromjpeg($file);
$imageWidth=imageSX($image);
$imageHeight=imageSY($image);
$watermarkWidth=imageSX($watermark);
$watermarkHeight=imageSY($watermark);
$coordinate_X = ($imageWidth - 5) - ($watermarkWidth);
$coordinate_Y = ($imageHeight - 5) - ($watermarkHeight);
imagecopy($image, $watermark, $coordinate_X, $coordinate_Y, 0, 0, $watermarkWidth, $watermarkHeight);
header('content-type: image/jpeg');
imagejpeg ($image);
imagedestroy($image);
imagedestroy($watermark);
?>
This only currently creates watermarks for jpegs, but can easily be changed to support all types.