How to programmatically add watermark to an image in PHP

Updated: January 12, 2024 By: Guest Contributor Post a comment

Introduction

Adding watermarks to images is a valuable technique for protecting digital assets or branding visual content. In this tutorial, we’ll discover how to programmatically add watermarks to images using PHP. By integrating simple PHP scripts, you’ll be able to manipulate images to include text or logo watermarks dynamically.

Prerequisites

  • A development environment with PHP (version 5.6 or higher) installed
  • GD library for PHP enabled (usually enabled by default on most PHP installations)
  • Access to a text editor or PHP IDE
  • Sample images to work with
  • A PNG image to be used as the watermark (for a logo watermark)

Understanding the GD Library

The GD library is a graphics drawing library that provides tools for manipulating image data. With GD, you can create, open, and modify images in various formats such as JPEG, PNG, and GIF. Before you begin coding, ensure that the GD library is enabled in your PHP setup, which you can check using phpinfo() function.

Basic Setup

<?php
  // Checks if GD extension is loaded
  if (!extension_loaded('gd')) {
    die('GD extension is not loaded. Please enable it in your php.ini configuration.');
  }
  // Define path to images
  $sourceImagePath = 'path/to/your/source-image.jpg';
  $watermarkImagePath = 'path/to/your/watermark.png';

  // Check if the files exist
  if (!file_exists($sourceImagePath) || !file_exists($watermarkImagePath)) {
    die('Images do not exist in the specified paths.');
  }
?>

Creating a Text Watermark

A text watermark is a simple way to add ownership details without the need for a separate image file. PHP’s GD library enables you to overlay text over an existing image. Below is a step-by-step guide to achieve this:

<?php
  // Load the source image
  $sourceImage = imagecreatefromjpeg($sourceImagePath);
  $textColor = imagecolorallocatealpha($sourceImage, 255, 255, 255, 50);  // white color with 50% transparency
  $fontPath = 'path/to/your/font.ttf';  // Absolute path to your TrueType font file
  $fontSize = 20;
  $angle = 0;
  $xPosition = 10;
  $yPosition = 30;
  $text = 'This is my watermark';

  // Add the text watermark
  imagettftext($sourceImage, $fontSize, $angle, $xPosition, $yPosition, $textColor, $fontPath, $text);

  // Save or output the watermarked image
  imagejpeg($sourceImage, 'path/to/your/watermarked-image.jpg');
  imagedestroy($sourceImage);
?>

Adding a Logo Watermark

Using an image as a watermark (such as a logo) provides a versatile form of branding. Let’s work through it:

<?php
  // Load the source and the watermark images
  $sourceImage = imagecreatefromjpeg($sourceImagePath);
  $watermarkImage = imagecreatefrompng($watermarkImagePath);

  // Get the width and height of the watermark image
  $watermarkWidth = imagesx($watermarkImage);
  $watermarkHeight = imagesy($watermarkImage);

  // Set the position of the watermark
  $positionRight = 10;
  $positionBottom = 10;
  $xPosition = imagesx($sourceImage) - $watermarkWidth - $positionRight;
  $yPosition = imagesy($sourceImage) - $watermarkHeight - $positionBottom;

  // Merge the watermark onto the source image
  imagecopy($sourceImage, $watermarkImage, $xPosition, $yPosition, 0, 0, $watermarkWidth, $watermarkHeight);

  // Save or output the merged image
  imagejpeg($sourceImage, 'path/to/your/watermarked-image.jpg');
  imagedestroy($sourceImage);
  imagedestroy($watermarkImage);
?>

Best Practices and Considerations

  • Memory usage: Working with large images can lead to high memory consumption. Ensure your PHP memory_limit setting can accommodate such operations.
  • Watermark transparency: When using an image as a watermark, ensure it has transparency (alpha channel) for a more professional appearance.
  • File permissions: Ensure that your scripts have the necessary permissions to read the source files and write the watermarked images.
  • Quality control: Adjust the quality parameter in the imagejpeg() function to control the quality of the saved image.
  • Experiment with placement: The watermark’s position can dynamically change to avoid obscuring important parts of the image.

Conclusion

By integrating watermarking in your image processing, you can protect your images while maintaining your brand presence. PHP with its GD library simplifies image manipulation, including adding watermarks.

This guide outlined the essentials of watermarking images with PHP. You can expand upon these basics by adding feature checks, error handling, and by refining the watermark’s appearance to suit your specific needs. With the power of PHP and GD library at your fingertips, watermarking is just one of the many image processing features you can implement with minimal effort. Happy coding!