Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

### NEXT (YYYY-MM-DD)
- Fix the Imagick driver inverting the alpha channel in `effects()->negative()` (an opaque image became fully transparent); it now excludes the alpha channel, matching the GD driver

### 1.5.2 (2026-01-09)
- Do not call curl_close on PHP 8.0+ (#875, @dmaicher)
Expand Down
5 changes: 4 additions & 1 deletion src/Imagick/Effects.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function gamma($correction)
public function negative()
{
try {
$this->imagick->negateImage(false, \Imagick::CHANNEL_ALL);
// \Imagick::CHANNEL_ALL includes the alpha channel, so a plain negateImage()
// call would invert transparency along with the color (an opaque image would
// become fully transparent). Exclude the alpha channel to match the GD driver.
$this->imagick->negateImage(false, \Imagick::CHANNEL_ALL & ~\Imagick::CHANNEL_ALPHA);
} catch (\ImagickException $e) {
throw new RuntimeException('Failed to negate the image', $e->getCode(), $e);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/tests/Effects/AbstractEffectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ public function testNegate()
$this->assertEquals('#ffff00', (string) $image->getColorAt(new Point(10, 10)));
}

public function testNegatePreservesAlpha()
{
if (!$this->getDriverInfo()->hasFeature(Info::FEATURE_NEGATEIMAGE)) {
$this->isGoingToThrowException('Imagine\Exception\NotSupportedException');
}
$palette = new RGB();
$imagine = $this->getImagine();

// Negating must invert the color channels only, not the alpha channel:
// a fully-opaque image must remain fully opaque (see the Imagick driver,
// where CHANNEL_ALL used to invert the alpha and turn the image transparent).
$image = $imagine->create(new Box(20, 20), $palette->color('ff0'));
$image->effects()
->negative();

$pixel = $image->getColorAt(new Point(10, 10));

$this->assertEquals('#0000ff', (string) $pixel);
$this->assertSame(100, $pixel->getAlpha());
}

public function testGamma()
{
$palette = new RGB();
Expand Down
Loading