Thursday, June 18, 2026
HomeLanguagesPHP | Imagick transparentPaintImage() Function

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 above.

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:




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


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6965 POSTS0 COMMENTS