Friday, October 24, 2025
HomeLanguagesPHP | Imagick mergeImageLayers() Function

PHP | Imagick mergeImageLayers() Function

The Imagick::mergeImageLayers() function is an inbuilt function in PHP which is used to merge image layers into one.

Syntax:

Imagick Imagick::mergeImageLayers( int $layer_method )

Parameters: This function accepts a single parameter $layer_method which holds an integer value corresponding to one of LAYERMETHOD constants. You can also pass the constants directly like mergeImageLayers(Imagick::LAYERMETHOD_COMPAREANY).

List of LAYERMETHOD constants are given below:

  • imagick::LAYERMETHOD_UNDEFINED (0)
  • imagick::LAYERMETHOD_COALESCE (1)
  • imagick::LAYERMETHOD_COMPAREANY (2)
  • imagick::LAYERMETHOD_COMPARECLEAR (3)
  • imagick::LAYERMETHOD_COMPAREOVERLAY (4)
  • imagick::LAYERMETHOD_DISPOSE (5)
  • imagick::LAYERMETHOD_OPTIMIZE (6)
  • imagick::LAYERMETHOD_OPTIMIZEPLUS (7)
  • imagick::LAYERMETHOD_OPTIMIZEIMAGE (8)
  • imagick::LAYERMETHOD_OPTIMIZETRANS (9)
  • imagick::LAYERMETHOD_REMOVEDUPS (10)
  • imagick::LAYERMETHOD_REMOVEZERO (11)
  • imagick::LAYERMETHOD_COMPOSITE (12)
  • imagick::LAYERMETHOD_MERGE (13)
  • imagick::LAYERMETHOD_FLATTEN (14)
  • imagick::LAYERMETHOD_MOSAIC (15)

Return Value: This function returns an Imagick object containing the new image.

Exceptions: This function throws ImagickException on error.

Below programs illustrate the Imagick::mergeImageLayers() function in PHP:

Program 1:




<?php
  
// Create a new Imagick object
$imagick = new Imagick(
  
// Add another image in the same object
$imagick->addImage(new Imagick(
  
// Set the Opacity
$imagick->setImageOpacity(0.5);
  
// Merge the Layers
$result = $imagick->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
  
// Display the image
header("Content-Type: image/png");
echo $result->getImageBlob();
?>


Output:

Program 2:




<?php
  
// Create a new Imagick object
$imagick = new Imagick(
  
// Add another image in the same object
$imagick->addImage(new Imagick(
  
// Set the Opacity
$imagick->setImageOpacity(0.7);
  
// Merge the Layers
$result = $imagick->mergeImageLayers(11);
  
// Display the image
header("Content-Type: image/png");
echo $result->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagick.mergeimagelayers.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS