Skip to content

Commit

Permalink
Update WebP class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ve3 committed Mar 27, 2024
1 parent e12764d commit 5b95870
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
81 changes: 79 additions & 2 deletions Rundiz/Image/Extensions/WebP.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ class WebP
{


/**
* @since 3.1.4
* @var string|null File path.
*/
protected $file;


/**
* WebP file information class.
*
* @since 3.1.4
* @param string $file Path to WEBP file.
*/
public function __construct($file = '')
{
if (is_string($file) && !empty($file)) {
$this->file = $file;
} else {
$this->file = null;
}
}// __construct


/**
* Check that WEBP constant (`IMAGETYPE_WEBP`) is already define, if not then define it.
*
Expand All @@ -29,17 +52,71 @@ public function checkWebPConstant()
}// checkWebPConstant


/**
* Check if GD supported current WEBP file.
*
* @return boolean Return `true` if yes, `false` if no.
*/
public function isGDSupported()
{
$info = $this->webPInfo();
if (!is_array($info)) {
// If not WEBP.
unset($info);
return false;
}

if (is_array($info) && array_key_exists('ANIMATION', $info) && true === $info['ANIMATION']) {
// If animated WEBP.
// Currently there is no supported for animated WEBP (last checked PHP 8.3).
// @link https://www.php.net/manual/en/function.imagecreatefromwebp.php Reference for can't read animated WEBP.
unset($info);
return false;
}

if (function_exists('imagecreatefromwebp')) {
// If there is function supported.
if (version_compare(PHP_VERSION, '7.0', '>=')) {
// If PHP 7.0 or newer.
// Yes!
unset($info);
return true;
} else {
// If PHP is older than 7.0.
// Notice:
// PHP <= 5.6 does not supported transparency WEBP and cause error "WebP decode: fail to decode input data".
// PHP < 5.6 does not fully supported non-transparency WEBP because image will becomes green or incorrect color.
$isTransparent = (is_array($info) && array_key_exists('ALPHA', $info) && true === $info['ALPHA']);
if (!$isTransparent) {
// If image does not contain transparency.
// Yes!
unset($info, $isTransparent);
return true;
}
unset($isTransparent);
}
}// endif; function_exists().

unset($info);
return false;
}// isGDSupported


/**
* Get WebP file info.
*
* @link https://www.php.net/manual/en/function.pack.php unpack format reference.
* @link https://developers.google.com/speed/webp/docs/riff_container WebP document.
* @link https://stackoverflow.com/q/61221874/128761 Original question.
* @param string $file Full path to image file.
* @param string $file Full path to image file. You can omit this argument and use one in class constructor instead.
* @return array|false Return associative array if success, return `false` for otherwise.
*/
public function webPInfo($file)
public function webPInfo($file = '')
{
if (!is_string($file) || (is_string($file) && empty($file))) {
$file = $this->file;
}

if (!is_file($file)) {
// if file was not found.
return false;
Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/ExtensionsTests/WebPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@ class WebPTest extends \Rundiz\Image\Tests\RDICommonTestCase
{


public function testIsGDSupported()
{
// non-transparent webp must be supported in all PHP version (>= 5.4) but in fact, it is not fully supported prior PHP 5.6.
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'city-amsterdam-non-transparent.webp');
$this->assertTrue($Webp->isGDSupported());
unset($Webp);

// not webp.
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'city-amsterdam.jpg');
$this->assertFalse($Webp->isGDSupported());
unset($Webp);

// animated.
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'city-amsterdam-animated.webp');
$this->assertFalse($Webp->isGDSupported());
unset($Webp);

// transparent.
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'city-amsterdam.webp');
$this->assertTrue($Webp->isGDSupported());
unset($Webp);
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'transparent-lossless.webp');
$this->assertTrue($Webp->isGDSupported());
unset($Webp);
} else {
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'city-amsterdam.webp');
$this->assertFalse($Webp->isGDSupported());
unset($Webp);
$Webp = new \Rundiz\Image\Extensions\WebP(static::$source_images_dir . 'transparent-lossless.webp');
$this->assertFalse($Webp->isGDSupported());
unset($Webp);
}// endif; php version compare.
}// testIsGDSupported


/**
* @depends Rundiz\Image\Tests\DependentTests\DirsFilesExistsTest::testImageExists
*/
Expand Down

0 comments on commit 5b95870

Please sign in to comment.