Adding Image as Watermark using PHP

Sovary May 27, 2022 798
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him