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

AVIF support #186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
7 changes: 7 additions & 0 deletions Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -84,6 +84,13 @@ public function savePng($file);
*/
public function saveWebp($file, $quality);

/**
* Save the image as a AVIF.
*
* @return $this
*/
public function saveAvif($file, $quality);

/**
* Save the image as a jpeg.
*
6 changes: 6 additions & 0 deletions Adapter/Common.php
Original file line number Diff line number Diff line change
@@ -193,6 +193,8 @@ abstract protected function openPng($file);

abstract protected function openWebp($file);

abstract protected function openAvif($file);

/**
* Creates an image.
*/
@@ -233,6 +235,10 @@ protected function loadFile($file, $type)
$this->openWebp($file);
}

if ($type == 'avif') {
$this->openAvif($file);
}

if (false === $this->resource) {
throw new \UnexpectedValueException('Unable to open file ('.$file.')');
} else {
32 changes: 31 additions & 1 deletion Adapter/GD.php
Original file line number Diff line number Diff line change
@@ -5,13 +5,20 @@
use Gregwar\Image\Image;
use Gregwar\Image\ImageColor;

// Add IMG_AVIF constant for PHP versions below 8.1
defined('IMG_AVIF') or define('IMG_AVIF', 256);

class GD extends Common
{
/**
* GD image types supported by this adapter.
*/
public static $gdTypes = array(
'jpeg' => \IMG_JPG,
'gif' => \IMG_GIF,
'png' => \IMG_PNG,
'webp' => \IMG_WEBP
'webp' => \IMG_WEBP,
'avif' => \IMG_AVIF
);

protected function loadResource($resource)
@@ -594,6 +601,16 @@ public function saveWebp($file, $quality)
return $this;
}

/**
* {@inheritdoc}
*/
public function saveAvif($file, $quality)
{
imageavif($this->resource, $file, $quality);

return $this;
}

/**
* {@inheritdoc}
*/
@@ -652,8 +669,21 @@ protected function openWebp($file)
}
}

/**
* Try to open the file using AVIF.
*/
protected function openAvif($file)
{
if (file_exists($file) && filesize($file)) {
$this->resource = @imagecreatefromavif($file);
} else {
$this->resource = false;
}
}

/**
* Does this adapter supports type ?
* Returns true if checked type is supported by the PHP/GD build and this adapter.
*/
protected function supports($type)
{
15 changes: 15 additions & 0 deletions Image.php
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
* @method Image saveGif($file)
* @method Image savePng($file)
* @method Image saveJpeg($file, $quality)
* @method Image saveWebP($file, $quality)
* @method Image saveAvif($file, $quality)
* @method Image resize($width = null, $height = null, $background = 'transparent', $force = false, $rescale = false, $crop = false)
* @method Image forceResize($width = null, $height = null, $background = 'transparent')
* @method Image scaleResize($width = null, $height = null, $background = 'transparent', $crop = false)
@@ -94,6 +96,7 @@ class Image
'jpg' => 'jpeg',
'jpeg' => 'jpeg',
'webp' => 'webp',
'avif' => 'avif',
'png' => 'png',
'gif' => 'gif',
);
@@ -580,6 +583,14 @@ public function webp($quality = 80)
return $this->cacheFile('webp', $quality);
}

/**
* Generates and output a webp cached file.
*/
public function avif($quality = 80)
{
return $this->cacheFile('avif', $quality);
}

/**
* Generates and output an image using the same type as input.
*/
@@ -687,6 +698,10 @@ public function save($file, $type = 'guess', $quality = 80)
$success = $this->getAdapter()->saveWebP($file, $quality);
}

if ($type == 'avif') {
$success = $this->getAdapter()->saveAvif($file, $quality);
}

if (!$success) {
return false;
}
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -148,11 +148,15 @@ array. This operation array, the name, type and modification time of file are ha

Once the cache directory configured, you can call the following methods:

* `jpeg($quality = 80)`: lookup or create a jpeg cache file on-the-fly
* `jpeg($quality = 80)`: lookup or create a JPEG cache file on-the-fly

* `gif()`: lookup or create a gif cache file on-the-fly
* `webp($quality = 80)`: lookup or create a WebP cache file on-the-fly

* `png()`: lookup or create a png cache file on-the-fly
* `avif($quality = 80)`: lookup or create a AVIF cache file on-the-fly (**PHP 8.1 or greater required**)

* `gif()`: lookup or create a GIF cache file on-the-fly

* `png()`: lookup or create a PNG cache file on-the-fly

* `guess($quality = 80)`: guesses the type (use the same as input) and lookup or create a
cache file on-the-fly
4 changes: 4 additions & 0 deletions Source/File.php
Original file line number Diff line number Diff line change
@@ -47,6 +47,10 @@ public function guessType()
if ($type == IMAGETYPE_WEBP) {
return 'webp';
}

if (defined('IMAGETYPE_AVIF') && $type == IMAGETYPE_AVIF) {
return 'avif';
}
}
}