Merging two images with PHP

Merging two images with PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Merging Two Images in PHP: A Practical Guide

Merging two images in PHP usually implies compositing one image over another, often to apply a watermark or overlay graphics. The core mechanism relies on the GD library, which is built into most PHP installations but requires explicit initialization to access its functions. You cannot simply load an image and expect it to behave like a canvas; you must create an in-memory resource first.

To place "Image One" (the foreground) onto "Image Two" (the background), the standard approach involves creating two separate image resources, loading them into memory using imagecreatefrom* functions appropriate for their file formats (e.g., JPEG or PNG), and then defining a blending operation. The specific function required depends entirely on how you want the images to interact at the pixel level.

The most common scenario is applying a watermark where the top image sits strictly above the bottom one without transparency blending. In this case, imagecopy() is the correct tool. It copies a rectangular portion of the source image onto the destination image. Crucially, imagecopy() does not account for alpha channels (transparency); it simply paints pixels. If your overlay image has a transparent background, using imagecopy() will result in a solid rectangle being pasted over your main image, likely obscuring details you intended to keep visible.

If the source image contains an alpha channel and you wish to respect that transparency so that parts of the background show through, you must use imagecopymergegray() or imagecopy(). However, for pure compositing where one image is solid, imagecopy() remains the most performant choice because it avoids unnecessary pixel blending calculations.

Here is a minimal implementation pattern for merging two images where the first is placed over the second:

// Load the background image (Image Two)
$bg = imagecreatefrompng('path/to/image-two.png'); // Use jpg/jpeg if applicable

// Load the overlay image (Image One)
$fg = imagecreatefrompng('path/to/image-one.png'); // Or imagecreatefromjpeg()

// Move the foreground image to a specific coordinate on the background
// The logic here is: destination x, destination y, source width, source height
imagecopy($bg, $fg, 0, 0, 0, 0, imagesx($fg), imagesy($fg));

// Output the merged result
header('Content-Type: image/png');
imagepng($bg);
imagedestroy($bg);
imagedestroy($fg);

A frequent pitfall developers encounter involves coordinate systems. The imagecopy() function signature is imagecopy(destination, source, dest_x, dest_y, src_x, src_y, src_width, src_height). It is easy to accidentally swap the dimensions or offsets, resulting in the image appearing shifted or cropped incorrectly. Always ensure you are passing the width and height of the source image at the end, not the destination.

Another critical consideration is the order of operations regarding resource destruction. PHP's garbage collector handles resources eventually, but explicitly calling imagedestroy() is best practice to free up memory immediately, especially in high-traffic applications processing hundreds of images per minute. Failing to do so can lead to memory exhaustion errors under load.

If you are working within a larger framework like Laravel, the logic remains identical, but you might wrap these calls in a service class or use the Intervention/image package for a more fluent syntax if the raw GD functions feel too low-level for your team's preferences. However, raw GD offers granular control over blending modes and color manipulation that higher-level libraries often abstract away.

When merging images programmatically, always validate that the input files exist and are valid images before attempting to load them. Attempting to imagecreatefrom... a non-existent file or an image with unsupported encoding will throw a warning and return false, which should be caught and handled gracefully to prevent script failure.

Our network