diff --git a/src/Drivers/Imagick/Modifiers/FillModifier.php b/src/Drivers/Imagick/Modifiers/FillModifier.php index 329902f49..318966131 100644 --- a/src/Drivers/Imagick/Modifiers/FillModifier.php +++ b/src/Drivers/Imagick/Modifiers/FillModifier.php @@ -90,7 +90,15 @@ private function fillAllWithColor(Imagick $frame, ImagickPixel $pixel): void try { $draw = new ImagickDraw(); $draw->setFillColor($pixel); - $draw->rectangle(0, 0, $frame->getImageWidth(), $frame->getImageHeight()); + + if ($pixel->getColorValue(Imagick::COLOR_ALPHA) === 1.0) { + // an opaque color replaces every pixel, so the frame can be reset to the color + // directly, which is many times faster than rasterizing a full size rectangle + $draw->color(0, 0, Imagick::PAINT_RESET); + } else { + $draw->rectangle(0, 0, $frame->getImageWidth(), $frame->getImageHeight()); + } + $frame->drawImage($draw); } catch (ImagickException | ImagickDrawException | ImagickPixelException $e) { throw new ModifierException( diff --git a/tests/Unit/Drivers/Imagick/Modifiers/FillModifierTest.php b/tests/Unit/Drivers/Imagick/Modifiers/FillModifierTest.php index 6dd29ca93..ce8d8d551 100644 --- a/tests/Unit/Drivers/Imagick/Modifiers/FillModifierTest.php +++ b/tests/Unit/Drivers/Imagick/Modifiers/FillModifierTest.php @@ -35,4 +35,30 @@ public function testFillAllColor(): void $this->assertEquals('cccccc', $image->colorAt(420, 270)->toHex()); $this->assertEquals('cccccc', $image->colorAt(540, 400)->toHex()); } + + public function testFillAllOpaqueColorCoversTransparentAndAnimatedImages(): void + { + foreach (['circle.png', 'animation.gif'] as $filename) { + $image = $this->readTestImage($filename); + $image->modify(new FillModifier(new Color(204, 204, 204))); + for ($index = 0; $index < count($image); $index++) { + $this->assertColor(204, 204, 204, 255, $image->colorAt(0, 0, $index)); + $this->assertColor( + 204, + 204, + 204, + 255, + $image->colorAt($image->width() - 1, $image->height() - 1, $index), + ); + } + } + } + + public function testFillAllTranslucentColorBlends(): void + { + $image = $this->readTestImage('blocks.png'); + $image->modify(new FillModifier(new Color(255, 255, 255, .5))); + $this->assertColor(255, 128, 128, 255, $image->colorAt(540, 400), tolerance: 1); + $this->assertColor(128, 128, 255, 255, $image->colorAt(420, 270), tolerance: 1); + } }