Monday, December 22, 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
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS