Commands we will use:
header()
imagefrompng()
imagefromjpeg()
imagecopymerge()
imagejpeg()
imagedestroy()
getimagesize()
The method:
1. send your browser an image header, because you will create a new image:
header("content-type: image/jpeg");2. get your watermark image, and the other image what your want to get watermarked too:
$wm = imagecreatefrompng("watermark.png");
$img = imagecreatefromjpeg("myimage.jpg");
4. if you want to center your watermark on your image, you should calculate it's position:
$wmsize = getimagesize("watermark.png");
$imgsize = getimagesize("myimage.jpg");
$destx = ($imgsize[0] - $wmsize[0]) / 2;
$desty = ($imgsize[1] - $wmsize[1]) / 2;
3.use the imagecopymerge() function to copy your watermark to your image:
imagecopymerge($img,$wm,$destx,$desty, 0, 0, $wmsize[0], $wmsize[1], 75);
- the last parameter of the above function is the alpha transparency
4. send the new image to the browser:
imagejpeg($img);5. finally, destroy your sources:
imagedestroy($img);
imagejpeg($img);
The full source:
<?php
header("content-type: image/jpeg");
$wm = imagecreatefrompng("watermark.png");
$img = imagecreatefromjpeg("myimage.jpg");
$wmsize = getimagesize("watermark.png");
$imgsize = getimagesize("myimage.jpg");
$destx = ($imgsize[0] - $wmsize[0]) / 2;
$desty = ($imgsize[1] - $wmsize[1] / 2;
imagecopymerge($img,$wm,$destx,$desty, 0, 0, $wmsize[0], $wmsize[1], 75);
imagejpeg($img);
imagedestroy($img);
imagedestroy($wm);
?>
By Peter Radics web design - cleanweb.hu

Comments (6)
The shortest and greatest watermark script on the net..
Greate work, thanks 4 share
very good.
Excellent ! Thank You
Does not work with transparent PNG with shadows
It'll be much better, if u use imagecopy() Instead of imagecopymerge()
Sometimes the <a href="http://blog.pracucci.com/2008/08/30/automatic-selection-of-the-best-image-corner-where-apply-a-watermark-with-php-and-imagick/">place of the watermark</a> matters. I have written an article about this.