Skip to content

Commit

Permalink
Fix bug in RemoveAnimationModifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 31, 2023
1 parent 2c39ebb commit e5f141f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/Drivers/Gd/Image.php
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php
Expand Up @@ -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)
{
//
Expand All @@ -21,13 +24,15 @@ 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) {
$frames->push($frame);
}
}

return new Image($frames);
return $image->setFrames($frames);
}
}
7 changes: 7 additions & 0 deletions src/Drivers/Imagick/Image.php
Expand Up @@ -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) {
Expand Down
18 changes: 11 additions & 7 deletions src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php
Expand Up @@ -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)
{
//
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion tests/Drivers/Gd/Modifiers/RemoveAnimationModifierTest.php
Expand Up @@ -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));
}
}
Expand Up @@ -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));
}
}

0 comments on commit e5f141f

Please sign in to comment.