Overlay PNG on JPEG with Transparency (Watermark) in PHP GD

Last Updated May 28, 2023 by . First Published in 2010.

Overlay PNG on JPEG with Transparency (Watermark) in PHP GD

This example is a common way to overlay png watermarks and stamps to photos and copyrighted images using PHP using GD library and imagecopy.

This is the code that I use on my website to watermark my own photos. You can see an example below where the script will overlay the PNG watermark signature and copyright message in the bottom right-hand corner. The copyright year is added dynamically so each new year I upload a photo will have the current year copyright. This is much better than manually updating the signature image each year.

Vantage point on the way towards Llanberis
Vantage point on the way towards Llanberis

The function takes in two parameters - the source image filename and the destination filename to save the watermarked image. The function will work with JPEG, GIF and PNG files, however, PNG8 (265 colour PNG) does not support alpha so you would need to convert any PNG8 to PNG24 before running this. GIF only supports 256 colours so alpha will not work, but transparency does.

php
function GenerateWatermarkedImage($srcFilename, $destFilename)
{
    list($width, $height, $type, $attr) = getimagesize($srcName);
    $ext =  strtolower(pathinfo($srcName, PATHINFO_EXTENSION));

    // load the image based on the extension
    if ($ext == 'image/png') {
        $image = imagecreatefrompng($srcFilename);
    } else if ($ext == 'jpg' || $ext == 'jpeg') {
        $image = imagecreatefromjpeg($srcFilename);
    } else if ($ext == 'gif') {
        $image = imagecreatefromgif($srcFilename);
    }

    // set transparencey and alpha
    imageinterlace($image, true);
    imagealphablending($image, true);

    // load the watermark
    $watermarkfile = 'watermark.png';
    list($watermarkWidth, $watermarkHeight, $watermarkType, $watermarkAttr) = getimagesize($watermarkfile);
    $watermark = imagecreatefrompng($watermarkfile);

    // add the watermark in the bottom right corner
    imagecopy($image, $watermark, $width-$watermarkWidth, $height-$watermarkHeight, 0, 0, $watermarkWidth, $watermarkHeight);

    // optionally add the date
    $year = date("Y");
    $white = imagecolorallocatealpha($image, 255, 255, 255, 0);
    imagettftext($image, 8, 90, $width-6, $height-16, $white, FONT, '©  '.$year);

    // save the watermarked image
    if ($ext == 'png') {
        imagepng($image, $destFilename, 9);
    } else if ($ext == 'jpg' || $ext == 'jpeg') {
        imagejpeg($image, $destFilename, 80);
    } else if ($ext == 'gif') {
        imagegif($image, $destFilename);
    }

    // free up resources
    if (is_resource($image)) {
        imagedestroy($image);
    }
    if (is_resource($watermark)) {
        imagedestroy($watermark);
    }
}

Was this article helpful to you?
 
Comments

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.