Open In App

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:

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



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

Program 1:




<?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();
   
?>

Output:

Program 2:




<?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();
   
?>

Output:

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


Article Tags :