TrinityTuts

Dynamically add watermark on image upload

Last updated on January 8th, 2020 at 10:25 pm

In my last tutorial i will explain you that how we can add watermark on image but there something more which i discuss on this tutorial. In this tut i will explain you that how we can dynamically add watermark on image when we upload it in our server. This small tut help those who add watermark using some application before they upload image to server but it take much time  and no buddy want to waste there time.

So this small piece of code help you to add watermark on the fly. So your original image not damaged. Here i create two option for watermark one for Text watermark and other is Image Watermark.

Code

HTML Form for image uploading:

<form action="" method="post" enctype="multipart/form-data">
		<label>Image: </label>
		<input type="file" name="image" value="">
		<select name="option">
			<option value="">Select option</option>
			<option value="textW">Text Water Mark</option>
			<option value="imageW">Image watermark</option>
		</select>
		<input type="submit" value="Upload">
</form>

This is normal form which we create to upload image or files.

Watermark Function:

<?php
// Function to add text water mark over image
function textwatermark($src, $watermark, $save=NULL) { 
 list($width, $height) = getimagesize($src);
 $image_p = imagecreatetruecolor($width, $height);
 $image = imagecreatefromjpeg($src);
 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
 $txtcolor = imagecolorallocate($image_p, 255, 255, 255);
 $font = 'monofont.ttf';
 $font_size = 50;
 imagettftext($image_p, $font_size, 0, 50, 150, $txtcolor, $font, $watermark);
 if ($save<>'') {
 imagejpeg ($image_p, $save, 100); 
 } else {
 header('Content-Type: image/jpeg');
 imagejpeg($image_p, null, 100);
 }
 imagedestroy($image); 
 imagedestroy($image_p); 
}

// Function to add image watermark
function watermarkImage($SourceFile, $WaterMark, $DestinationFile=NULL, $opacity) { 

 $main_img = $SourceFile; 
 $watermark_img = $WaterMark; 
 $padding = 3; 
 $opacity = $opacity; 

 $watermark = imagecreatefromgif($watermark_img); // create watermark
 $image = imagecreatefromjpeg($main_img); // create main graphic

 if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");

 $watermark_size = getimagesize($watermark_img);
 $watermark_width = $watermark_size[0]; 
 $watermark_height = $watermark_size[1]; 

 $image_size = getimagesize($main_img); 
 $dest_x = $image_size[0] - $watermark_width - $padding; 
 $dest_y = $image_size[1] - $watermark_height - $padding;

 // copy watermark on main image
 imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
 if ($DestinationFile<>'') {
	imagejpeg($image, $DestinationFile, 100); 
 } 
 else {
	 header('Content-Type: image/jpeg');
	 imagejpeg($image);
 }
 imagedestroy($image); 
 imagedestroy($watermark); 
}
?>

I use same function which i create early and also explain in my last tutorial please check here to learn about these function.

Add watermark
I am using simple validation method here you add your own validation method and call these function and pass your image location to watermark making function and also your watermark text or image over watermark.

<?php
	if(isset($_FILES['image']['name'])){
		// Validating Type of image
		switch($_FILES['image']['type']){
			case 'image/jpeg':
			case 'image/png':
			case 'image/jpg':
				// Add more validation if you like
				if(getimagesize($_FILES['image']['tmp_name']) < (1024*1024*1024*1024)){
					echo 'Image size is greater than 2MB';
				}
				elseif(empty($_POST['option'])){
					echo 'Please select option';
				}else{
					// Create new name for your image
					list($txt, $ext) = explode(".", $_FILES['image']['name']);
					$newName = rand(0, 9999).'.'.$ext;
					$up = copy($_FILES['image']['tmp_name'], $newName);
						if($up == true){

							// Check which type of water mark is requested 
							if($_POST['option'] == 'textW'){
								// Add text watermark over image
								$watermark = "Trinity Tuts"; // Add your own water mark here
								textwatermark($newName, $watermark, $newName);							
							}elseif($_POST['option'] == 'imageW'){
								// Add image watermark 
								$WaterMark = 'http://trinityblog.in/power-admin/usercontent/1389684758watermark.gif';
								watermarkImage ($newName, $WaterMark, $newName, 50);
							}
							echo '<img src="'.$newName.'" class="preview" width="500">';
						}else{
							echo 'Error uploading image';
						}
					}
			break;
			default:
				echo 'Please select valid file for upload';
		}
	}

Thanku!
Happy Coding.