Current Location: Home> Latest Articles> Detailed Explanation of the Basic Usage of the imagecopymerge Function: How to Merge Two Images Step by Step?

Detailed Explanation of the Basic Usage of the imagecopymerge Function: How to Merge Two Images Step by Step?

M66 2025-06-21

In PHP, the imagecopymerge() function is a commonly used method for image manipulation. It allows you to merge part or all of one image into another. This function is primarily used for operations such as merging, overlaying, and cropping images, which are commonly used for creating watermarks, compositing images, and more. In this article, we will provide a detailed explanation of the basic usage of the imagecopymerge() function and how to use it to merge two images.

1. Overview of the imagecopymerge() Function

The prototype of the imagecopymerge() function is as follows:

bool imagecopymerge(resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_width, int $src_height, int $pct)

Explanation of Parameters:

  • $dst_image: The target image resource, which represents the image you want to merge another image into.

  • $src_image: The source image resource, which represents the image you want to merge into the target image.

  • $dst_x, $dst_y: The starting position (top-left corner) of the source image to be pasted onto the target image.

  • $src_x, $src_y: The starting position (top-left corner) where the copy operation begins in the source image.

  • $src_width, $src_height: The width and height of the region to be copied from the source image.

  • $pct: The transparency percentage for the merge, where 0 means fully transparent, and 100 means fully opaque.

This function copies the source image ($src_image) from the specified position onto the target image ($dst_image). The area to be copied can be specified with the width and height of the source image, and the transparency of the merge is controlled by $pct.

2. Basic Steps to Use the imagecopymerge() Function

Next, let's demonstrate how to merge two images using the imagecopymerge() function through a practical example.

Step 1: Load the Images

First, we need to load two images. PHP provides multiple functions to load images in different formats, such as imagecreatefromjpeg(), imagecreatefrompng(), etc. Let’s assume we have a background image and a logo image that needs to be overlaid.

// Load the target image (background image)  
$bg_image = imagecreatefromjpeg('background.jpg');  
<p>// Load the source image (logo image)<br>
$logo_image = imagecreatefrompng('logo.png');<br>

Step 2: Merge the Images Using imagecopymerge()

Assuming we want to merge logo.png onto background.jpg and set the logo's transparency to 50%, we can achieve this with the following code:

// Set the position for merging (top-left corner of the target image)  
$dst_x = 50;  
$dst_y = 50;  
<p>$src_x = 0;<br>
$src_y = 0;<br>
$src_width = imagesx($logo_image);<br>
$src_height = imagesy($logo_image);</p>
<p>// Set the transparency to 50%<br>
$pct = 50;</p>
<p>// Merge the images<br>
imagecopymerge($bg_image, $logo_image, $dst_x, $dst_y, $src_x, $src_y, $src_width, $src_height, $pct);<br>

Step 3: Output the Merged Image

After the merging operation is complete, we need to output the result image. You can use imagejpeg() (or other appropriate functions depending on the image format) to either save the merged image to a file or directly display it in the browser.

// Output the merged image to the browser  
header('Content-Type: image/jpeg');  
imagejpeg($bg_image);  
<p>// Release resources<br>
imagedestroy($bg_image);<br>
imagedestroy($logo_image);<br>

Alternatively, you can save the image to a file:

// Save the merged image to a file  
imagejpeg($bg_image, 'result.jpg');  

3. Points to Note

  • Transparency Setting: The imagecopymerge() function supports transparency (via the $pct parameter), but transparency only works for PNG and GIF images. If you are merging JPEG images, transparency will not be visible, as JPEG does not support transparent channels.

  • Resource Cleanup: After processing the images, remember to use imagedestroy() to release the image resources to prevent memory leaks.

  • Image Size: The size of the source image should be properly positioned according to the target image's size to avoid stretching or cropping the image. You can use imagesx() and imagesy() to get the width and height of an image to adjust the position and size of the source image as needed.

4. Summary of the Example

By following the steps above, you can use the imagecopymerge() function to achieve image composition, overlaying, and other effects, which are especially useful for creating watermarks, collages, and similar tasks. By properly adjusting the merge position, transparency, and size, you can flexibly handle different image composition requirements.