Open In App

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:



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


Article Tags :