Skip to content

Preserve alpha channel in Imagick negative effect#877

Merged
mlocati merged 1 commit into
php-imagine:developfrom
nlemoine:fix/imagick-negative-alpha
Jun 3, 2026
Merged

Preserve alpha channel in Imagick negative effect#877
mlocati merged 1 commit into
php-imagine:developfrom
nlemoine:fix/imagick-negative-alpha

Conversation

@nlemoine

@nlemoine nlemoine commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

effects()->negative() on the Imagick driver inverts the alpha channel as well as the colours, which GD doesn't do. Result: an opaque image comes back fully transparent. The colour values are right, you just can't see them.

Cause is negateImage(false, \Imagick::CHANNEL_ALL). CHANNEL_ALL includes alpha, so opaque alpha flips to transparent. GD avoids this because IMG_FILTER_NEGATE only touches colour.

Fix is to negate with CHANNEL_ALL & ~CHANNEL_ALPHA so only the colour channels flip (same thing intervention/image does). The Gmagick driver doesn't need it: in the gd-gmagick docker image CHANNEL_ALL leaves an opaque image opaque, and Gmagick's CHANNEL constants are sequential enum values rather than bitmasks, so the & ~ trick wouldn't apply anyway.

Added testNegatePreservesAlpha to AbstractEffectsTest so every driver gets covered. It negates a fully-opaque image and asserts it stays opaque. Fails on Imagick before the fix (alpha 100 to 0), passes after. Verified the full suite green in both the gd-imagick and gd-gmagick docker images.

@nlemoine nlemoine force-pushed the fix/imagick-negative-alpha branch from 9639b50 to 3e7b8a9 Compare June 3, 2026 08:30
@nlemoine nlemoine changed the title Preserve alpha channel in Imagick/Gmagick negative effect Preserve alpha channel in Imagick negative effect Jun 3, 2026
@mlocati

mlocati commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

@mlocati mlocati merged commit 5c93f04 into php-imagine:develop Jun 3, 2026
21 checks passed
@nlemoine nlemoine deleted the fix/imagick-negative-alpha branch June 3, 2026 09:56
@nlemoine

nlemoine commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for merging this so fast @mlocati!

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants