Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: isTransparent always returning 1 in Craft 4.3.6 #12565

Closed
tomkiss opened this issue Jan 23, 2023 · 5 comments
Closed

[4.x]: isTransparent always returning 1 in Craft 4.3.6 #12565

tomkiss opened this issue Jan 23, 2023 · 5 comments
Assignees

Comments

@tomkiss
Copy link

tomkiss commented Jan 23, 2023

What happened?

Description

Whilst attempting to use isTransparent in a Craft module, I'm seeing true returned for all images.

Steps to reproduce

In order to access the property in twig, I have a module set up something like this:

public function getFunctions()
{
    return [
        new TwigFunction('isTransparent', [$this, 'getIsTransparent']), 
    ];
}

public function getIsTransparent(Asset $asset) 
{
    if (!isset($asset)) {
        return null;
    } else {
        $images = Craft::$app->getImages();
        $localCopy = ImageTransforms::getLocalImageSource($asset);
        $image = $images->loadImage($localCopy, true);
        return $image->getIsTransparent();
    }
}

The isTransparent property can then be accessed in twig via something like:

{% set myAsset = craft.assets.id(123).one() %}
{{isTransparent(myAsset)}}

Expected behavior

I am expecting that transparent images such as pngs, gifs or webp that contain an alpha channel would return 1. Anything opaque, such as any jpg would return null.

Actual behavior

It will always return 1. Regardless of whether or not it has an alpha channel, or if its transparent or not.

Having looked at this for a while now I'm wondering if this might be a bug. Loading imagemagick directly, within the module, and running its getImageAlphaChannel() method returns the expected result.

NOTE: If I downgrade to Craft CMS 4.3.5 I do not see this issue.

See attached module as example of my test: imagehelpers-module.zip

Craft CMS version

4.3.6.1

PHP version

8.1.12

Operating system and version

MacOS with DDev

Database type and version

No response

Image driver and version

imagick 3.7.0 / gd 2.3.3

@brianjhanson
Copy link
Contributor

Thanks for the report! We've tracked this down to an issue in the underlying Imagine library. It looks like in version 1.3 they introduced a change that will result in isTransparent returning true when $palette->supportsAlpha() is true (which happens to be the case for the RGB palette that most things will use).

I've opened an issue over there but I'm also going to see if we have a good way to work around it. I just wanted to provide an update for future travelers (and before I forget the result of our digging)

@brianjhanson
Copy link
Contributor

This one was quite the journey! Thanks to the library authors I've opened a PR that replaces the isTransparent value with something more reliable for the next 3 and 4 releases.

@tomkiss
Copy link
Author

tomkiss commented Jan 25, 2023

Aha, nice sleuthing!
Thanks for the quick response Brian, and the PR 🙌

@brandonkelly
Copy link
Member

This is fixed for the next Craft 3 and 4 releases, via #12571.

@brandonkelly
Copy link
Member

Craft 3.7.64 and 4.3.7 have been released with that fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants