Open In App

PHP | Imagick mapImage() Function

Last Updated : 05 Sep, 2019
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
The Imagick::mapImage() function is an inbuilt function in PHP which is used to replace the colors of an image with the closest color from a reference image. Syntax:
bool Imagick::mapImage( Imagick $map, float $dither )
Parameters: This function accepts two parameters as mentioned above and described below:
  • $map: This parameter holds the Imagick object.
  • $dither: This parameter holds a boolean which decides whether to add noise or not.
Return Value: This function returns TRUE on success. Errors/Exceptions: This function throws ImagickException on error. Below programs illustrate the Imagick::mapImage() function in PHP: Program 1: php
<?php

// Create a new imagick object
$imagick1 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190823154611/geeksforgeeks24.png');
 
// Create another imagick object
$imagick2 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190901195451/mattefloodfillimage.png');
 
// Mapping the image with noise
$imagick1->mapImage($imagick2, true);
 
header("Content-Type: image/jpg"); 
   
// Display the output image 
echo $imagick1->getImageBlob();

?>
Output: Program 2: php
<?php

// Create a new imagick object
$imagick1 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190823154611/geeksforgeeks24.png');
 
// Create another imagick object
$imagick2 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190901195451/mattefloodfillimage.png');
 
// Mapping the image without noise
$imagick1->mapImage($imagick2, false);
 
header("Content-Type: image/jpg"); 
   
// Display the output image 
echo $imagick1->getImageBlob();

?>
Output: Reference: https://www.php.net/manual/en/imagick.mapimage.php

Similar Reads