We see images all over various websites, we also see a lot of those images are watermarked with that sites ‘stamp’ (logo). This is to stop other people using the image without acknowledging the source it game from.
Using PHP’s GD Graphics Libraries it is very simple to add a watermark to any image. This could be done at either upload time, when the image is uploaded to your server or at the point when the image is sent from your server.
I will be concentrating more on the sending to the user type, as I show lots of dynamic images that change, so I can’t pre-watermark them.
First off, we need to load the original image into PHP’s memory to then start editing it.
function watermark ($image)
{
$origImage = imagecreatefrompng('/path/to/original/' . $image);
// Load the watermark
$watermark = imagecreatefrompng('/path/to/watermark.png');
$watermarkWidth = imagesx($watermark);
$watermarkHeight = imagesy($watermark);
$Dest_X = imagesx($origImage) - $watermarkWidth - 5;
$Dest_Y = imagesy($origImage) - $watermarkHeight - 5;
imagecopymerge($origImage, $watermark, $Dest_X, $Dest_Y, 0, 0, $watermarkWidth, $watermarkHeight, 100);
// Send it to the browser as a new PNG and clean up
header('Content-type: image/png');
imagepng($origImage);
imagedestroy($origImage);
imagedestroy($watermark);
}
This script is making the assumption that both the original image and watermark come in the form of PNG’s already, this can be changed for jpegs or gifs easily by changing a couple of functions.
Be First to Comment