Support Forums

Full Version: How to Watermark your Images
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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!
Great, man, you are god.
(10-08-2009, 09:00 PM)Headshot Wrote: [ -> ]Great, man, you are god.

Thank you man.
Just trying to help. Blackhat
Roflmao
Nice tutorial. Well written and easy to follow.
(10-10-2009, 03:20 PM)Grizzly Wrote: [ -> ]Nice tutorial. Well written and easy to follow.

Thanks man!
Yeye
Nice tutorial, written in a good format, thanks =)
(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
Great TUT!!!!!!!
(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.
Thanks ninja geek your posts are always amazing
Pages: 1 2