10-08-2009, 08:59 PM
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.
Requirements:
Functions:
Let's Start:
We start with the following code:
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"
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.
So far so good, we use imagecreatetruecolor() to create black image with the size of our watermark image.
In the next step we get the info of the original image, and create right type of image.
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.
All we need to do now is to merge the two created resources with the function imagecopymerge().
The last parameter defines opacity of the watermark.
And finally we just need to output the image with the imagejpeg() function.
With imagedestroy() we clear any memory association.
To use this script you just need to load it as following:
Thank you for reading!
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:
Requirements:
- Basic PHP knowledge.
- Host/Server with GD2 installed/enabled.
- Some time.
Functions:
- header()
- imagecreatefrompng()
- imagecreatefromgif()
- imagecreatefrompjpeg()
- imagecreatetruecolor()
- getimagesize()
- imagecopymerge()
- imagejpeg()
- 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_y, 0, 0, $width, $height, 50);
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">
Source (Click to View)
PHP Code:
<?php
if(!isset($_GET['img'])) {
die();
}
header('content-type: image/jpeg');
$path = $_GET['img'];
$watermark = imagecreatefrompng('watermark.png');
$width = imagesx($watermark);
$height = imagesy($watermark);
$image = imagecreatetruecolor($width, $height);
$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();
}
$pos_x = $img[0] - $width;
$pos_y = $img[1] - $height;
imagecopymerge($image, $watermark, $pos_x, $pos_y, 0, 0, $width, $height, 50);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
Thank you for reading!