We received many tutorial requests from 9lessons readers that asked how to generate watermark image using PHP. In this post Arun Kumar Sekar coded two functions such as watermark_text() and watermark_image() to generate text and images watermarks on images. Integrate this to your web project upload image system and produce copyright photos.

Download Script
Text Live Demo
Image Live DemoDeveloper
Watermark with Text - watermark_text()
Text watermark function, here you have to configure font source, size and text. live demo
$font_path = "GILSANUB.TTF"; // Font file
$font_size = 30; // in pixcels
$water_mark_text_2 = "9lessons"; // Watermark Text
function watermark_text($oldimage_name, $new_image_name)
{
global $font_path, $font_size, $water_mark_text_2;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $height = 300;
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate($image, 79, 166, 185);
imagettftext($image, $font_size, 0, 68, 190, $blue, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
return true;
}
$font_size = 30; // in pixcels
$water_mark_text_2 = "9lessons"; // Watermark Text
function watermark_text($oldimage_name, $new_image_name)
{
global $font_path, $font_size, $water_mark_text_2;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $height = 300;
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate($image, 79, 166, 185);
imagettftext($image, $font_size, 0, 68, 190, $blue, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
return true;
}
Watermark with Image - watermark_image()
Image watermark function, here you have to configure image water mark source. live demo
$image_path = "9lesson.png";
function watermark_image($oldimage_name, $new_image_name)
/{
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $height = 300;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 100);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
function watermark_image($oldimage_name, $new_image_name)
/{
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $height = 300;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 100);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
Image Form
Contains simple PHP and HTML form submit code. Here HTML form sending post request.
<?php
$demo_image= "";
if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")
{
$path = "uploads/";
$valid_formats = array("jpg", "bmp","jpeg");
$name = $_FILES['imgfile']['name'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)
{
$upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);
if($upload_status){
$new_name = $path.time().".jpg";
// Here you have to user functins watermark_text or watermark_image
if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))
$demo_image = $new_name;
}
}
else
$msg="File size Max 256 KB or Invalid file format.";
}
}
?>
// HTML Code
<form name="imageUpload" method="post" enctype="multipart/form-data" >
Upload Image
Image :<input type="file" name="imgfile" /><br />
<input type="submit" name="createmark" value="Submit" />
<?php
if(!empty($demo_image))
echo '<img src="'.$demo_image.'" />';
?>
</form>
$demo_image= "";
if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")
{
$path = "uploads/";
$valid_formats = array("jpg", "bmp","jpeg");
$name = $_FILES['imgfile']['name'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)
{
$upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);
if($upload_status){
$new_name = $path.time().".jpg";
// Here you have to user functins watermark_text or watermark_image
if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))
$demo_image = $new_name;
}
}
else
$msg="File size Max 256 KB or Invalid file format.";
}
}
?>
// HTML Code
<form name="imageUpload" method="post" enctype="multipart/form-data" >
Upload Image
Image :<input type="file" name="imgfile" /><br />
<input type="submit" name="createmark" value="Submit" />
<?php
if(!empty($demo_image))
echo '<img src="'.$demo_image.'" />';
?>
</form>















Cool :)
Hi Arun, How integrate this with Jquery crop image???
In what moment neet image to be watermarked
http://www.9lessons.info/2011/06/image-upload-and-cropping-with-php-and.html
Thank You
Nice Post... :)
Thanks,..
nice post
Nice Srinivas
nice!!! Thanks!
Hi Escort,
You can't able to use this code directly into image cropping. You have to modify this.
Why did you declare $font_path, $font_size and $water_mark_text_2 global??
The Problem/bug i just found out that is when any image named with another dot (.) like hi.buddy.jpg it constantly give error msg.
to avoid this problem need a change in it.
use this:
list($txt, $ext) = strtolower(substr(strrchr($name, '.'), 1));
instead of:
list($txt, $ext) = explode(".", $name);
in the line 55.
nice one:):)
Like That +1
Nice tut, thank u very much :D
Nice
nice work and helpful too!!!
Hi Srinivas,
Please help me I develop a music website, I have a problem. How I attach logo when user download music file and run in windows media player or VLC player than image or my website logo is show in player how do it pls help me.........
Thanks & Regards
Saleem M.
nice working but i hv one problem related this, i have wapsite and how i use watermark on video
Arun,
Good work,
I have faced height and width problem in this script.
I have uploaded image any size but its height and width are fix means (300px).
Can you help me to display image in actual size afrer watermarking?
Thanks
Bhargav Anadkat
Shree Gokul Infotech
@Bhargav comment imagecopyresampled line
@Arun Thanks keep it up.
nice one
I am working with wordpress themes, plugins and other php code things but didn't know what php is capable of making watermarks to your pictures and this process is so simple, just like picture resizing
thnx ...
sooo coool ^^
Finally some decent and simple solution to watermark my pictures.
I have blogs in niches where pictures are really important and they got shared all over the web and no one cares to add source to picture and watermarking really makes it easier to follow original picture source
Hi, there
Where should i config position of water mark to display?
Hello,
I happened to go through your blog a couple of days back and was impressed. Let me get straight to the point. We have a forum at http://www.chennaichatter.com exclusively for Chennai-ites where we discuss various issues pertaining to Chennai and the nation. I think it would be great if you join us and take part in the debates. It would add a whole new class to the discussions.
Have a great day!
Thomson.
Amazing
Congratulation! Thanks for your leasons. Gracias amigo por tus ejemplos, son de gran ayuda. Happy Merry Christmas. Felices Fiestas.
Thanks for the post.
I want to use this code with OOP . . . how can i implement ? ? ?
good job
Thank you for this.
Hi, there
how uploaded imageshack.us
$path = "imageshack.us";
Thank You
how can we translate only one word using google API in php
eg: this is a cat,and i want to convert only cat
I am very Happy to see this post...
This is very Use full Post...
Thanks
Hey! Epic script dude. I've got a question for you... that i HOPE! You can answer.
My question is:
Is there anyway to make a watermark adder, withouth saving/uploading the image to the server? or a autodelete, after x days? Would be very helpfull, because i want one for my forum, and i dont want them to upload tons of images to my server.
Hello, I would like to know if is possible instead of writing on a image that will be uploaded via form make it work with an already existing image on your server/folder making it possible to choose via input form what phrase is going to be written on the image, how can i do that? Is it too hard?
Thanks
Great work Arun
Nice post, a similar post which places the watermark at a random position. I hope this helps too.
thanks
http://ezref.info/How+to+add+watermark+to+images+on+the+fly+using+PHP.html