Friday, October 10, 2025
HomeLanguagesPHP | Imagick opaquePaintImage() Function

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:




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

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS