Miranda Roberts
Miranda Roberts

Reputation: 341

Alpha channel failing after Imagick update

I have an application that frequently uses Imagick's PHP module to recolor and composite images. I recently upgraded the server's software from PHP 5.4 to PHP 7.0 and subsequently upgraded Imagick as well. I am now running ImageMagic 7.0.3 with the module 3.4.3. I have verified this in my site's phpinfo() and with the server command convert -version. The update for both was done in cPanel's WHM.

I use the following function to color an image using a given hex while preserving it's alpha shape and it's worked just fine up until updating.

protected function recolor($obj, $hex)
{
    $obj->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
    $obj->setImageBackgroundColor('#' . str_replace('#', '', $hex));
    $obj->setImageAlphaChannel(Imagick::ALPHACHANNEL_SHAPE);
    return $obj;
}

Example of image successfully recolored (pre-update): enter image description here

Example of the same input with current behavior (post-update): enter image description here

I'm not really sure what's causing this new behavior. There is definitely color being applied, but it's not being applied in the shape of the supplied image. In addition, everything that got recolored after the base layer (the first layer that gets recolored in this process) doesn't seem to be showing at all, with the only layers showing above being ones that are not recolored.

Edit: Here is one of the original input images that gets recolored:

https://i.sstatic.net/iyaoo.png - Base Color

https://i.sstatic.net/5W1nr.png - Background (Composites under base)

https://i.sstatic.net/LUmWd.png - Lineart (Composites over base)

The composite process has been added below. First the function recolors all the applicable layer objects and then composites them and masks them to the "color" image (the "base" layer posted above in the shape of the wolf). Then that image is composited onto the background and the lines and logo added on top to create the final image.

$this->recolor($this->color, $baseHex);
$this->recolor($this->eyes, $eyesHex);
$this->recolor($this->eyebrows, $eyebrowsHex);
$this->recolor($this->pads, $padsHex);
$this->recolor($this->nose, $noseHex);
$this->recolor($this->claws, $clawsHex);
$this->recolor($this->tongue, $tongueHex);

$this->image->newImage(800, 598, new \ImagickPixel('transparent'));
$this->image->compositeImage($this->color, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->compositeMarkings();
$this->image->compositeImage($this->eyes, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->image->compositeImage($this->eyebrows, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->image->compositeImage($this->pads, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->image->compositeImage($this->nose, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->image->compositeImage($this->claws, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->image->compositeImage($this->tongue, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->mask($this->image, $this->color);
$this->bg->compositeImage($this->image, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->bg->compositeImage($this->lines, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->bg->compositeImage($this->logo, Imagick::COMPOSITE_DEFAULT, 0, 0);
$this->bg->setImageColorSpace(Imagick::COLORSPACE_SRGB);`

Upvotes: 1

Views: 395

Answers (1)

fmw42
fmw42

Reputation: 53202

Imagemagick 7.0.3.0 has a bug and I can reproduce your issue in the command line.

magick 5W1nr.png \( iyaoo.png -alpha extract -background "#988777" -alpha shape \) -compose over -composite LUmWd.png  -compose over -composite result7030.png

enter image description here

But it works fine in the current Imagemagick 7.0.6.9 in the command line.

magick 5W1nr.png \( iyaoo.png -alpha extract -background "#988777" -alpha shape \) -compose over -composite LUmWd.png  -compose over -composite result7069.png

enter image description here

So I suggest you upgrade your version of Imagemagick

Upvotes: 2

Related Questions