Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AVIF #333

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 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,10 @@ public function testGetFormat()
if (function_exists('imagecreatefromwebp')) {
$mock->shouldReceive('mime')->andReturn('image/webp')->once();
}

ADmad marked this conversation as resolved.
Show resolved Hide resolved
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 +117,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