Skip to content

Commit

Permalink
Refactor RemoveAnimationModifier::class, Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 28, 2024
1 parent ca067a8 commit 5a1b6f5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Modifiers/RemoveAnimationModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Modifiers/RemoveAnimationModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function apply(ImageInterface $image): ImageInterface
{
// 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
Expand Down
27 changes: 22 additions & 5 deletions src/Modifiers/RemoveAnimationModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,30 @@ public function __construct(public int|string $position = 0)
/**
* @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 (is_numeric($this->position)) {
return (int) $this->position;
}

if (preg_match("/^(?P<percent>[0-9]{1,3})%$/", $position, $matches) != 1) {
// 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.'
);
Expand All @@ -35,6 +52,6 @@ public function chosenFrame(ImageInterface $image, int|string $position): FrameI
$position = intval(round($total / 100 * intval($matches['percent'])));
$position = $position == $total ? $position - 1 : $position;

return $image->core()->frame($position);
return $position;
}
}
44 changes: 44 additions & 0 deletions tests/Unit/Modifiers/RemoveAnimationModifierTest.php
Original file line number Diff line number Diff line change
@@ -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];
}
}

0 comments on commit 5a1b6f5

Please sign in to comment.