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'); imagejpeg($source_img); imagedestroy($source_img);
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.
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:
- PNG images:
- GIF images:
For outputting the watermarked image, use the respective GD output function:
- JPG images:
- PNG images:
- GIF images:
So the process remains the same, just change the input and output functions as per the image type.
Step 4: Adding Image Watermarks
In addition to text, we can also add image watermarks using the
// 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'); imagejpeg($source_img); // Free memory imagedestroy($source_img); imagedestroy($watermark_img);
imagecopy() function takes the source image, watermark image, destination x, y coordinates, source x, y coordinates, width and height to copy over.
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
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
imagecopy()to add watermark
- Output image to browser or file with JPEG/PNG/GIF functions
- Free memory with
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:
- Cracking the Code of Local SEO: Dominate the SERPs in Your Area
- Demystifying Technical SEO: Optimizing Your Website for Search Engines
- Unveiling the Power of Backlinks: Boosting Your Website’s SEO Authority
- Mastering the Art of On-Page SEO: A Step-by-Step Guide for Success
- High CPC keywords 2023 For Web Site & YouTube Channel
- Most Expensive WordPress Theme | Most Expensive WordPress Theme for Blogging
- Sydney Pro Theme Free Download | Sydney Pro WordPress Theme
- Best Free Blogger Theme for AdSense Approval
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
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
imagedestroy() to free up resources after outputting watermarked image to avoid memory leaks.