Php add watermark to image and save

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.

File Upload Form

Create an HTML form that allows selecting a file to upload.

  • Make sure the tag contains the following attributes.
    • method=”post”
    • enctype=”multipart/form-data”
  • Also, make sure tag contains type="file" attribute.

    Select Image File to Upload:
    
    

After the form submission, the file data is posted to the upload.php file to upload and add watermark to image using PHP.

Upload Image and Add Watermark with PHP [upload.php]

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

  • Use 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 create a new stamp from the watermark image using imagecreatefrompng[] function.
  • Load and create a new image from the uploaded image based on the file type.
  • Set the right and bottom margin for the watermark image.
  • Get 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 resource using imagedestroy[] function.
  • Display the watermarked image upload status.

Chủ Đề