PHP | Imagick transparentPaintImage() Function

The Imagick::transparentPaintImage() function is an inbuilt function in PHP which Paints pixels transparent. Paints pixels matching the target color transparent. This method is available if Imagick has been compiled against ImageMagick version 6.3.8 or avobe.

Syntax:

bool Imagick::transparentPaintImage ( mixed $target, float $alpha, float $fuzz, bool $invert )

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

  • $target: The target color to paint transparent.
  • $alpha: The range of transparency: 1.0 is fully opaque and 0.0 is fully transparent.
  • $fuzz: If TRUE paints any pixel that does not match the target color.
  • $invert: The image whitePoint.
    Return Value: This function returns True on success.

Below program illustrates the Imagick::transparentPaintImage() function in PHP:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
//$source="gfg_350X350.png";
$target="transpaintimg.png";
$color="rgb(39, 194, 255)";
$alpha="0.5";
$fuzz="0.1";
$inverse="normal";
$imagick = new \Imagick(realpath($source));
   
    //Need to be in a format that supports transparency
    $imagick->setimageformat('png');
   
    $imagick->transparentPaintImage(
        $color, $alpha, $fuzz * \Imagick::getQuantum(), $inverse
    );
   
    //Not required, but helps tidy up left over pixels
    $imagick->despeckleimage();
       
    $canvas = new Imagick();
    $canvas->newPseudoImage(
        $imagick->getImageWidth(),
        $imagick->getImageHeight(),
        "pattern:checkerboard"
    );
       
    $canvas->compositeimage($imagick, \Imagick::COMPOSITE_ATOP, 0, 0);
    $canvas->setImageFormat('png');
   
    header("Content-Type: image/png");
    echo $canvas->getImageBlob();
    $canvas->WriteImage($target);
?>

chevron_right


Output:

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