Skip to content

Commit 0b78aa6

Browse files
authored
Webp format should be animated (#812)
* Allow webp as animated format * tests that show webp format cannot be used as animated * Allow webp as animated format * Fix style and tests
1 parent 93bdd9a commit 0b78aa6

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/Gmagick/Layers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public function coalesce()
108108
public function animate($format, $delay, $loops)
109109
{
110110
$formatInfo = Format::get($format);
111-
if ($formatInfo === null || $formatInfo->getID() !== Format::ID_GIF) {
112-
throw new InvalidArgumentException('Animated picture is currently only supported on gif');
111+
if ($formatInfo === null || !in_array($formatInfo->getID(), array(Format::ID_GIF, Format::ID_WEBP))) {
112+
throw new InvalidArgumentException('Animated picture is currently only supported on gif and webp');
113113
}
114114

115115
if (!is_int($loops) || $loops < 0) {

src/Imagick/Layers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public function merge()
9292
public function animate($format, $delay, $loops)
9393
{
9494
$formatInfo = Format::get($format);
95-
if ($formatInfo === null || $formatInfo->getID() !== Format::ID_GIF) {
96-
throw new InvalidArgumentException('Animated picture is currently only supported on gif');
95+
if ($formatInfo === null || !in_array($formatInfo->getID(), array(Format::ID_GIF, Format::ID_WEBP))) {
96+
throw new InvalidArgumentException('Animated picture is currently only supported on gif and webp');
9797
}
9898

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

112112
if ($delay !== null) {
113113
$layer->getImagick()->setImageDelay($delay / 10);

tests/tests/Gmagick/LayersTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public function testCount()
8484
$this->assertCount(42, $layers);
8585
}
8686

87+
public function testWebpFormatIsAllowedAsAnimatedFormat()
88+
{
89+
$palette = new RGB();
90+
$resource = $this->getMockBuilder('\Gmagick')->getMock();
91+
92+
$resource->expects($this->atLeastOnce())
93+
->method('getNumberImages')
94+
->will($this->returnValue(42));
95+
96+
$layers = new Layers(new Image($resource, $palette, new MetadataBag()), $palette, $resource);
97+
98+
$layers->animate('webp', 200, 0);
99+
}
100+
87101
public function testGetLayer()
88102
{
89103
$this->checkGmagickMockable();

tests/tests/Imagick/LayersTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ public function testCount()
8383
$this->assertCount(42, $layers);
8484
}
8585

86+
public function testWebpFormatIsAllowedAsAnimatedFormat()
87+
{
88+
$palette = new RGB();
89+
$resource = $this->getMockBuilder('\Imagick')->getMock();
90+
91+
$resource->expects($this->atLeastOnce())
92+
->method('getNumberImages')
93+
->will($this->returnValue(42));
94+
95+
$layers = new Layers(new Image($resource, $palette, new MetadataBag()), $palette, $resource);
96+
97+
$layers->animate('webp', 200, 0);
98+
}
99+
86100
public function testGetLayer()
87101
{
88102
$palette = new RGB();

0 commit comments

Comments
 (0)