Adding Image as Watermark using PHP

Sovary May 27, 2022 763
1 minute read

To protect your image or want to mark an image is made by you, adding watermark on photo is the option to identify. In PHP I have wrote an article how to adding text on photo as watermark . For this version I will apply an image watermark instead of using text.

The below steps will covers the quick basics how to do we apply another image as the watermark with example.

Summary Functions

The below are functions that we will use in this article.

Functions Description
imagecreatefrompng() Create object image from png file
imagecreatefromjpeg() Create object image from jpeg file
imagecopy() Copy image or part of image
getimagesize() Get width & height image info
imagedestroy() Free clear memory associated with image
imagejpeg() Save new image as jpeg file

Add Watermark on Image

Example #1

<?php
    $sourceImage = "original_photo.jpg";
    $watermark = "watermarked.png";
    $watermakedImage = "photo_with_watermark.jpg";// Not exist yet

    // Watermark image object
    $imgWatermark = imagecreatefrompng($watermark);
    
    // Create image object from jpeg file.
    $imgToBeWatermark= imagecreatefromjpeg($sourceImage);
    // Get size source image, width & height
    $size = getimagesize($sourceImage);
    // Get size watermark, width & height
    $sizeWm = getimagesize($watermark);
    
    $watermarkWidth = $sizeWm[0];
    $watermarkHeight = $sizeWm[1];
    // Image to be watermarked quality (0 to 100)
    $quality = 60; 
    
    // Position X = left, Y = top
    $posX= $size[0] - $watermarkWidth - 50;
    $posY= $size[1] - $watermarkHeight - 100;
    // Apply watermark image on source image
    imagecopy($imgToBeWatermark, $imgWatermark,
    $posX, $posY,
    0, 0,
    $watermarkWidth, $watermarkHeight);

    // Save new jpegimage
    imagejpeg($imgToBeWatermark, $watermakedImage, $quality);

    // Clear memory
    imagedestroy($imgToBeWatermark);
    imagedestroy($imgWatermark);
?>

Output #1

 

PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him