Skip to content

Commit

Permalink
Webp format should be animated (#812)
Browse files Browse the repository at this point in the history
* Allow webp as animated format

* tests that show webp format cannot be used as animated

* Allow webp as animated format

* Fix style and tests
  • Loading branch information
Yoann-TYT authored Nov 18, 2024
1 parent 93bdd9a commit 0b78aa6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Gmagick/Layers.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public function coalesce()
public function animate($format, $delay, $loops)
{
$formatInfo = Format::get($format);
if ($formatInfo === null || $formatInfo->getID() !== Format::ID_GIF) {
throw new InvalidArgumentException('Animated picture is currently only supported on gif');
if ($formatInfo === null || !in_array($formatInfo->getID(), array(Format::ID_GIF, Format::ID_WEBP))) {
throw new InvalidArgumentException('Animated picture is currently only supported on gif and webp');
}

if (!is_int($loops) || $loops < 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/Imagick/Layers.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function merge()
public function animate($format, $delay, $loops)
{
$formatInfo = Format::get($format);
if ($formatInfo === null || $formatInfo->getID() !== Format::ID_GIF) {
throw new InvalidArgumentException('Animated picture is currently only supported on gif');
if ($formatInfo === null || !in_array($formatInfo->getID(), array(Format::ID_GIF, Format::ID_WEBP))) {
throw new InvalidArgumentException('Animated picture is currently only supported on gif and webp');
}

if (!is_int($loops) || $loops < 0) {
Expand All @@ -107,7 +107,7 @@ public function animate($format, $delay, $loops)
try {
foreach ($this as $offset => $layer) {
$this->resource->setIteratorIndex($offset);
$this->resource->setFormat(Format::ID_GIF);
$this->resource->setFormat($formatInfo->getID());

if ($delay !== null) {
$layer->getImagick()->setImageDelay($delay / 10);
Expand Down
14 changes: 14 additions & 0 deletions tests/tests/Gmagick/LayersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ public function testCount()
$this->assertCount(42, $layers);
}

public function testWebpFormatIsAllowedAsAnimatedFormat()
{
$palette = new RGB();
$resource = $this->getMockBuilder('\Gmagick')->getMock();

$resource->expects($this->atLeastOnce())
->method('getNumberImages')
->will($this->returnValue(42));

$layers = new Layers(new Image($resource, $palette, new MetadataBag()), $palette, $resource);

$layers->animate('webp', 200, 0);
}

public function testGetLayer()
{
$this->checkGmagickMockable();
Expand Down
14 changes: 14 additions & 0 deletions tests/tests/Imagick/LayersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public function testCount()
$this->assertCount(42, $layers);
}

public function testWebpFormatIsAllowedAsAnimatedFormat()
{
$palette = new RGB();
$resource = $this->getMockBuilder('\Imagick')->getMock();

$resource->expects($this->atLeastOnce())
->method('getNumberImages')
->will($this->returnValue(42));

$layers = new Layers(new Image($resource, $palette, new MetadataBag()), $palette, $resource);

$layers->animate('webp', 200, 0);
}

public function testGetLayer()
{
$palette = new RGB();
Expand Down

0 comments on commit 0b78aa6

Please sign in to comment.