Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Watermark your Images
#1
Welcome and Enjoy!

Intro:
In this tutorial, I will show you how to use PHP and GD to watermark your images.

Preview:
Now the watermark text doesn't look nice as you can see but that is my mistake in creating process.
Image copyright http://artgerm.deviantart.com Wrote:[Image: watermark.php?img=tut.jpg]

Requirements:
  • Basic PHP knowledge.
  • Host/Server with GD2 installed/enabled.
  • Some time.

Functions:
  1. header()
  2. imagecreatefrompng()
  3. imagecreatefromgif()
  4. imagecreatefrompjpeg()
  5. imagecreatetruecolor()
  6. getimagesize()
  7. imagecopymerge()
  8. imagejpeg()
  9. imagedestroy()


Let's Start:
We start with the following code:
PHP Code:
if(!isset($_GET['img'])) {
    die();
}

header('content-type: image/jpeg'); 

Frist we check if the url is like "watermark.php?img=path_to_image", if it's not we let the script end running with the die() function.
If all goes well we define with header() that the page is an image.

In the next step we define $path, wich holds the full path to the image. "watermark.php?img=path_to_image"
PHP Code:
$path $_GET['img']; 

Now we use imagecreatefrompng() to create new image resource for use within our scritp.
And then imagesx to get the width of the watermark image, and imagesy to get the height of the watermark image.
NOTE: Since we are using imagecreatefrompng(), the watermark image needs to be an PNG image.
PHP Code:
$watermark imagecreatefrompng('watermark.png');  

$width imagesx($watermark);  
$height imagesy($watermark); 

So far so good, we use imagecreatetruecolor() to create black image with the size of our watermark image.
PHP Code:
$image imagecreatetruecolor($width$height); 

In the next step we get the info of the original image, and create right type of image.
PHP Code:
$img getimagesize($path);

switch(
$img['mime']) {
    case 
'image/jpeg':
        
$image imagecreatefromjpeg($path);
        break;
    case 
'image/gif':
        
$image imagecreatefromgif($path);
        break;
    case 
'image/png':
        
$image imagecreatefrompng($path);
        break;
    default:
        die();


getimagesize() will get us the size and file type of the original image.
We use the "mime" type returned by "getimagesize()" to create right type of resource.

Now let us define the position of our watermark.
This will place the watermark at the bottom right corner.
PHP Code:
$pos_x $img[0] - $width;  
$pos_y $img[1] - $height

All we need to do now is to merge the two created resources with the function imagecopymerge().
PHP Code:
imagecopymerge($image$watermark$pos_x$pos_y00$width$height50); 
The last parameter defines opacity of the watermark.

And finally we just need to output the image with the imagejpeg() function.
PHP Code:
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark); 

With imagedestroy() we clear any memory association.

To use this script you just need to load it as following:
Code:
<img src="watermark.php?img=tut.jpg">


Thank you for reading!
Reply
#2
Great, man, you are god.
Reply
#3
(10-08-2009, 09:00 PM)Headshot Wrote: Great, man, you are god.

Thank you man.
Just trying to help. Blackhat
Roflmao
Reply
#4
Nice tutorial. Well written and easy to follow.
Do you know how to make games? Do you know 3d Modeling? If so then visit
[Image: logo.png]
Reply
#5
(10-10-2009, 03:20 PM)Grizzly Wrote: Nice tutorial. Well written and easy to follow.

Thanks man!
Yeye
Reply
#6
Nice tutorial, written in a good format, thanks =)
Reply
#7
(10-11-2009, 02:53 AM)Bartdevil Wrote: Nice tutorial, written in a good format, thanks =)

Thank you, I'm glad you like it. Yeye
Reply
#8
Great TUT!!!!!!!
Reply
#9
(10-11-2009, 09:24 AM)HuNt3R Wrote: Great TUT!!!!!!!

Thank you very much!
edit: crap 110mb closed my account and I muss find another host for the files.
Reply
#10
Thanks ninja geek your posts are always amazing
[Image: 2d75599e9a.png]:superman:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating Dynamic images with PHP Gaijin 15 8,203 01-21-2012, 06:17 PM
Last Post: Dube

Forum Jump:


Users browsing this thread: 1 Guest(s)