Search code examples
phppngimage-manipulationtransparencyimagick

PHP Imagick setImageOpacity destroys transparency and does nothing


Here's the thing.

I hava a simple snippet in PHP like this regarding a transparent image:

$im = new Imagick('some-transparent-image.png');
$im->setImageOpacity(0.3);
$im->writeImage('output.png');

The file output should be a transparent image with lower opacity, right?

Well, the output is an image with black color where it was supposed to be transparent and the image opacity is exactly the same.

Does it have to do with configuration or am i missing something?

Thank you in advance


Solution

  • Unfortunately setImageOpacity affects the whole image, so to leave the transparent areas transparent replace the following:

    $im->setImageOpacity(0.3); 
    

    with :

    $im->evaluateImage(Imagick::EVALUATE_MULTIPLY, 0.3, Imagick::CHANNEL_ALPHA);