Open In App

PHP | Imagick mergeImageLayers() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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



Last Updated : 04 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads