PHP | Imagick remapImage() Function

The Imagick::remapImage() function is an inbuilt function in PHP which is used to replace colors in an image with those defined by replacement. The colors are replaced with the closest possible color.

Syntax:

bool Imagick::remapImage( Imagick $replacement, int $dither )

Parameters:This function accepts two parameters as mentioned above and described below:

  • $replacement: It specifies an Imagick object containing the replacement colors.
  • $dither: It specifies an integer value corresponding to one of DITHERMETHOD constants. You can also pass constant directly like remapImage($replacement, imagick::DITHERMETHOD_RIEMERSMA);.

List of all DITHERMETHOD constants are given below:

  • imagick::DITHERMETHOD_UNDEFINED (0)
  • imagick::DITHERMETHOD_NO (1)
  • imagick::DITHERMETHOD_RIEMERSMA (2)
  • imagick::DITHERMETHOD_FLOYDSTEINBERG (3)

Return Value: This function returns TRUE on success.



Exceptions: This function throws ImagickException on error.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Imagick object
$imagick = new Imagick(
  
// Create another Imagick object
$replacement = new Imagick(
  
// Remap the image
$imagick->remapImage($replacement, imagick::DITHERMETHOD_RIEMERSMA);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Imagick object
$imagick = new Imagick(
  
// Create another Imagick object
$replacement = new Imagick(
  
// Remap the image
$imagick->remapImage($replacement, imagick::DITHERMETHOD_FLOYDSTEINBERG);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>

chevron_right


Output:

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.