9lessons programming blog
Loading Search
Wednesday, November 2, 2011

PHP Image and Text Watermark

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.

PHP Image Watermark

Download Script     Text Live Demo     Image Live Demo

Developer
Arun Kumar Shekar
Arun Kumar Sekar
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;
}

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;
}


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>
Sponsored Links

Share this post

Comments
{ 38 comments }
Vitaut said...

Cool :)

All Star Escort said...

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

Anonymous said...

Nice Post... :)

aan said...

Thanks,..
nice post

Manussawin said...

Nice Srinivas

bloodshed said...

nice!!! Thanks!

Arun said...

Hi Escort,

You can't able to use this code directly into image cropping. You have to modify this.

Sanjoe said...

Why did you declare $font_path, $font_size and $water_mark_text_2 global??

ItsMeMiraaz said...

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.

Anonymous said...

nice one:):)

Mohit Bumb said...

Like That +1

Hamid Karimi said...

Nice tut, thank u very much :D

Mertcan EKREN said...

Nice

charmingsuru said...

nice work and helpful too!!!

samuel said...

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.

android zone said...

nice working but i hv one problem related this, i have wapsite and how i use watermark on video

Bhargav Anadkat said...

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

Arun said...

@Bhargav comment imagecopyresampled line

Bhargav Anadkat said...

@Arun Thanks keep it up.

Anonymous said...

nice one

David said...

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

معهد تطويري said...

thnx ...

sooo coool ^^

Mike said...

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

gadgetzoom.info said...

Hi, there

Where should i config position of water mark to display?

Thomson said...

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.

Godwin said...

Amazing

Marcelo said...

Congratulation! Thanks for your leasons. Gracias amigo por tus ejemplos, son de gran ayuda. Happy Merry Christmas. Felices Fiestas.

Anonymous said...

Thanks for the post.

NSDL Pan said...

I want to use this code with OOP . . . how can i implement ? ? ?

Amranul Islam said...

good job

Anonymous said...

Thank you for this.

live tv قنوات بث مباشر said...

Hi, there
how uploaded imageshack.us
$path = "imageshack.us";
Thank You

Anonymous said...

how can we translate only one word using google API in php
eg: this is a cat,and i want to convert only cat

Anonymous said...

I am very Happy to see this post...
This is very Use full Post...
Thanks

Mcmar said...

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.

caio said...

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

Jagannath said...

Great work Arun

Anonymous said...

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

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension