Upload Image & Add Watermark Using PHP

Kailash Singh

Upload Image & Add Watermark Using PHP

Published May 07,2022 by Kailash Singh

0 Comment     2946 Views    


In this tutorial, we are going to teach you, how to create Upload Image & Add Watermark Using PHP.

 

Why we are using watermark?

On one end, watermarking helps protect the copyright of your work and ensures that it cannot be reused or altered without your permission. This means that people can still preview your work before purchasing it, without the risk of them stealing it.

 

Step 1: Create Folder, where your file will be uploaded.

 

Step 2: Upload File Form

Create an HTML form to upload an image.

  • Make sure the <form> tag contains the following attributes.
    1. action = ""
    2. method = "post"
    3. enctype = "multipart/form-data"
  • Also, make sure <input> tag contains type="file" attribute.

Example: 


<form method="post" action="" enctype="multipart/form-data">

	<input type="file" name="image">

	<button type="submit" name="submit">Upload</button>
	
</form>

 

Step 3: Add code of upload image and apply watermark on image.

<?php 
	$errorMsg = ''; 

	$targetDir = "uploadPath/";  //create path

	$watermark = 'logo.png'; //select watermark image 

	if(isset($_POST["submit"]))
	{ 
	    if(!empty($_FILES["image"]["name"]))
	    {
	    	//file should be less than 2MB
		    if (($_FILES['image']['size'] > 2097152))
		    {      
		        $errorMsg = 'File too large. File must be less than 2 MB.'; 

		    }
		    else
		    { 
		    	//Image Upload in folder code
		       	$imageName = basename($_FILES["image"]["name"]); 
		       	$image_upload1 = @end(explode('.', $imageName));
				$image_upload2 = strtolower($image_upload1);
				$new_image_upload = time().'.'.$image_upload2;
		        $filePath = $targetDir . $new_image_upload; 

		        //get file extention
		        $fileType = pathinfo($filePath,PATHINFO_EXTENSION); 

		        // check only file accepted 
		        $allowTypes = array('jpg','png','jpeg'); 
		        if(in_array($fileType, $allowTypes))
		        { 
		            // Upload file to the server 
		            if(move_uploaded_file($_FILES["image"]["tmp_name"], $filePath))
		            { 

	                	$watermarkImage = imagecreatefrompng($watermark); 

	                	if($fileType == 'jpg'){
	                		$im = imagecreatefromjpeg($filePath); 
	                	}elseif($fileType == 'jpeg'){
	                		$im = imagecreatefromjpeg($filePath); 
	                	}elseif($fileType == 'png'){
	                		$im = imagecreatefrompng($filePath); 
	                	}else{
	                		$im = imagecreatefromjpeg($filePath);
	                	}
		                
		                 
		                // Set the margins for the watermark 
		                $marge_right = 10; 
		                $marge_bottom = 10; 
		                 
		                // Get the height/width of the watermark image 
		                $sx = imagesx($watermarkImage); 
		                $sy = imagesy($watermarkImage); 
		                 
		                // Copy the watermark image onto our photo using the margin offsets and the photo width to calculate the positioning of the watermark. 
		                imagecopy($im, $watermarkImage, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage)); 
		                 
		                // Save image and free memory 
		                imagepng($im, $filePath); 
		                imagedestroy($im); 
		     
		     			//check file exist or not
		                if(file_exists($filePath))
		                { 
		                    $errorMsg = "Watermark has been applied on image successfully."; 
		                }else{ 
		                    $errorMsg = "Image uploading error, please try again."; 
		                } 
		            }else{ 
		                $errorMsg = "Error: there was an error uploading your file."; 
		            } 
		        }else{ 
		            $errorMsg = 'Only accept JPG, JPEG, and PNG files.'; 
		        } 
		    } 
	    }else{ 
	        $errorMsg = 'Please select a image.'; 
	    } 
	} 

	// Display message according to condition 
	echo $errorMsg;

?>

 


Comments ( 0 )