PHP | Imagick opaquePaintImage() Function

The Imagick::opaquePaintImage() function is an inbuilt function in PHP which is used to replace the target color with the specified fill color value of any pixel that matches the target color.

Syntax:

bool Imagick::opaquePaintImage( $target, $fill, $fuzz,
                   $invert, $channel = Imagick::CHANNEL_DEFAULT ] )

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



  • $target: This parameter holds the ImagickPixel object or target color which need to be changed.
  • $fill: This parameter holds the color for replacement.
  • $fuzz: This parameter holds the amount of fuzz that will be in float type.
  • $invert: Switch between 0 and 1 where 0 is normal and 1 is inverse.
  • $channel: This parameter holds the Imagick channel constants that provide any channel constant which is valid for channel mode. More than one channel can be combined using bitwise operators.

Return Value: This function returns TRUE on success or FALSE on failure.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Image Path
$imagePath
   
// Target color
$target = 'rgb(255, 255, 255)';
   
// Replacing target color with another color
$fill = 'rgb(255, 234, 128)';
$fuzz = '0.1';
  
// Initialize invert variable
$invert = '0';
   
$imagick = new \Imagick($imagePath);
  
// Set the image format
$imagick->setimageformat('png');
    
$imagick->opaquePaintImage(
    $target, $fill, $fuzz * \Imagick::getQuantum(), $invert
);
  
// Use despeckleimage() function to reduce
// the speckle noise in an image
$imagick->despeckleimage();
    
header("Content-Type: image/png");
  
// Display the output image
echo $imagick->getImageBlob();
   
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Image Path
$imagePath
   
// Target color
$target = 'rgb(255, 255, 255)';
   
// Replacing target color with another color
$fill = 'rgb(21, 200, 236)';
$fuzz = '0.1';
  
// Initialize invert variable
$invert = '0';
   
$imagick = new \Imagick($imagePath);
  
// Set the image format
$imagick->setimageformat('png');
    
$imagick->opaquePaintImage(
    $target, $fill, $fuzz * \Imagick::getQuantum(), $invert
);
    
header("Content-Type: image/png");
  
// Display the output image
echo $imagick->getImageBlob();
   
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.opaquepaintimage.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.