Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Add WaterMark To An Image Using PHP.

Friday, Apr 7, 2017


WaterMark is like a label which is used on an image or any other creative thing by the owner and tell that he is the owner of that particular thing.WaterMark is very useful in a digital age where tons of digital content spread every where and anyone can use that content.In this tutorial we add WaterMark to an image using PHP. You may also like add text on image using PHP.

Add WaterMark To An Image Using PHP

To Add WaterMark To An Image it takes only Two steps:-

  1. Make a HTML file and define markup to Upload Image
  2. Make a PHP file to add watermark to an image

Step 1.Make a HTML file and define markup for webpage

We make a HTML file and save it with a name watermark.html

<!DOCTYPE html>

  <form method="post" action="process.php" enctype="multipart/form-data">
  <input type="file" name="imageupload">
  <input type="submit" name="submit" value="upload">


Step 2.Make a PHP file to add watermark to an image

We make a PHP file and save it with name process.php.

  // Give the Complete Path of the folder where you want to save the image	
  move_uploaded_file($_FILES["imageupload"]["tmp_name"], "$folder".$_FILES["imageupload"]["name"]);


  // Set the thumbnail name
  $thumbnail = $folder.$newname."_thumbnail.jpg"; 
  $actual = $folder.$newname.".jpg";

  // Load the mian image
  $source = imagecreatefromjpeg($uploadimage);

  // load the image you want to you want to be watermarked
  $watermark = imagecreatefrompng('watermark.png');

  // get the width and height of the watermark image
  $water_width = imagesx($watermark);
  $water_height = imagesy($watermark);

  // get the width and height of the main image image
  $main_width = imagesx($source);
  $main_height = imagesy($source);

  // Set the dimension of the area you want to place your watermark we use 0
  // from x-axis and 0 from y-axis 
  $dime_x = 0;
  $dime_y = 0;

  // copy both the images
  imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);

  // Final processing Creating The Image
  imagejpeg($source, $thumbnail, 100);

  <img src='images/<?php echo $imgname;?>'>

In this step we use PHP GD library to add watermark to an image.First we upload the image to the image folder you can also view our tutorial on How to upload image. And after that we load both the images one that we get on form submit and other is our watermark image you can use any image for watermark of any format in our case we use PNG image and after that we get the dimension of both the images to recreate a single image having watermark on it

Then We set the dimension to place the watermark on that particular area we set 0 from X-axis and Y-axis to place the watermark in Upper-Left corner of the image you set the coordiantes as per your requirement.And then we use imagecopy() function to copy both the images and make a single one then we use imagejpeg to create the resultant image.We use imagejpeg() function because we want the resultant image to be in a J PEG format you can use any format function.You may also like display text over image using HTML and CSS.

Thats all, this is how to Add WaterMark To An Image Using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH