1
0
mirror of https://github.com/Intervention/image.git synced 2025-03-15 22:49:40 +01:00

Change behaviour of Image::getFrame()

Method now throws exception if the given frame could not be found.
This commit is contained in:
Oliver Vogel 2023-10-26 15:46:45 +02:00
parent 2d565be6cb
commit e81a9969e3
6 changed files with 27 additions and 10 deletions

View File

@ -6,6 +6,7 @@ use Intervention\Image\Collection;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Drivers\Abstract\AbstractImage;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Exceptions\AnimationException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
@ -51,9 +52,13 @@ class Image extends AbstractImage implements ImageInterface, IteratorAggregate
return $this;
}
public function getFrame(int $position = 0): ?FrameInterface
public function getFrame(int $position = 0): FrameInterface
{
return $this->frames->get($position);
if ($frame = $this->frames->get($position)) {
return $frame;
}
throw new AnimationException('Frame #' . $position . ' is not be found in the image.');
}
public function addFrame(FrameInterface $frame): ImageInterface

View File

@ -12,6 +12,7 @@ use Intervention\Image\Drivers\Imagick\Modifiers\ColorspaceModifier;
use Intervention\Image\Drivers\Imagick\Modifiers\ProfileModifier;
use Intervention\Image\Drivers\Imagick\Modifiers\ProfileRemovalModifier;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Exceptions\AnimationException;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
@ -36,7 +37,7 @@ class Image extends AbstractImage implements ImageInterface, Iterator
return $this->imagick;
}
public function getFrame(int $position = 0): ?FrameInterface
public function getFrame(int $position = 0): FrameInterface
{
foreach ($this->imagick as $core) {
if ($core->getIteratorIndex() == $position) {
@ -44,7 +45,7 @@ class Image extends AbstractImage implements ImageInterface, Iterator
}
}
return null;
throw new AnimationException('Frame #' . $position . ' is not be found in the image.');
}
public function addFrame(FrameInterface $frame): ImageInterface

View File

@ -0,0 +1,8 @@
<?php
namespace Intervention\Image\Exceptions;
class AnimationException extends \RuntimeException
{
//
}

View File

@ -12,9 +12,9 @@ interface ImageInterface extends Traversable, Countable
* Get frame of animation image at given position starting with zero
*
* @param int $position
* @return null|FrameInterface
* @return FrameInterface
*/
public function getFrame(int $position = 0): ?FrameInterface;
public function getFrame(int $position = 0): FrameInterface;
/**
* Add frame to animated image

View File

@ -11,6 +11,7 @@ use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\AnimationException;
/**
* @requires extension gd
@ -109,8 +110,8 @@ class ImageTest extends TestCase
$this->assertInstanceOf(Color::class, $color);
$this->assertEquals([0, 0, 255, 255], $color->toArray());
$color = $this->image->pickColor(0, 0, 3);
$this->assertNull($color);
$this->expectException(AnimationException::class);
$this->image->pickColor(0, 0, 3);
}
public function testPickColors(): void

View File

@ -9,6 +9,7 @@ use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
use Intervention\Image\Colors\Profile;
use Intervention\Image\Drivers\Imagick\Frame;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Exceptions\AnimationException;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Tests\TestCase;
@ -44,12 +45,13 @@ class ImageTest extends TestCase
{
$this->assertInstanceOf(Image::class, $this->image);
}
public function testGetFrame(): void
{
$this->assertInstanceOf(Frame::class, $this->image->getFrame());
$this->assertInstanceOf(Frame::class, $this->image->getFrame(1));
$this->assertNull($this->image->getFrame(2));
$this->expectException(AnimationException::class);
$this->image->getFrame(2);
}
public function testAddFrame(): void