Skip to content

Commit

Permalink
Merge pull request #333 from gijsdev/avif
Browse files Browse the repository at this point in the history
Add support for AVIF
  • Loading branch information
ADmad authored Oct 26, 2021
2 parents 244d3f6 + a302e93 commit 8f62b10
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"require": {
"php": "^7.2|^8.0",
"intervention/image": "^2.4",
"intervention/image": "^2.7",
"league/flysystem": "^2.0",
"psr/http-message": "^1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/2.0/api/encode.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Defines the quality of the image. Use values between `0` and `100`. Defaults to

## Format `fm`

Encodes the image to a specific format. Accepts `jpg`, `pjpg` (progressive jpeg), `png`, `gif` or `webp`. Defaults to `jpg`.
Encodes the image to a specific format. Accepts `jpg`, `pjpg` (progressive jpeg), `png`, `gif`, `webp` or `avif`. Defaults to `jpg`.

~~~ html
<img src="kayaks.jpg?w=500&fm=gif">
Expand Down
1 change: 1 addition & 0 deletions src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function run(Image $image)
public function getFormat(Image $image)
{
$allowed = [
'avif' => 'image/avif',
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'pjpg' => 'image/jpeg',
Expand Down
27 changes: 27 additions & 0 deletions tests/Manipulators/EncodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class EncodeTest extends TestCase
private $gif;
private $tif;
private $webp;
private $avif;

public function setUp(): void
{
Expand All @@ -26,6 +27,10 @@ public function setUp(): void
$this->webp = $manager->canvas(100, 100)->encode('webp');
}

if (function_exists('imagecreatefromavif')) {
$this->avif = $manager->canvas(100, 100)->encode('avif');
}

$this->manipulator = new Encode();
}

Expand Down Expand Up @@ -64,6 +69,21 @@ public function testRun()
$this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->gif)->mime);
$this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->webp)->mime);
}
if (function_exists('imagecreatefromavif')) {
$this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'jpg'])->run($this->avif)->mime);
$this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'pjpg'])->run($this->avif)->mime);
$this->assertSame('image/png', $this->manipulator->setParams(['fm' => 'png'])->run($this->avif)->mime);
$this->assertSame('image/gif', $this->manipulator->setParams(['fm' => 'gif'])->run($this->avif)->mime);
$this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->jpg)->mime);
$this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->png)->mime);
$this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->gif)->mime);
$this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->avif)->mime);
}

if (function_exists('imagecreatefromwebp') && function_exists('imagecreatefromavif')) {
$this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->avif)->mime);
$this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->webp)->mime);
}
}

public function testGetFormat()
Expand All @@ -78,6 +98,9 @@ public function testGetFormat()
if (function_exists('imagecreatefromwebp')) {
$mock->shouldReceive('mime')->andReturn('image/webp')->once();
}
if (function_exists('imagecreatefromavif')) {
$mock->shouldReceive('mime')->andReturn('image/avif')->once();
}
});

$this->assertSame('jpg', $this->manipulator->setParams(['fm' => 'jpg'])->getFormat($image));
Expand All @@ -93,6 +116,10 @@ public function testGetFormat()
if (function_exists('imagecreatefromwebp')) {
$this->assertSame('webp', $this->manipulator->setParams(['fm' => null])->getFormat($image));
}

if (function_exists('imagecreatefromavif')) {
$this->assertSame('avif', $this->manipulator->setParams(['fm' => null])->getFormat($image));
}
}

public function testGetQuality()
Expand Down

0 comments on commit 8f62b10

Please sign in to comment.