PHP Image and Text Watermark
Wall Script
Follow Me:
Wednesday, November 02, 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>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


73 comments:

  1. 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

    ReplyDelete
  2. Hi Escort,

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

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

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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.

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. @Bhargav comment imagecopyresampled line

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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

    ReplyDelete
  11. Hi, there

    Where should i config position of water mark to display?

    ReplyDelete
  12. 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.

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

    ReplyDelete
  14. Thanks for the post.

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

    ReplyDelete
  16. Thank you for this.

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

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

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

    ReplyDelete
  20. 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.

    ReplyDelete
  21. 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

    ReplyDelete
  22. 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

    ReplyDelete
  23. Excellent post.. thank you for this.

    ReplyDelete
  24. cool 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

    ReplyDelete
  25. how to use multiple file upload watermark in this script

    ReplyDelete
  26. nice... this is what i need...

    ReplyDelete
  27. Nice arun it is working fine.

    ReplyDelete
  28. how to add new ext such as png and gif

    ReplyDelete
  29. very nice tutorial, good for my projects

    ReplyDelete
  30. Thanks!

    I was using imagecopyresized instead of imagecopyresampled and my images were not looking good.

    ReplyDelete
  31. Hi, the demo links do not work! Please reopen

    ReplyDelete
  32. Hi 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.

    ReplyDelete
  33. nice stuff,
    how can i set position of watermark text

    ReplyDelete
  34. How to change the colour of the font

    ReplyDelete
  35. text watermark to be transparent use imagecolortransparent and imagealphablending

    ReplyDelete
  36. not valid for png image uploads, how we can do for png ?

    ReplyDelete
  37. Thanks alot for the Script. It was very Helpful

    ReplyDelete
  38. Any way to create a gradient text, with automatic center-alignment of the text as well?

    ReplyDelete
  39. hi thnx, its working....

    ReplyDelete
  40. Thanks for Fixing Comment form :)

    ReplyDelete
  41. Have you combined this watermark / image resize with dropzonejs?? I have had quite big difficulties with that... Thanks for nice tutorial!

    ReplyDelete
  42. 256*1024 i want to increase size of image uploading .. please help me
    Arun

    ReplyDelete
  43. Hi, SRINIVAS TAMADA
    Your watermarks demo does not work.

    ReplyDelete
  44. 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 !

    ReplyDelete
  45. getting error Message: imagettftext(): Could not find/open font

    ReplyDelete

Make in India