Skip to Content
PHP watermark library
php code / September 21, 2021


The best way to prevent an image from being stolen or re-used is to add a watermark. By putting a watermark to an image, you may show who owns it. The watermark aids in identifying the image/originator. photo’s In most copyrighted images, the watermark stamp is employed. In most cases, a watermark is used to add the corporate logo or the name of the creator to photos.

Using PHP, you may easily upload an image to the server. Using PHP, you can also add a watermark on an uploaded image on the fly. Using alpha channels, the PHP GD library makes it simple to add a watermark picture to a photo. For the image upload management portion, the dynamic watermark feature is really beneficial. In this article, we’ll show you how to use PHP to upload a picture to a server and add a watermark on it.

Form for Uploading Files

Create an HTML form that allows users to choose the file they want to upload.

The following attributes must be present in the form> tag.
method=”post” \senctype=”multipart/form-data”
Also, make sure the type=”file” property is present in the input> tag.

<form action="upload.php" method="post" enctype="multipart/form-data">
Select Image File to Upload:
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">

Following the submission of the form, the file data is transferred to the upload.php file, which is used to upload and add a watermark on the image using PHP.

Using PHP, you may submit an image and add a watermark to it (upload.php)

The upload.php file is responsible for picture uploading and watermarking.

To acquire the file extension and see if the selected file type is inside the authorised file format, use the PHP pathinfo() function.
Using PHP’s move uploaded file() method, upload a file to the server.

Using the imagecreatefrompng() function, load and make a new stamp from the watermark image.
Based on the file type, load and build a new image from the uploaded image.
Set the watermark image’s right and bottom margins.
Get the watermark image’s height and width.
Using the imagecopy() function, copy the watermark image onto the uploaded photo.
Calculate the watermark’s position using margin offsets and image width.
Using the imagepng() method, save an image with a watermark.
Using the imagedestroy() function, you can free up memory connected with an image resource.
The status of the watermarked image upload is displayed.

// Path configuration 
$targetDir = "uploads/"; 
$watermarkImagePath = 'codexworld-logo.png'; 
$statusMsg = ''; 
// File upload path 
$fileName = basename($_FILES["file"]["name"]); 
$targetFilePath = $targetDir . $fileName; 
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); 
// Allow certain file formats 
$allowTypes = array('jpg','png','jpeg'); 
if(in_array($fileType, $allowTypes)){ 
// Upload file to the server 
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ 
// Load the stamp and the photo to apply the watermark to 
$watermarkImg = imagecreatefrompng($watermarkImagePath); 
case 'jpg': 
$im = imagecreatefromjpeg($targetFilePath); 
case 'jpeg': 
$im = imagecreatefromjpeg($targetFilePath); 
case 'png': 
$im = imagecreatefrompng($targetFilePath); 
$im = imagecreatefromjpeg($targetFilePath); 
// Set the margins for the watermark 
$marge_right = 10; 
$marge_bottom = 10; 
// Get the height/width of the watermark image 
$sx = imagesx($watermarkImg); 
$sy = imagesy($watermarkImg); 
// Copy the watermark image onto our photo using the margin offsets and 
// the photo width to calculate the positioning of the watermark. 
imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg)); 
// Save image and free memory 
imagepng($im, $targetFilePath); 
$statusMsg = "The image with watermark has been uploaded successfully."; 
$statusMsg = "Image upload failed, please try again."; 
$statusMsg = "Sorry, there was an error uploading your file."; 
$statusMsg = 'Sorry, only JPG, JPEG, and PNG files are allowed to upload.'; 
$statusMsg = 'Please select a file to upload.'; 
// Display status message 
echo $statusMsg;

Note :

Using PHP, our example script allows you to upload a picture with a watermark and save it on the server. Using PHP, you will be able to automatically add the watermark on uploaded photographs. The watermark can be placed anywhere on the image based on the provided margin offsets. Using PHP, you can simply improve the script’s functionality by adding a text watermark on an image.


PHPCODE © 2024