Watermark an image in PHP in four different ways

July 12, 2014 | Last tested: May 2015 | 747 views | Comments

Decide which one is the best for your project, here are four different ways to watermark an image using PHP, in no particular order.

1. Add a text watermark to an image with PHP

Among all techniques that will be presented here, this is probably the easiest way to watermark an image with PHP. The only challenge here (which is also present on the other methods of watermarking an image below) is the computation of the x and y coordinates where the position of the text will be placed. However, there are some limitations, like the use of font size and font style. The function imagestring() is the primary function that will generate the watermark on this example.


2. Add a text watermark using a True Type Font to an image with PHP

As compared with the first procedure, this one gives the option to set the font size more flexible because it is not limited to just five choices. Font size using this method can be set in either pixel size (GD1) or point size (GD2).

What's best in this technique is that a TrueType Font can also be used, giving more choices of font design for the users. X and Y coordinates needs to be computed/set as well. The function imagettftext() will write the text to the image using the selected TrueType font.


3. Watermark an image with PHP using imagecopy()

Using this manner of watermarking an image in PHP will not write any text on an image, but instead, this will combine two images, the watermark (in an image form), and the image to be watermarked. It is better to make the watermark with a transparent background so it blends smoothly with the image to be watermarked. The watermark can be designed in any image editor like Photoshop.

The only little inconvenience of using this approach is that the opacity of the watermark can't be dynamically changed, it needs to be manually edited to control it's opacity. If for some reason, the watermark needs to be 100% clear in one image, and 50% opacity in another, there needs to be two different watermark images. One with 100% opacity, and the other 50% opacity to obtain the goal.


4. Use imagecopymerge() to watermark an image with PHP

Just like the one before this method, the freedom of designing the watermark using any available font or any graphic resource is also possible with this watermark an image in PHP technique. Freely design the watermark in an image editor, and then use it to watermark an image.

But unlike the function imagecopy() which is used above, the function imagecopymerge() (used on this method), the opacity of the watermark can be adjusted from zero to one hundred. Therefore, only a single watermark image (best with 100% opacity) can only be created.