Upload Image & Add Watermark Using PHP

Kailash Singh

Upload Image & Add Watermark Using PHP

Published May 07,2022 by Kailash Singh

0 Comment     3205 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.


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

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

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


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

	$errorMsg = ''; 

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

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

	    	//file should be less than 2MB
		    if (($_FILES['image']['size'] > 2097152))
		        $errorMsg = 'File too large. File must be less than 2 MB.'; 

		    	//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); 
	                		$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); 
		     			//check file exist or not
		                    $errorMsg = "Watermark has been applied on image successfully."; 
		                    $errorMsg = "Image uploading error, please try again."; 
		                $errorMsg = "Error: there was an error uploading your file."; 
		            $errorMsg = 'Only accept JPG, JPEG, and PNG files.'; 
	        $errorMsg = 'Please select a image.'; 

	// Display message according to condition 
	echo $errorMsg;



Comments ( 0 )