PHP Image and Text Watermark
Wall Script
Wall Script
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>
web notification

72 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. Congratulation! Thanks for your leasons. Gracias amigo por tus ejemplos, son de gran ayuda. Happy Merry Christmas. Felices Fiestas.

    ReplyDelete
  13. Thanks for the post.

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

    ReplyDelete
  15. Thank you for this.

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

    ReplyDelete
  17. 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
  18. I am very Happy to see this post...
    This is very Use full Post...
    Thanks

    ReplyDelete
  19. 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
  20. 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
  21. 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
  22. Excellent post.. thank you for this.

    ReplyDelete
  23. 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
  24. how to use multiple file upload watermark in this script

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

    ReplyDelete
  26. Nice arun it is working fine.

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

    ReplyDelete
  28. Thanks!

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

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

    ReplyDelete
  30. 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
  31. nice stuff,
    how can i set position of watermark text

    ReplyDelete
  32. How to change the colour of the font

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

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

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

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

    ReplyDelete
  37. hi thnx, its working....

    ReplyDelete
  38. Thanks for Fixing Comment form :)

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

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

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

    ReplyDelete
  42. 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
  43. getting error Message: imagettftext(): Could not find/open font

    ReplyDelete
  44. how can i do this with already uploaded image means without form submiting

    ReplyDelete

mailxengine Youtueb channel
Make in India
X