So you have some images that you need watermarked, but it would be very time consuming to do it by hand. Or you have an application and want to add watermark to user uploaded images. You obviously can not ask the user to do it for you.

This simple solution will help you to make watermarked images on the fly, so your original images stay intact. First of all, you will need an image for the watermark and original image. In a live production you would get original image from the database.

You can see the demo below:


Then declare the header for jpeg image:

[code lang=”php”]
// this tells the browser to render jpg image
header(‘content-type: image/jpeg’);
[/code]

Next, we are creating a png image of our watermark and fetching its dimensions:

[code lang=”php”]
// creating png image of watermark
$watermark = imagecreatefrompng(‘watermark.png’);
// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
[/code]

We are doing the same for original picture, this time we are creating jpg. It is probably the best scenario that you always create jpg images from uploaded content. This way you can manipulate it more easily:

[code lang=”php”]
// creting jpg from original image
$image_path = ‘original.jpg’;
$image = imagecreatefromjpeg($image_path);
// getting the dimensions of original image
$size = getimagesize($image_path);
[/code]

Now, we need to place a watermark over the original image:

[code lang=”php”]
// placing the watermark 5px from bottom and right
$dest_x = $size[0] – $watermark_width – 5;
$dest_y = $size[1] – $watermark_height – 5;
[/code]

After that, we will use alpha blending function to set blending option for both images to true:

[code lang=”php”]
imagealphablending($image, true);
imagealphablending($watermark, true);
[/code]

And finally, we are creating a new image and showing it to the world using taken parameters:

[code lang=”php”]
// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
[/code]

It is important to free the used resources:

[code lang=”php”]
// destroying and freeing memory
imagedestroy($image);
imagedestroy($watermark);
[/code]

You can adjust the transparency effect of the watermark by reducing opacity for watermark image in Photoshop.

OK, I showed you the theory. Now let’s see how to use this in a live environment. This is the whole code that you will need to save in a file, let’s say, watermark.php:

[code lang=”php”]
// this tells the browser to render jpg image
header(‘content-type: image/jpeg’);

// getting the image name from GET variable
$image = $_GET[‘image’];

// creating png image of watermark
$watermark = imagecreatefrompng(‘watermark.png’);

// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

// creting jpg from original image
$image_path = ‘/path/to/image/folder/’ . $image;
$image = imagecreatefromjpeg($image_path);
//something went wrong
if ($image === false) {
return false;
}
// getting the dimensions of original image
$size = getimagesize($image_path);
// placing the watermark 5px from bottom and right
$dest_x = $size[0] – $watermark_width – 5;
$dest_y = $size[1] – $watermark_height – 5;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true);
// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
// destroying and freeing memory
imagedestroy($image);
imagedestroy($watermark);
[/code]

Now, when you need to show watermarked image just use above code like this:

[code lang=”php”]

[/code]

You just parse the image name to your watermark script, and use that as source of your image tag. Simple as that.

Tags: