Current Location: Home> Latest Articles> Implement transparent layer merging with imagecopymerge()

Implement transparent layer merging with imagecopymerge()

M66 2025-05-23

In PHP's image processing library GD, imagecopymerge() is a common function that combines an image onto another image and allows the transparency of the merge (actually the "level of mixing" of the merge). But if you want to control transparency more finely, such as using real alpha channels (rather than just simple opacity adjustments), you need to use it with imagecolorallocatealpha() .

This article will introduce how to use imagecolorallocatealpha() to define colors with alpha channels (transparency), and combine imagecopymerge() to achieve layer merging with transparent effects.

1?? Basic introduction

  • imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)
    Used to assign a color with an alpha channel to the specified image. The value of $alpha is from 0 (completely opaque) to 127 (completely transparent).

  • imagecopymerge(resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h, int $pct)
    Copy the $src_im image to $dst_im and set the blending degree with $pct (0 = completely transparent, 100 = completely opaque).

It should be noted that imagecopymerge() does not actually support real alpha channel mixing, it simply simulates transparent effects by adjusting pixel brightness. If a finer translucent merge is needed, you can use imagecopy() and fill the transparent area with imagecolorallocatealpha() in the source image.

2?? Sample code: Layer merge with transparent effects

Here is a complete example showing how to create a layer of transparent background and merge it onto the main image.

 <?php
// Create the main image(background)
$background = imagecreatetruecolor(400, 300);
$white = imagecolorallocate($background, 255, 255, 255);
imagefilledrectangle($background, 0, 0, 400, 300, $white);

// Create transparent layers
$layer = imagecreatetruecolor(200, 150);
imagesavealpha($layer, true);
$transparent = imagecolorallocatealpha($layer, 0, 0, 0, 127); // Completely transparent
imagefill($layer, 0, 0, $transparent);

// Draw a translucent red rectangle on the layer
$red_alpha = imagecolorallocatealpha($layer, 255, 0, 0, 63); // translucent
imagefilledrectangle($layer, 20, 20, 180, 130, $red_alpha);

// Merge the layer to the main image(Notice imagecopymerge Not supported alpha aisle)
imagecopy($background, $layer, 100, 75, 0, 0, 200, 150);

// Output result
header('Content-Type: image/png');
imagepng($background);

// Clean the memory
imagedestroy($background);
imagedestroy($layer);
?>

3?? Code parsing

  • imagecreatetruecolor() is used to create a true color canvas.

  • imagesavealpha() opens the flag to save the alpha channel to ensure that transparent information is not lost.

  • imagecolorallocatealpha() defines a color with transparency, where we use it to create a completely transparent background and a translucent red.

  • imagecopy() instead of imagecopymerge() is used to preserve the alpha channel. If you use imagecopymerge() , the GD library forces the alpha mixing to close, using only $pct to simulate transparency.

  • Finally, use imagepng() to output the generated image.