AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

Upload and Add Watermark to Image using PHP

Upload and Add Watermark to Image using PHP

Lakshika Mathur by Lakshika Mathur
December 23, 2019
Reading Time: 4 mins read
0
Upload and Add Watermark to Image using PHP

Watermark is the perfect choice to safeguard the image from being stolen or re-used by another person. You can show the ownership by adding a watermark to an image. The watermark helps to recognize the originator of the image/photo. Habitually, We use the watermark stamp in the copyrighted images. Therefore, It adds the company logo or creator name to images as a watermark.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

Upload images to the server can be easily applied using PHP. You can also add a watermark to uploaded images on the fly using PHP. Firstly, the PHP GD library offers an easy way to add a watermark image to photo using alpha channels. Secondly, the dynamic watermark feature is handy for the photos to upload the management section. At last, with the help of this tutorial, we will explain to you how to upload the image to the server and add a watermark to an image using PHP.

File Upload Form

Generate an HTML form that permits selecting a file to upload.

  • Make sure the <form> tag comprises the following attributes.
    • method=”post”
    • enctype=”multipart/form-data”
  • Also, make sure <input> tag contains 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 submitting the form, you have to post the file data to the upload.php  file to upload and add a watermark to an image using PHP.

SEE ALSO: Autocomplete Address Field using Google Maps JavaScript API with Places Library

Upload Image and Add Watermark with PHP (upload.php)

The upload.php  file handles the image upload and watermark adding functionality.

  • Use the PHP pathinfo() function to get the file extension and check whether the selected file type is within the allowed file format.
  • Upload file to server using move_uploaded_file() function in PHP.
  • Load and make a new stamp from the watermark image using imagecreatefrompng() function.
  • Make and load a new image from the uploaded image based on the file type.
  • Set the right and bottom margin for the watermark image.
  • Catch the height and width of the watermark image.
  • Copy the watermark image onto the uploaded photo using imagecopy() function.
  • Use margin offsets and image width to calculate the positioning of the watermark.
  • Save image with watermark using imagepng() function.
  • Free memory associated with image resources using imagedestroy() function.
  • Display the watermarked image upload status.
<?php 
// Path configuration 
$targetDir = "uploads/"; 
$watermarkImagePath = 'allsweb-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;

Conclusion

Firstly, our sample script helps you to upload an image with a watermark and save on the server using PHP. Secondly, it will be easy for you to add the watermark to uploaded images automatically using PHP. Based on the specified margin offsets, You can add the watermark to any position on the image. And at last, you can quickly improve the script functionality to add text watermark to an image using PHP.

Also, read our previous blog- Preview and Rotate Image Before Upload using jQuery and PHP

 

 

Tags: PHPWatermark
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
61
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
739
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
162
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
82
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
28
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
83
Next Post
Image Gallery CRUD with PHP and MySQL

Image Gallery CRUD with PHP and MySQL

Upload Multiple Images without Page Refresh using jQuery Ajax and PHP

Upload Multiple Images without Page Refresh using jQuery Ajax and PHP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version