Add Watermark To Images In Php

A watermark is a visible image imprinted or embossed directly onto the paper or digitally added onto an image later.  There is a danger factor in publishing your images online. Hence adding a watermark to your image will prevent others from downloading and using it directly.Also if you have a website where users upload their private photos specific/relevant to your website, you may like to protect these images from being misused.so you can add your website/company logo as a watermark on these images.

Adding watermark on images in php is a fairly simple task and rest of this article will show you the code to achieve it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
define('WATERMARK_OVERLAY_IMAGE', 'logo.png'); // add your watermark image path here
define('WATERMARK_OVERLAY_OPACITY', 50);
define('WATERMARK_OUTPUT_QUALITY', 90);
function create_watermark($source_file_path, $output_file_path)
{
 list($source_width, $source_height, $source_type) = getimagesize($source_file_path);
 if ($source_type === NULL) {
 return false;
 }
 switch ($source_type) {
 case IMAGETYPE_GIF:
 $source_gd_image = imagecreatefromgif($source_file_path);
 break;
 case IMAGETYPE_JPEG:
 $source_gd_image = imagecreatefromjpeg($source_file_path);
 break;
 case IMAGETYPE_PNG:
 $source_gd_image = imagecreatefrompng($source_file_path);
 break;
 default:
 return false;
 }
 $overlay_gd_image = imagecreatefrompng(WATERMARK_OVERLAY_IMAGE);
 $overlay_width = imagesx($overlay_gd_image);
 $overlay_height = imagesy($overlay_gd_image);
 imagecopymerge(
 $source_gd_image,
 $overlay_gd_image,
 $source_width - $overlay_width,
 $source_height - $overlay_height,
 0,
 0,
 $overlay_width,
 $overlay_height,
 WATERMARK_OVERLAY_OPACITY
 );
 imagejpeg($source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY);
 imagedestroy($source_gd_image);
 imagedestroy($overlay_gd_image);
}
/*
 * Uploaded file processing function
 */
define('UPLOADED_IMAGE_DESTINATION', '../images/folder1/');
define('PROCESSED_IMAGE_DESTINATION', '../images/folder2/');
function process_image_upload($Field)
{
 $temp_file_path = $_FILES[$Field]['tmp_name'];
 $temp_file_name = $_FILES[$Field]['name'];
 list(, , $temp_type) = getimagesize($temp_file_path);
 if ($temp_type === NULL) {
 return false;
 }
 switch ($temp_type) {
 case IMAGETYPE_GIF:
 break;
 case IMAGETYPE_JPEG:
 break;
 case IMAGETYPE_PNG:
 break;
 default:
 return false;
 }
 $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
 $processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace('/\\.[^\\.]+$/', '.jpg', $temp_file_name);
 move_uploaded_file($temp_file_path, $uploaded_file_path);
 $result = create_watermark($uploaded_file_path, $processed_file_path);
 if ($result === false) {
 return false;
 } else {
 return array($uploaded_file_path, $processed_file_path);
 }
}
?>

code which call these functions to store uploaded image in “folder1″ and watermark image in “folder2″ is as follows:

1
2
3
4
5
6
7
<?
$result = process_image_upload('imagename'); // uploaded image name goes here
if ($result === false)
 echo '<br>An error occurred during file processing.';
?>