1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-12 00:43:59 +02:00

Fix bug in RemoveAnimationModifiers

This commit is contained in:
Oliver Vogel
2023-10-31 14:47:44 +01:00
parent 2c39ebb1e8
commit e5f141fb65
6 changed files with 40 additions and 10 deletions

View File

@@ -27,6 +27,18 @@ class Image extends AbstractImage implements ImageInterface, IteratorAggregate
//
}
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;

View File

@@ -7,9 +7,12 @@ use Intervention\Image\Drivers\Gd\Image;
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 @@ class RemoveAnimationModifier implements ModifierInterface
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 @@ class RemoveAnimationModifier implements ModifierInterface
}
}
return new Image($frames);
return $image->setFrames($frames);
}
}

View File

@@ -39,6 +39,13 @@ class Image extends AbstractImage implements ImageInterface, Iterator
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) {

View File

@@ -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 @@ class RemoveAnimationModifier implements ModifierInterface
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);
}
}

View File

@@ -18,7 +18,8 @@ class RemoveAnimationModifierTest extends TestCase
{
$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));
}
}

View File

@@ -18,7 +18,8 @@ class RemoveAnimationModifierTest extends TestCase
{
$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));
}
}