By Peter Radics :: web design - cleanweb.hu
Tuesday 9 January 2007
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
web designer, programmer