5 Steps to Add Watermarks to Images with PHP GD Library

The GD library in PHP provides a powerful and easy way to manipulate images programmatically. One common use case for the GD library is adding watermarks to images to protect copyright or brand them.

In this post, we’ll walk through how to use PHP GD to add watermarks to JPG, PNG and GIF images.

Our latest article: Image Resizing Magic: How to Resize on Upload with PHP

Step 1: Overview of the GD Library

The GD library allows you to perform various image manipulation operations in PHP. Some key features include:

  • Loading existing image files in various formats (JPG, PNG, GIF)
  • Creating new blank images
  • Drawing shapes, lines, arcs and text
  • Getting and setting colors and transparency
  • Copying, resizing, cropping and rotating images
  • Adding watermarks and overlays
  • Outputting images to browser or file

To use the GD library, you need a PHP installation with GD support enabled. It comes pre-enabled in most PHP distributions. You can confirm it is installed by checking if the gd_info() function returns information about the installed GD version.

Step 2: Creating a Basic Watermarked Image

Let’s look at a simple example of generating an image with a text watermark using GD:

// Load the image 
$source_img = imagecreatefromjpeg('image.jpg');

// Create the watermark text
$text_color = imagecolorallocate($source_img, 0, 0, 0);  
imagestring($source_img, 5, 10, 10, 'Copyright MyWebsite.com', $text_color);

// Output and free memory
header('Content-Type: image/jpeg');

This loads the source JPG image, allocates a color for the text, draws the text at coordinate (10, 10), sets the header for outputting JPEG, outputs the image, and frees memory.

This will add a simple copyright text watermark to the bottom left corner of the image.

5 Steps to Add Watermarks to Images with PHP GD Library
5 Steps to Add Watermarks to Images with PHP GD Library

Step 3: Adding Watermark to Different Image Types

The above example adds a watermark to a JPG image. To add watermarks to other image types, we just need to change the imagecreatefromjpeg() function to the corresponding GD create function:

  • JPG images: imagecreatefromjpeg()
  • PNG images: imagecreatefrompng()
  • GIF images: imagecreatefromgif()

For outputting the watermarked image, use the respective GD output function:

  • JPG images: imagejpeg()
  • PNG images: imagepng()
  • GIF images: imagegif()

So the process remains the same, just change the input and output functions as per the image type.

Also Read: Mastering the Trail: Essential Off-Road Vehicle Maintenance for Peak Performance

Step 4: Adding Image Watermarks

In addition to text, we can also add image watermarks using the imagecopy() function.

For example:

// Load source and watermark images
$source_img = imagecreatefromjpeg('image.jpg');
$watermark_img = imagecreatefrompng('watermark.png');

// Copy watermark image onto source image
imagecopy($source_img, $watermark_img, 10, 10, 0, 0, 100, 100); 

// Output watermarked image 
header('Content-Type: image/jpeg');

// Free memory

The imagecopy() function takes the source image, watermark image, destination x, y coordinates, source x, y coordinates, width and height to copy over.

5 Steps to Add Watermarks to Images with PHP GD Library
5 Steps to Add Watermarks to Images with PHP GD Library

Step 5: Setting Watermark Transparency

By default, the watermark is copied over opaque. To add transparency, we can use a PNG watermark image which has transparency, or call imagealphablending() and imagesavealpha() before the copy operation:

imagealphablending($source_img, true);
imagesavealpha($source_img, true);

This will preserve the alpha channel of the watermark and let the watermark blend in partially with the source image.


Adding watermarks to images using the GD library in PHP is straightforward. The key steps are:

  • Load the source image with createfrom functions
  • Create watermark text or load image watermark
  • Use imagestring() or imagecopy() to add watermark
  • Output image to browser or file with JPEG/PNG/GIF functions
  • Free memory with imagedestroy()

Some additional options are setting transparency and colors of the watermark. Refer to the GD documentation for additional image manipulation features.

Our Blogging & SEO Series:

Common Problems and their Solutions to Add Watermarks to Images

1. Watermark text or image not appearing on image
  • Check that the path to the watermark file is correct
  • Ensure the watermark is added after loading the source image
  • Confirm JPEG/PNG/GIF createfrom and output functions match the image type


Verify source image is loaded first before adding watermark. Double check paths and image types used in GD functions.

2. Watermark appearing distorted or squished
  • The dimensions used when copying watermark image are incorrect
  • The source and watermark images are different sizes


Pass the correct width and height to imagecopy() to match watermark dimensions. Resize watermark to match source image size.

3. Watermark not positioned at desired location
  • Incorrect x,y coordinates provided to imagestring() or imagecopy()


Calculate and provide proper x,y coordinates that represent the desired location.

4. Transparency of PNG watermark not working
  • Transparency not enabled with imagealphablending() and imagesavealpha()


Call imagealphablending($source_img, true) before adding watermark to preserve transparency.

5. Out of memory errors when generating watermarked images
  • Not freeing GD image resources after generation


Always call imagedestroy() to free up resources after outputting watermarked image to avoid memory leaks.

1 thought on “5 Steps to Add Watermarks to Images with PHP GD Library”

Leave a Comment

All you need to know about Apple’s new iPhone 14 series iPhone 14 with emergency SOS function, Apple Ultra Watch unveiled Apple iPhone 14 Pro and 14 Pro Max arrive with pill notch, Always-On Display, and new cameras Apple iPhone 14 Pro and 14 Pro Max arrive with pill notch, Always-On Display, and new cameras Google Unveils Launch Date of its iPhone 14 Competitors