Open In App

PHP | Imagick mergeImageLayers() Function

Last Updated : 04 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
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
<?php

// Create a new Imagick object
$imagick = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190918234528/colorize1.png');

// Add another image in the same object
$imagick->addImage(new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20191126190119/geeksforgeeks-copy.png'));

// 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
<?php

// Create a new Imagick object
$imagick = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190918234528/colorize1.png');

// Add another image in the same object
$imagick->addImage(new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20191126191401/geeksforgeekshalf.png'));

// 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

Next Article

Similar Reads