Adding watermarks to your website images, or online catalog is a great way to ensure no one else uses them and also promotes your brand or website. In this tutorial, we will learn how to watermark images with PHP in just 20 minutes.

Prerequisites
Before we get started, you need to have the following installed on your system:

PHP (version 8 or higher)
GD Library (for image manipulation)

Create a Watermark Image
The first step is to create a watermark image that you want to overlay on your original image. You can create a transparent PNG image with your logo or text using any image editing software like Photoshop or GIMP.

Upload the Original Image
Upload the original image that you want to watermark to your server. You can use PHP’s $_FILES superglobal to handle file uploads. Here’s an example:


if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

$extensions= array("jpeg","jpg","png");

if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}

if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}

if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}

This code checks for file extensions and size, and then moves the uploaded file to a directory named “images”.

Load the Original Image
Next, we need to load the original image using the GD library. Here’s an example:


$original_img = imagecreatefromjpeg("images/original_image.jpg");

This code loads the original image “original.jpg” from the “images” directory.

Load the Watermark Image
Now, we need to load the watermark image that we created earlier. Here’s an example:


$watermark_img = imagecreatefrompng("images/watermarked_image.png");

This code loads the watermark image “watermark.png” from the “images” directory.

Get the Dimensions of the Original Image
To overlay the watermark image on the original image, we need to know the dimensions of the original image. Here’s an example:


$original_width_img = imagesx($original_img);
$original_height_img = imagesy($original_img);

This code gets the width and height of the original image.

Get the Dimensions of the Watermark Image
Similarly, we need to know the dimensions of the watermark image. Here’s an example:


$watermark_width_img = imagesx($watermark_img);
$watermark_height_img = imagesy($watermark_img);

This code gets the width and height of the watermark image.

Overlay the Watermark Image on the Original Image
Now, we can overlay the watermark image on the original image using the imagecopy() function. Here’s an example:


imagecopy($original_image, $watermark_image, $original_width_img - $watermark_width_img - 10, $original_height_img - $watermark_height_img - 10, 0, 0, $watermark_width_img, $watermark_height_img);

This code overlays the watermark image on the bottom right corner of the original image with a margin of 10 pixels.

Save the Watermarked Image
Finally, we can save the watermarked image using the imagejpeg() function. Here’s an example:


imagejpeg($original_image, "images/watermarked.jpg");

This code saves the watermarked image as “watermarked_image.jpg” in the “images” directory.