Home > PHP > How to Add Watermark to Image Using PHP?

How to Add Watermark to Image Using PHP?

Do you know what is what is a watermark and how you can protect your images by adding watermark on them using PHP programmatically?


Don’t worry!

In this tutorial, we will learn how to add a watermark on the image using PHP and what it is actually.

So let’s get started.

What is a Watermark?

Suppose You are a web professional and working on an e-commerce project. you have some images of the different products. Now you need to write ‘featured’ or ‘new’? on some products. for doing so you need to edit your images of the product.

So you can do it using a simple PHP GD script that’s called a watermark.

You can create different types of watermarks on all of your images using PHP script. With this PHP GD script, you can add text as a watermark and also image as a watermark. Let’s start work with this library script.

Add Text Watermark to Image Using PHP

Here I am going to show you a way how you can dynamically put a text message on your images of the products.

Only you need it? JPG product image file and a font file used to generate the watermark message. Here I am using Arial font which you can also download from google.

Below are some steps to create a watermark on an image you just need to follow these steps:

Step 1: First of all You need to download the ‘arial.ttf’ font file and place it in your script folder. you can download the font file from here.

Step 2: Create a new PHP file in your script folder and place the following code in your file.

In the above code, we created a function watermarkImage() and did the functionality for adding watermark on image inside.

The last 4 lines for holding the data to run the watermark function.
$SourcceFile: The main image file path
$DestinationFile: Output image name
$WaterMarkText: Text you want to add on the image as watermark.

And last is hitting the watermarkImage() function to create a watermark image with text.

Step 3: If you want to change the value of the $red variable but make sure your color will be in RGB color. And change the watermark text angle and position where text will be shown, you can change in the imagettftext().

If you don’t want to download the image and just want to show it on the browser then set the $DestinationFile variable blank in the if condition like if ($DestinationFile="").

Step 4: After the change, all the required fields as per your requirements then save the file and run it on the browser. You will see the image displaying with a watermark text message.

Add Image Watermark to Image Using PHP

As we said above, you can also add image watermark to an image. This means, you can add an image onto another image as a watermark. To do this you need to use imagecopy() function.

The imagecopy() function will copy the source image onto the destination image.

Syntax: imagecopy($destinationImage, $srcImage, $destinatioX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight);

Follow the below steps to add image watermark:

Step 1: Create a folder and take two images inside it. One will be main image and second will be watermal image in PNG format.

Step 2: Now create a new PHP file and paste the following code in this file.

This script code will create a new image with watermark image. You have to provide the images file and required parameter. The last 4 line same as I explained in above.

Step 3: Save the file and run on the browser. You will see watermark image onto the main image file.

That’s it!!!

Some guidelines:

As you can see watermarkImage() image function in the script above.

which takes 3 parameters ($SourceFile, $WaterMarkText, $DestinationFile) and creates an watermarked image from the source image specified.

$SourceFile – It is the full file path to the JPG image that you are going to watermark.

$WaterMarkText and $WaterMarkImage – It is the text message and image that you want to use for the watermark.

$DestinationFile – It can either be blank or full file path to a new file which will be the source file with watermark text on it.

What this function does is to read the source file, then create a new image object (using the imagecopyresampled() function). Then using the Arial.ttf font file and the imagettftext() function it writes the text onto the image and image onto image. The last IF statement checks, if it should save a new watermarked file or should just display it on the screen.

Hope you understand the code to add watermark to image using PHP GD script library.

If you have any quetions please ask me in the comment section. I’ll respond to you as soon as possible.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment