Adding Watermarks to Images in PHP: Text and Logo Watermarking Guide

Adding Watermarks to Images in PHP: Text and Logo Watermarking Guide

Introduction

Watermarking images is essential for branding, copyright protection, and preventing unauthorized use. Using PHP, we can dynamically add text or logo watermarks to images before displaying or saving them.

In this guide, you’ll learn how to:

Add a text watermark to images using GD Library.
Apply a logo watermark to images in PHP.
Use Imagick for more advanced watermarking.
Automatically watermark images during upload.

By the end, you’ll have a PHP-based watermarking system for securing your images.

1. Setting Up PHP for Watermarking

Check if GD Library or Imagick is Installed

For GD Library:

php -m | grep gd

If missing, enable it in php.ini:

extension=gd

For Imagick:

php -m | grep imagick

If missing, install it using:

sudo apt install php-imagick

GD Library is built-in, while Imagick offers more advanced image processing.

2. Adding a Text Watermark to an Image Using GD Library

Text watermarks are simple and effective for branding.

Example: Add Text Watermark to an Image

function addTextWatermark($source, $destination, $text = "© MyWebsite") {
    $image = imagecreatefromjpeg($source);
    $color = imagecolorallocatealpha($image, 255, 255, 255, 50); // White with transparency
    $font = __DIR__ . '/arial.ttf'; // Path to a TrueType font file
    $fontSize = 20;
    $x = imagesx($image) - 200;
    $y = imagesy($image) - 50;

    imagettftext($image, $fontSize, 0, $x, $y, $color, $font, $text);
    imagejpeg($image, $destination, 90);

    imagedestroy($image);
    echo "Text watermark added successfully!";
}

// Example usage
addTextWatermark("input.jpg", "watermarked.jpg", "© MyBrand");

Explanation:

imagettftext() adds text with a custom font.
Uses RGBA colors for transparency (50% opacity).
Places the watermark in the bottom right corner.

3. Adding a Logo Watermark to an Image Using GD Library

A logo watermark adds branding and professional appeal to images.

Example: Overlay a Logo on an Image

function addLogoWatermark($source, $watermark, $destination) {
    $image = imagecreatefromjpeg($source);
    $logo = imagecreatefrompng($watermark);

    $logoWidth = imagesx($logo);
    $logoHeight = imagesy($logo);
    $x = imagesx($image) - $logoWidth - 10;
    $y = imagesy($image) - $logoHeight - 10;

    imagecopy($image, $logo, $x, $y, 0, 0, $logoWidth, $logoHeight);

    imagejpeg($image, $destination, 90);
    imagedestroy($image);
    imagedestroy($logo);

    echo "Logo watermark added successfully!";
}

// Example usage
addLogoWatermark("input.jpg", "logo.png", "watermarked.jpg");

Why PNG for Watermarks?

Preserves transparency in logos.
Blends better with the image background.

4. Adding Watermarks Using Imagick (Advanced Method)

Imagick offers better image quality and effects than GD.

Example: Text Watermark Using Imagick

function addTextWatermarkImagick($source, $destination, $text = "© MyBrand") {
    $image = new Imagick($source);
    $draw = new ImagickDraw();
    $draw->setFont("Arial");
    $draw->setFontSize(24);
    $draw->setFillColor("rgba(255, 255, 255, 0.5)");
    $draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
    
    $image->annotateImage($draw, 10, 10, 0, $text);
    $image->writeImage($destination);
    
    echo "Text watermark added with Imagick!";
}

// Example usage
addTextWatermarkImagick("input.jpg", "watermarked.jpg", "© MyWebsite");

Imagick provides sharper text rendering and better placement options.


Example: Logo Watermark Using Imagick

function addLogoWatermarkImagick($source, $watermark, $destination) {
    $image = new Imagick($source);
    $logo = new Imagick($watermark);
    
    $image->compositeImage($logo, Imagick::COMPOSITE_OVER, 
        $image->getImageWidth() - $logo->getImageWidth() - 10,
        $image->getImageHeight() - $logo->getImageHeight() - 10
    );
    
    $image->writeImage($destination);
    echo "Logo watermark added with Imagick!";
}

// Example usage
addLogoWatermarkImagick("input.jpg", "logo.png", "watermarked.jpg");

Imagick makes it easy to overlay images while preserving transparency.

5. Automatically Watermarking Images During Upload

To automatically watermark images when users upload them, modify the upload script.

Example: Watermark on Image Upload

if ($_FILES['image']['error'] == 0) {
    $uploadDir = "uploads/";
    $imagePath = $uploadDir . basename($_FILES['image']['name']);

    if (move_uploaded_file($_FILES['image']['tmp_name'], $imagePath)) {
        addLogoWatermark($imagePath, "logo.png", $imagePath);
        echo "Image uploaded and watermarked!";
    } else {
        echo "Upload failed!";
    }
}

This script ensures every uploaded image gets a watermark automatically.

Best Practices for Watermarking Images in PHP

Use PNG format for logo watermarks to preserve transparency.
Place text watermarks in the bottom right for better visibility.
Apply opacity to avoid intrusive branding.
Use Imagick for high-quality watermarks.
Automate watermarking for uploaded images.

Conclusion

Adding watermarks to images in PHP is essential for branding and copyright protection. Using GD Library and Imagick, you can create text and logo watermarks dynamically.

Text watermarks are simple but effective.
Logo watermarks add a professional touch.
Automating watermarking ensures consistent branding.

By implementing these techniques, you can secure your images and maintain professional branding on your website. 🚀

Leave a Reply