Imager-Craft icon indicating copy to clipboard operation
Imager-Craft copied to clipboard

Unable to set image alpha channel

Open bymayo opened this issue 4 years ago • 4 comments

We're getting the error ImagickException Unable to set image alpha channel when using effects grayscale, gamma and colorBlend using Imagick:

{% set heroImageTransform = craft.imager.transformImage(
    heroImage, 
    { 
            width: 700,
            height: 700,
            effects: { 
                grayscale: true, 
                gamma: 1.1, 
                colorBlend: ['#262672']
            }
    }
) %}

This seems to work fine locally but not on our staging env. The PHP versions are the same and so are the ImageMagick versions:

Version: ImageMagick 6.7.8-9 2019-08-08 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

I've checked the It seems to be choking on this part of the plugin:

if (ImagerService::$imageDriver === 'imagick') {
    /** @var ImagickImage $imageInstance */
    $imagickInstance = $imageInstance->getImagick();

    $hasTransparency = $imagickInstance->getImageAlphaChannel();
    $imagickInstance->setImageType(\Imagick::IMGTYPE_GRAYSCALE);

    if ($hasTransparency) {
        $imagickInstance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
        $imagickInstance->setBackgroundColor(new \ImagickPixel('transparent'));
    }
}

Is this possibly related to the PR https://github.com/aelvan/Imager-Craft/pull/142, which I'm unsure if it got merged or not, and also it seems old.

Using:

PHP 7.0.33 MySQL 5.7 Craft CMS 3.3.4.1 Imager 2.2.0

bymayo avatar Oct 10 '19 09:10 bymayo

What type of image is it (gif, png, jpg), and is it one image in particular or all images/...of that type? What line is the stacktrace pointing to, is it $imagickInstance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);? Can you also check what the imagick version is (see "Image driver & version" under Utilities > System report)?

aelvan avatar Oct 12 '19 17:10 aelvan

The file is a PNG with transparency, but i'm sure we did try it with a Jpeg but still get this the error.

The line is like you said:

$imagickInstance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);

The Image Driver version is Imagick 3.4.4 (ImageMagick 6.7.8-9). We recently update the server to this version to match our local one (Which doesn't throw this error)

bymayo avatar Oct 14 '19 08:10 bymayo

I was able to reproduce this by compiling ImageMagick 6.7.8-10 and Imagick 3.4.4. But I've not been able to find a solution or workaround for the issue. Something is missing/wrong with that version of ImageMagick and/or Imagick, the code works in both older and newer versions I've tested.

My only advise would be to either upgrade ImageMagick, or work around the issue. The grayscale effect can be replaced by modulate: [100, 0, 100], but the same issue is in the colorBlend effect, and replacing that with something that gives the exact same result is a bit more difficult.

Please not that you can also make your own, custom effects if you need more complex logic to produce the desired effect.

aelvan avatar Oct 15 '19 10:10 aelvan

@aelvan Okay yeah, I double checked locally and that version is Imagick 3.4.3 (ImageMagick 6.9.6-2) so I might downgrade to these versions if possible as I know these work. Will try this and update the issue.

bymayo avatar Oct 15 '19 12:10 bymayo