PHP Snippets
image_watermark
Information
(PHP Version >= 3)Functions used in this snippet: imagecreatefromjpeg, imagesx, imagesy, imagecreatefrompng, imagecreatetruecolor, imagealphablending, imagecopy, min.
Description
resource image_watermark( resource img, resource wmg )
Creates a watermark layover image over an image. The watermark image will be repeated on both the x and y axis. Using an image that will work as a seamless repeating image is best.
If the watermark is in PNG format and has its alpha transparceny set, the final watermark layover will be the same opacity as the watermark image.
Snippet
<?php
function image_watermark($img, $wmg)
{
$ix = imagesx($img);
$iy = imagesy($img);
$wx = imagesx($wmg);
$wy = imagesy($wmg);
$fmg = imagecreatetruecolor($ix, $iy);
imagealphablending($fmg, true);
imagecopy($fmg, $img, 0, 0, 0, 0, $ix, $iy);
for($sx = 0; $sx < $ix; $sx += $wx)
{
for($sy = 0; $sy < $iy; $sy += $wy)
{
imagecopy($fmg, $wmg, $sx, $sy, 0, 0, min($ix - $sx, $wx), min($iy - $sy, $wy));
}
}
return $fmg;
}
?>
Example
image_watermark Can be used in the following way:
<?php
// make our image into a php resource
$img = imagecreatefromjpeg('yourimage.jpg');
// make our watermark into a php resource
$wmg = imagecreatefrompng('your_watermark.png');
// apply the watermark
$fmg = image_watermark($img, $wmg);
// send the image to the browser
imagejpeg($fmg);
?>
Updated on Jan 9th at 8 pm.
2 Responses to image_watermark
Congratulations on a great web site. I am a new computer user and finding you was like coming home. Continued success.
Thanks, I really appreciate it :D