-
I'm trying to detect whether an image asset has transparency, in a twig template. Looking at craft\base\Image I thought that So to test it, I created a small Module with a PHP function, available as a Twig filter, to load the asset and return the value from public function getIsTransparent(Asset $asset)
{
if (!isset($asset)) {
return null;
} else {
$images = Craft::$app->getImages();
$localCopy = TransformHelper::getLocalImageSource($asset);
$image = $images->loadImage($localCopy, true);
return $image->getIsTransparent();
}
} ...but no matter what image is loaded, it always returns 1. However, if I create a new ImageMagick instance and test the image like so: public function getIsTransparent(Asset $asset)
{
if (!isset($asset)) {
return null;
} else {
$localCopy = TransformHelper::getLocalImageSource($asset);
$imgk = new Imagick();
$imgk->readImage($localCopy);
return $imgk->getImageAlphaChannel() ? 1 : 0;
}
} This second solution works as expected. I've solved the issue I had, but I can't help thinking I'm doing something wrong here with the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Note that I've since discovered that isTransparent doesnt appear to work in craft 4.3.6, but does in 4.3.5. As such, I've filed a bug report. |
Beta Was this translation helpful? Give feedback.
Note that I've since discovered that isTransparent doesnt appear to work in craft 4.3.6, but does in 4.3.5. As such, I've filed a bug report.