From e5f141fb6516a50aba7560a3cd3a65307ae71f46 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 31 Oct 2023 14:47:44 +0100 Subject: [PATCH] Fix bug in RemoveAnimationModifiers --- src/Drivers/Gd/Image.php | 12 ++++++++++++ .../Gd/Modifiers/RemoveAnimationModifier.php | 7 ++++++- src/Drivers/Imagick/Image.php | 7 +++++++ .../Modifiers/RemoveAnimationModifier.php | 18 +++++++++++------- .../Modifiers/RemoveAnimationModifierTest.php | 3 ++- .../Modifiers/RemoveAnimationModifierTest.php | 3 ++- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/Drivers/Gd/Image.php b/src/Drivers/Gd/Image.php index 55fe560c2..04b3b4eda 100644 --- a/src/Drivers/Gd/Image.php +++ b/src/Drivers/Gd/Image.php @@ -27,6 +27,18 @@ public function __construct(protected Collection $frames, protected int $loops = // } + public function frames(): Collection + { + return $this->frames; + } + + public function setFrames(Collection $frames): ImageInterface + { + $this->frames = $frames; + + return $this; + } + public function getIterator(): Traversable { return $this->frames; diff --git a/src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php b/src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php index 6043ffa94..744430bed 100644 --- a/src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php +++ b/src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php @@ -7,9 +7,12 @@ use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; +use Intervention\Image\Traits\CanCheckType; class RemoveAnimationModifier implements ModifierInterface { + use CanCheckType; + public function __construct(protected int $position = 0) { // @@ -21,6 +24,8 @@ public function apply(ImageInterface $image): ImageInterface throw new RuntimeException('Image is not animated.'); } + $image = $this->failIfNotClass($image, Image::class); + $frames = new Collection(); foreach ($image as $key => $frame) { if ($this->position == $key) { @@ -28,6 +33,6 @@ public function apply(ImageInterface $image): ImageInterface } } - return new Image($frames); + return $image->setFrames($frames); } } diff --git a/src/Drivers/Imagick/Image.php b/src/Drivers/Imagick/Image.php index c36d25ee6..4ae52f579 100644 --- a/src/Drivers/Imagick/Image.php +++ b/src/Drivers/Imagick/Image.php @@ -39,6 +39,13 @@ public function getImagick(): Imagick return $this->imagick; } + public function setImagick(Imagick $imagick): ImageInterface + { + $this->imagick = $imagick; + + return $this; + } + public function frame(int $position = 0): FrameInterface { foreach ($this->imagick as $core) { diff --git a/src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php b/src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php index 1a611a5e6..f939ec2ad 100644 --- a/src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php +++ b/src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php @@ -3,13 +3,16 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use Imagick; -use Intervention\Image\Drivers\Imagick\Image; use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; +use Intervention\Image\Drivers\Imagick\Image; +use Intervention\Image\Traits\CanCheckType; class RemoveAnimationModifier implements ModifierInterface { + use CanCheckType; + public function __construct(protected int $position = 0) { // @@ -21,15 +24,16 @@ public function apply(ImageInterface $image): ImageInterface throw new RuntimeException('Image is not animated.'); } + $image = $this->failIfNotClass($image, Image::class); + + // create new imagick with just one image $imagick = new Imagick(); - foreach ($image as $frame) { - if ($frame->core()->getIteratorIndex() == $this->position) { - $imagick->addImage($frame->core()->getImage()); + foreach ($image->getImagick() as $key => $core) { + if ($key == $this->position) { + $imagick->addImage($core->getImage()); } } - $image->destroy(); - - return new Image($imagick); + return $image->setImagick($imagick); } } diff --git a/tests/Drivers/Gd/Modifiers/RemoveAnimationModifierTest.php b/tests/Drivers/Gd/Modifiers/RemoveAnimationModifierTest.php index a48473ab7..ea6a1fe4b 100644 --- a/tests/Drivers/Gd/Modifiers/RemoveAnimationModifierTest.php +++ b/tests/Drivers/Gd/Modifiers/RemoveAnimationModifierTest.php @@ -18,7 +18,8 @@ public function testApply(): void { $image = $this->createTestImage('animation.gif'); $this->assertEquals(8, count($image)); - $image = $image->modify(new RemoveAnimationModifier(2)); + $result = $image->modify(new RemoveAnimationModifier(2)); $this->assertEquals(1, count($image)); + $this->assertEquals(1, count($result)); } } diff --git a/tests/Drivers/Imagick/Modifiers/RemoveAnimationModifierTest.php b/tests/Drivers/Imagick/Modifiers/RemoveAnimationModifierTest.php index 8f0591a4b..7a3eaf144 100644 --- a/tests/Drivers/Imagick/Modifiers/RemoveAnimationModifierTest.php +++ b/tests/Drivers/Imagick/Modifiers/RemoveAnimationModifierTest.php @@ -18,7 +18,8 @@ public function testApply(): void { $image = $this->createTestImage('animation.gif'); $this->assertEquals(8, count($image)); - $image = $image->modify(new RemoveAnimationModifier(2)); + $result = $image->modify(new RemoveAnimationModifier(2)); $this->assertEquals(1, count($image)); + $this->assertEquals(1, count($result)); } }