BigToach.com

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($fmgtrue);
    
imagecopy($fmg$img0000$ix$iy);
    for(
$sx 0$sx $ix$sx += $wx)
    {
        for(
$sy 0$sy $iy$sy += $wy)
        {
            
imagecopy($fmg$wmg$sx$sy00min($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);
?>

Added on Jan 9th at 8 pm by Scott - 2 Comments
Updated on Jan 9th at 8 pm.


2 Responses to image_watermark

Curtel Knudge (knudge[at]yahoo[dot]com) commented on Aug 13th at 9 am
Congratulations on a great web site. I am a new computer user and finding you was like coming home. Continued success.
Scott Roach commented on Aug 13th at 5 pm
Thanks, I really appreciate it :D

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: