1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-26 15:24:37 +02:00

Refactor RemoveAnimationModifier::class, Add tests

This commit is contained in:
Oliver Vogel
2024-12-28 10:52:08 +01:00
parent ca067a8fbb
commit 5a1b6f55f8
4 changed files with 68 additions and 7 deletions

View File

@@ -18,7 +18,7 @@ class RemoveAnimationModifier extends GenericRemoveAnimationModifier implements
public function apply(ImageInterface $image): ImageInterface
{
$image->core()->setNative(
$this->chosenFrame($image, $this->position)->native()
$this->selectedFrame($image)->native()
);
return $image;

View File

@@ -15,7 +15,7 @@ class RemoveAnimationModifier extends GenericRemoveAnimationModifier implements
{
// create new imagick with just one image
$imagick = new Imagick();
$frame = $this->chosenFrame($image, $this->position);
$frame = $this->selectedFrame($image);
$imagick->addImage($frame->native()->getImage());
// set new imagick to image

View File

@@ -19,13 +19,30 @@ class RemoveAnimationModifier extends SpecializableModifier
/**
* @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($position);
return $image->core()->frame($this->normalizePosition($image));
}
/**
* 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(
'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 = $position == $total ? $position - 1 : $position;
return $image->core()->frame($position);
return $position;
}
}

View 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];
}
}