Watermark is the best option to protect the image from being stolen or re-used by another person. You can display the ownership by adding watermark to image. The watermark helps to identify the creator of image/photo. Mostly, the watermark stamp is used in the copyrighted images. Generally, the company logo or creator name is added to images as a watermark.
Upload image to the server can be easily implemented using PHP. You can also add watermark to uploaded image on the fly using PHP. PHP GD library provides an easy way to add a watermark image to photo using alpha channels. The dynamic watermark feature is very useful for the images to upload management section. In this tutorial, we will show you how to upload image to server and add watermark to image using PHP.
Create an HTML form that allows selecting a file to upload.
type="file"
attribute.<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">
</form>
After the form submission, the file data is posted to the upload.php
file to upload and add watermark to image using PHP.
The upload.php
file handles the image upload and watermark adding functionality.
<?php
// Path configuration
$targetDir = "uploads/";
$watermarkImagePath = 'codexworld-logo.png';
$statusMsg = '';
if(isset($_POST["submit"])){
if(!empty($_FILES["file"]["name"])){
// 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);
switch($fileType){
case 'jpg':
$im = imagecreatefromjpeg($targetFilePath);
break;
case 'jpeg':
$im = imagecreatefromjpeg($targetFilePath);
break;
case 'png':
$im = imagecreatefrompng($targetFilePath);
break;
default:
$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);
imagedestroy($im);
if(file_exists($targetFilePath)){
$statusMsg = "The image with watermark has been uploaded successfully.";
}else{
$statusMsg = "Image upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, and PNG files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
}
// Display status message
echo $statusMsg;
Create and Add Watermark to PDF using PHP
Our example script helps you to upload image with watermark and save on the server using PHP. You will be able to add the watermark to uploaded images automatically using PHP. Based on the specified margin offsets, the watermark can be added to any position on the image. You can easily enhance the script functionality to add text watermark to image using PHP.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Thanks, now i have an head start
Wow Supperb! Good Work! Thank you!
Good work. Written very well and clean