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


Messages In This Thread
How to Watermark your Images - by Gaijin - 10-08-2009, 08:59 PM
RE: How to Watermark your Images - by Headshot - 10-08-2009, 09:00 PM
RE: How to Watermark your Images - by Gaijin - 10-08-2009, 09:01 PM
RE: How to Watermark your Images - by Grizzly - 10-10-2009, 03:20 PM
RE: How to Watermark your Images - by Gaijin - 10-10-2009, 06:19 PM
RE: How to Watermark your Images - by Bartdevil - 10-11-2009, 02:53 AM
RE: How to Watermark your Images - by Gaijin - 10-11-2009, 02:55 AM
RE: How to Watermark your Images - by HuNt3R - 10-11-2009, 09:24 AM
RE: How to Watermark your Images - by Gaijin - 10-13-2009, 01:48 AM
RE: How to Watermark your Images - by Akshay* - 10-13-2009, 01:51 AM
RE: How to Watermark your Images - by immi - 10-13-2009, 02:33 AM
RE: How to Watermark your Images - by Gaijin - 10-13-2009, 05:36 PM

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

Forum Jump:


Users browsing this thread: 2 Guest(s)