How To Add Watermark To Image Using PHP

Watermark is a label that is used on images to display ownership to protect images from being stolen. If you're developing image gallery application using PHP and wants to add watermark to images when uploaded, then its very easy. You can easily add watermark to images using PHP. In this tutorial you will learn how to add text or image Watermark to images dynamically when uploaded. You can also view running demo and can download add watermark to image script.

So let’s start the coding. We will have following file structure for this tutorial.


  • index.php

  • functions.php



Steps1: Create File Upload Form

In index.php, we will create file upload FORM HTML with file browse input and drop down list to select watermark type text or image.

<div class="container">
<h2>Example: How To Add Watermark To Images Using PHP</h2>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" value="">
<select name="image_upload">
<option value="">Select Water Mark Type</option>
<option value="text_watermark">Text Water Mark</option>
<option value="image_watermark">Image Water Mark</option>
</select>
<input type="submit" value="Upload">
</form>
</div>



Steps2: Handle Image Upload and Add Watermark

Now index.php, we will handle image upload and add watermark functionality to uploaded images. We will check for valid image type and then handle image upload and then add Watermark to uploaded images according selected bookmark type (text or image). We will call function addTextWatermark() add text Watermark and call function addImageWatermark() to add image watermark. We have used watermark.png for image watermark and text "PHPZAG" for text watermark.

<?php
if(isset($_FILES['image']['name'])){
// Validating Type of uploaded image
switch($_FILES['image']['type']){
case 'image/jpeg':
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['image_upload'])){
echo 'Please select watermark type';
} else {
// Create new name for uploaded image with upload directory path
list($txt, $ext) = explode(".", $_FILES['image']['name']);
$file_name = "images/".rand(0, 9999).'.'.$ext;
$upload = copy($_FILES['image']['tmp_name'], $file_name);
if($upload == true){
// Check type of water mark is selected
if($_POST['image_upload'] == 'text_watermark'){
// Add text watermark over image
$watermark = "PHPZAG"; // Add your own water mark here
addTextWatermark($file_name, $watermark, $file_name);
} elseif($_POST['image_upload'] == 'image_watermark'){
// Add image watermark over image
$WaterMark = 'watermark.png';
addImageWatermark ($file_name, $WaterMark, $file_name, 50);
}
echo '<br><img src="'.$file_name.'" class="preview" width="500"><br>';
} else {
echo 'Error uploading image';
}
}
break;
default:
echo 'Please select only JPEG or JPG file type for upload';
}
}
?>



Steps3: Define Add Watermark Functions
In functions.php, we will create functions addTextWatermark() and addImageWatermark to add both text and image watermark to images. Here we have used MONOFONT.ttf font to add text watermark

<?php
// Function to add text water mark over image
function addTextWatermark($src, $watermark, $save=NULL) {
list($width, $height) = getimagesize($src);
$image_color = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($src);
imagecopyresampled($image_color, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$txtcolor = imagecolorallocate($image_color, 255, 255, 255);
$font = 'MONOFONT.ttf';
$font_size = 50;
imagettftext($image_color, $font_size, 0, 50, 150, $txtcolor, $font, $watermark);
if ($save<>'') {
imagejpeg ($image_color, $save, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_color, null, 100);
}
imagedestroy($image);
imagedestroy($image_color);
}
// Function to add image watermark over images
function addImageWatermark($SourceFile, $WaterMark, $DestinationFile=NULL, $opacity) {
$main_img = $SourceFile;
$watermark_img = $WaterMark;
$padding = 5;
$opacity = $opacity;
// create watermark
$watermark = imagecreatefrompng($watermark_img);
$image = imagecreatefromjpeg($main_img);
if(!$image || !$watermark) die("Error: main image or watermark image 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;
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);
}
?>


You can view the live demo from the Demo link and can download the script from the Download link below.
Demo [sociallocker]Download[/sociallocker]

Komentar

  1. Great code sample. Integrated it into my solution.
    Instead of
    imagecolorallocate(..)
    I used
    imagecolorallocatealpha(..., alpha)
    which exactly does, what I need.

    Thank you

    BalasHapus
  2. Demo doesn't seem to work. Uploads but has no watermark.

    BalasHapus
  3. I have just checked and its working fine.

    BalasHapus

Posting Komentar

Postingan Populer