Skip to content

Commit

Permalink
Merge commit 'pullrequests/TAR5/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Oct 25, 2024
2 parents 73f2d57 + 070fd0f commit 111b9ba
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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.
*
Expand Down
6 changes: 6 additions & 0 deletions Adapter/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ abstract protected function openPng($file);
*/
abstract protected function openWebp($file);

abstract protected function openAvif($file);

/**
* Creates an image.
*
Expand Down Expand Up @@ -288,6 +290,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.')');
}
Expand Down
33 changes: 31 additions & 2 deletions Adapter/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
use Gregwar\Image\ImageColor;
use Gregwar\Image\Utils\FileUtils;

// 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
);

/**
Expand Down Expand Up @@ -632,6 +639,16 @@ public function saveWebp($file, $quality)
return $this;
}

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

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -706,9 +723,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.
* @param string $type
*
* @return bool
Expand Down
15 changes: 15 additions & 0 deletions Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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)
Expand Down Expand Up @@ -111,6 +113,7 @@ class Image
'jpg' => 'jpeg',
'jpeg' => 'jpeg',
'webp' => 'webp',
'avif' => 'avif',
'png' => 'png',
'gif' => 'gif',
);
Expand Down Expand Up @@ -702,6 +705,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.
*
Expand Down Expand Up @@ -823,6 +834,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;
}
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Source/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function guessType()
if ($type === IMAGETYPE_WEBP) {
return 'webp';
}

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

Expand Down

0 comments on commit 111b9ba

Please sign in to comment.