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