WaterMark is like a brand that the owner uses on an image or any other innovative thing and says he is the owner of that particular thing. WaterMark is very beneficial in a digital age where lots of digital content are spreading everywhere and everybody can use that content.
Watermark is the best way to protect the image from being stolen or reused by another person. By introducing watermark to the picture, you can show the property. The watermark helps recognize the image/photo maker. The watermark stamp is mostly used in the pictures that are copyrighted. The business logo or title of the designer is generally introduced as a watermark to the pictures.
The dynamic watermark function is very helpful for the images to upload the management section. In this tutorial, we’ll show you how to upload the image to the server and add watermark to an image using PHP.
To Dynamically add WaterMark to an image, it only takes two steps:-
Step 1.Make an index file (index.php) and Below Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<div class="col-xs-12 col-md-12"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Select Image File to Upload:</h3> </div> <div class="panel-body"> <!-- display errors returned by createToken --> <p class="payment-status"></p> <form action="" method="post" enctype="multipart/form-data" class="form-inline"> <?php if(isset($statusMsg)){?> <p style="color:green"><?php echo $statusMsg;?></p> <?php } ?> <div class="form-group"> <label for="file"> Upload File:</label> <input type="file" name="file" class="form-control"> </div> <input type="submit" name="submit" value="Upload" class="btn btn-success"> </form> </div> </div> <?php if(isset($targetFilePath)){?> <img src="<?php echo $targetFilePath?>" alt="Watermark img" width="100%"> <?php } ?> </div> |
Step 2. Add below code on index.php file above html code to add watermark image
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php // Path configuration $targetDir = "uploads/"; $watermarkImagePath = 'tutorials-website-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 $watermark_png_Img = imagecreatefrompng($watermarkImagePath); switch($fileType){ case 'image/jpg': $imagesource = imagecreatefromjpeg($targetFilePath); break; case 'image/jpeg': $imagesource = imagecreatefromjpeg($targetFilePath); break; case 'image/png': $imagesource = imagecreatefrompng($targetFilePath); break; default: $imagesource = imagecreatefromjpeg($targetFilePath); } // Set the margins for the watermark $marge_right = 10; $marge_bottom = 10; $water_width = imagesx($watermark_png_Img); $water_height = imagesy($watermark_png_Img); $dime_x = imagesx($imagesource) - imagesx($watermark_png_Img) - $marge_right; $dime_y = imagesy($imagesource) - imagesy($watermark_png_Img) - $marge_bottom; // Copy the watermark image onto our photo using the margin offsets and // the photo width to calculate the positioning of the watermark. imagecopy($imagesource, $watermark_png_Img, $dime_x ,$dime_y , 0, 0, $water_width, $water_height); // Save image and free memory imagepng($imagesource, $targetFilePath); imagedestroy($imagesource); 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.'; } } ?> |
Conclusion
This is how to add WaterMark to an image using PHP. You can further customize this code according to your requirements. You can automatically add the watermark to upload pictures using PHP. The watermark can be added to any spot on the picture based on the specified margin offsets.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co