PHP – Creating watermarks

In this tutorial we are going to merge two images below together to create a watermarked image. This will allow us to save time, since we do not have to create a watermark with an graphic application. We could also make this a function, and pass the images to the function to create a watermarked image in real time.

<?php
header("Content-type: image/png");

$text = imagecreatefromgif("site.gif");
$lightbulb = imagecreatefrompng("lightbulb.png");
list($width, $height) = getimagesize("site.gif");

Instead of using the imagecreate, to create our own image, we are using imagecreatefrompng and imagecreatefromgif, which creates an image from a png and gif sources. We are also getting the image height and width with getimagesize() and setting the results to the varibles $width, and $height.

imagecopymerge($lightbulb, $text, 0,10, 0,0, $width, $height, 70);

Imagecopymerge first needs to know what image we want to use as a background, followed by what image we want to merge onto it. Then the X-axis, and Y-axis to join the images at on the background image. Next, is the images where we want to start coping the foreground image, and the width and height that we want to be merged. The last number is the percent, ranging from 0 to 100, that we want merged. Finally, we need to create then destory the image from memory.

imagepng($lightbulb);
imagedestroy($lightbulb);

The outcome will create a watermarked image like the one below.

Leave a Reply

Your email address will not be published. Required fields are marked *

*