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 Demo
Developer
Arun Kumar Sekar
Engineer
Chennai, INDIA
Engineer
Chennai, INDIA
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 :)
ReplyDeleteHi Arun, How integrate this with Jquery crop image???
ReplyDeleteIn 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... :)
ReplyDeleteThanks,..
ReplyDeletenice post
Nice Srinivas
ReplyDeletenice!!! Thanks!
ReplyDeleteHi Escort,
ReplyDeleteYou 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??
ReplyDeleteThe Problem/bug i just found out that is when any image named with another dot (.) like hi.buddy.jpg it constantly give error msg.
ReplyDeleteto 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:):)
ReplyDeleteLike That +1
ReplyDeleteNice tut, thank u very much :D
ReplyDeletenice work and helpful too!!!
ReplyDeleteHi Srinivas,
ReplyDeletePlease 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
ReplyDeleteArun,
ReplyDeleteGood 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
ReplyDelete@Arun Thanks keep it up.
ReplyDeletenice one
ReplyDeleteI 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
ReplyDeletethnx ...
ReplyDeletesooo coool ^^
Finally some decent and simple solution to watermark my pictures.
ReplyDeleteI 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
ReplyDeleteWhere should i config position of water mark to display?
Amazing
ReplyDeleteCongratulation! Thanks for your leasons. Gracias amigo por tus ejemplos, son de gran ayuda. Happy Merry Christmas. Felices Fiestas.
ReplyDeleteThanks for the post.
ReplyDeleteI want to use this code with OOP . . . how can i implement ? ? ?
ReplyDeletegood job
ReplyDeleteThank you for this.
ReplyDeleteHi, there
ReplyDeletehow uploaded imageshack.us
$path = "imageshack.us";
Thank You
how can we translate only one word using google API in php
ReplyDeleteeg: this is a cat,and i want to convert only cat
I am very Happy to see this post...
ReplyDeleteThis is very Use full Post...
Thanks
Hey! Epic script dude. I've got a question for you... that i HOPE! You can answer.
ReplyDeleteMy 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?
ReplyDeleteThanks
Great work Arun
ReplyDeleteNice post, a similar post which places the watermark at a random position. I hope this helps too.
ReplyDeletethanks
http://ezref.info/How+to+add+watermark+to+images+on+the+fly+using+PHP.html
Excellent post.. thank you for this.
ReplyDeletecool script, but again, this is pointless when it save and uploads to the server, if you cud post a simple script to auto delete images after x days would be good man
ReplyDeletehow to use multiple file upload watermark in this script
ReplyDeletenice... this is what i need...
ReplyDeleteNice arun it is working fine.
ReplyDeleteNice :)
ReplyDeletehow to add new ext such as png and gif
ReplyDeletevery nice tutorial, good for my projects
ReplyDeleteThanks!
ReplyDeleteI was using imagecopyresized instead of imagecopyresampled and my images were not looking good.
Hi, the demo links do not work! Please reopen
ReplyDeleteHi thanks for the script :D, I'm from Mexico and I have a problem, I do not want the image is resized, I want to be provisionally maintain the dimensions and maximum 1000px. I should clarify that I know nothing of this textofue PHP and translated by google :S hope its rrespuesta. Thank you again.
ReplyDeletenice stuff,
ReplyDeletehow can i set position of watermark text
I was looking for it. thank you
ReplyDeleteHow to change the colour of the font
ReplyDeletetext watermark to be transparent use imagecolortransparent and imagealphablending
ReplyDeletevery nice tutorial..
ReplyDeletevery nice tutorial..
ReplyDeletenot valid for png image uploads, how we can do for png ?
ReplyDeleteThanks alot for the Script. It was very Helpful
ReplyDeleteNice work :)
ReplyDeleteAny way to create a gradient text, with automatic center-alignment of the text as well?
ReplyDeletehi thnx, its working....
ReplyDeletethanks
ReplyDeleteThanks for Fixing Comment form :)
ReplyDeleteHave you combined this watermark / image resize with dropzonejs?? I have had quite big difficulties with that... Thanks for nice tutorial!
ReplyDeletecoolweb site thankss
ReplyDeleteTNX FRND :)
ReplyDeleteThanks :)
ReplyDeletemany thanks guys ♥ :)
ReplyDelete256*1024 i want to increase size of image uploading .. please help me
ReplyDeleteArun
make it as 2048 * 1024 ...
DeleteHi, SRINIVAS TAMADA
ReplyDeleteYour watermarks demo does not work.
Nice Script , How to output image without resizing the image > I need watermark to be added and save the same image with default dimensions instead of making it 600px !
ReplyDeletegetting error Message: imagettftext(): Could not find/open font
ReplyDeleteCheck the font
Deletehow can i do this with already uploaded image means without form submiting
ReplyDelete