In this tutorial I’m going to show you how to add watermarks to images. The advantages to this script is that it will not effect the original image.
So this is the first bit of the code…
What this does is defines the functions and retrives the images.
$stamp = imagecreatefrompng(’stamp.png’);
$im = imagecreatefromjpeg(’photo.jpeg’);
Next…What this does is sets the size and the margins for the watermark.
$marge_right = 10; $marge_bottom = 10; $sx = imagesx($stamp); $sy = imagesy($stamp);
Now we need to calculate the position of the watermark
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp))
Now we need to tell the brower to display an image. And display the image we have created. Then destroy the image to reduce the server load.
header('Content-type: image/png'); imagepng($im); imagedestroy($im);
And thats everything.
Full Source
<?php // Load the stamp and the photo to apply the watermark to $stamp = imagecreatefrompng('stamp.png'); $im = imagecreatefromjpeg('photo.jpeg'); // Set the margins for the stamp and get the height/width of the stamp image $marge_right = 10; $marge_bottom = 10; $sx = imagesx($stamp); $sy = imagesy($stamp); // Copy the stamp image onto our photo using the margin offsets and the photo // width to calculate positioning of the stamp. imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp)); // Output and free memory header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?>






































November 9th, 2008 at 2:37 am
This is a usefull post, A demo would have been nice!
Havnt used php before regarding imagary
Thanks!