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
10 changes: 9 additions & 1 deletion src/Drivers/Imagick/Modifiers/FillModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Drivers/Imagick/Modifiers/FillModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}