mirror of
https://github.com/Intervention/image.git
synced 2025-08-27 07:44:30 +02:00
Refactor RemoveAnimationModifier::class, Add tests
This commit is contained in:
@@ -18,7 +18,7 @@ class RemoveAnimationModifier extends GenericRemoveAnimationModifier implements
|
|||||||
public function apply(ImageInterface $image): ImageInterface
|
public function apply(ImageInterface $image): ImageInterface
|
||||||
{
|
{
|
||||||
$image->core()->setNative(
|
$image->core()->setNative(
|
||||||
$this->chosenFrame($image, $this->position)->native()
|
$this->selectedFrame($image)->native()
|
||||||
);
|
);
|
||||||
|
|
||||||
return $image;
|
return $image;
|
||||||
|
@@ -15,7 +15,7 @@ class RemoveAnimationModifier extends GenericRemoveAnimationModifier implements
|
|||||||
{
|
{
|
||||||
// create new imagick with just one image
|
// create new imagick with just one image
|
||||||
$imagick = new Imagick();
|
$imagick = new Imagick();
|
||||||
$frame = $this->chosenFrame($image, $this->position);
|
$frame = $this->selectedFrame($image);
|
||||||
$imagick->addImage($frame->native()->getImage());
|
$imagick->addImage($frame->native()->getImage());
|
||||||
|
|
||||||
// set new imagick to image
|
// set new imagick to image
|
||||||
|
@@ -19,13 +19,30 @@ class RemoveAnimationModifier extends SpecializableModifier
|
|||||||
/**
|
/**
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
public function chosenFrame(ImageInterface $image, int|string $position): FrameInterface
|
protected function selectedFrame(ImageInterface $image): FrameInterface
|
||||||
{
|
{
|
||||||
if (is_int($position)) {
|
return $image->core()->frame($this->normalizePosition($image));
|
||||||
return $image->core()->frame($position);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the position of the selected frame as integer
|
||||||
|
*
|
||||||
|
* @param ImageInterface $image
|
||||||
|
* @throws InputException
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function normalizePosition(ImageInterface $image): int
|
||||||
|
{
|
||||||
|
if (is_int($this->position)) {
|
||||||
|
return $this->position;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match("/^(?P<percent>[0-9]{1,3})%$/", $position, $matches) != 1) {
|
if (is_numeric($this->position)) {
|
||||||
|
return (int) $this->position;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate position from percentage value
|
||||||
|
if (preg_match("/^(?P<percent>[0-9]{1,3})%$/", $this->position, $matches) != 1) {
|
||||||
throw new InputException(
|
throw new InputException(
|
||||||
'Position must be either integer or a percent value as string.'
|
'Position must be either integer or a percent value as string.'
|
||||||
);
|
);
|
||||||
@@ -35,6 +52,6 @@ class RemoveAnimationModifier extends SpecializableModifier
|
|||||||
$position = intval(round($total / 100 * intval($matches['percent'])));
|
$position = intval(round($total / 100 * intval($matches['percent'])));
|
||||||
$position = $position == $total ? $position - 1 : $position;
|
$position = $position == $total ? $position - 1 : $position;
|
||||||
|
|
||||||
return $image->core()->frame($position);
|
return $position;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
tests/Unit/Modifiers/RemoveAnimationModifierTest.php
Normal file
44
tests/Unit/Modifiers/RemoveAnimationModifierTest.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Unit\Modifiers;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
use Intervention\Image\Modifiers\RemoveAnimationModifier;
|
||||||
|
use Intervention\Image\Tests\BaseTestCase;
|
||||||
|
use Mockery;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
|
#[CoversClass(RemoveAnimationModifier::class)]
|
||||||
|
final class RemoveAnimationModifierTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
#[DataProvider('normalizePositionProvider')]
|
||||||
|
public function testNormalizePosition(int|string $position, int $frames, int $normalized): void
|
||||||
|
{
|
||||||
|
$modifier = new class ($position) extends RemoveAnimationModifier
|
||||||
|
{
|
||||||
|
public function testResult(int $frames): int
|
||||||
|
{
|
||||||
|
$image = Mockery::mock(ImageInterface::class)->makePartial();
|
||||||
|
$image->shouldReceive('count')->andReturn($frames);
|
||||||
|
|
||||||
|
return $this->normalizePosition($image);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->assertEquals($normalized, $modifier->testResult($frames));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function normalizePositionProvider(): Generator
|
||||||
|
{
|
||||||
|
yield [0, 100, 0];
|
||||||
|
yield [10, 100, 10];
|
||||||
|
yield ['10', 100, 10];
|
||||||
|
yield ['0%', 100, 0];
|
||||||
|
yield ['50%', 100, 50];
|
||||||
|
yield ['100%', 100, 99];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user