Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials
And Offer Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development Right Via Email.

Add WaterMark To An Image Using PHP.

Sunday, Jan 8, 2017

Tags:- PHP PHP GD HTML


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>
<html>
<body>

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

</body>
</html>


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

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

<?php
if(isset($_POST['submit']))
{
  // Give the Complete Path of the folder where you want to save the image	
  $folder="photos/";
  move_uploaded_file($_FILES["imageupload"]["tmp_name"], "$folder".$_FILES["imageupload"]["name"]);
  $file='photos/'.$_FILES["imageupload"]["name"];

  $uploadimage=$folder.$_FILES["imageupload"]["name"];
  $newname=$_FILES["imageupload"]["name"];

  // Set the thumbnail name
  $thumbnail = $folder.$newname."_thumbnail.jpg"; 
  $actual = $folder.$newname.".jpg";
  $imgname=$newname."_thumbnail.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