From: Johan Cwiklinski Date: Thu, 8 Jun 2023 20:17:21 +0000 (+0200) Subject: Add WebP images support; closes #1681 X-Git-Tag: 1.0.0rc1~32 X-Git-Url: https://git.agnieray.net/?a=commitdiff_plain;h=0274352255b12799e119540fa465b51a316894d7;p=galette.git Add WebP images support; closes #1681 --- diff --git a/galette/lib/Galette/Core/Picture.php b/galette/lib/Galette/Core/Picture.php index 17987d4c5..cf5339db0 100644 --- a/galette/lib/Galette/Core/Picture.php +++ b/galette/lib/Galette/Core/Picture.php @@ -96,11 +96,12 @@ class Picture implements FileInterface $this->init( null, - array('jpeg', 'jpg', 'png', 'gif'), + array('jpeg', 'jpg', 'png', 'gif', 'webp'), array( 'jpg' => 'image/jpeg', 'png' => 'image/png', - 'gif' => 'image/gif' + 'gif' => 'image/gif', + 'webp' => 'image/webp' ) ); @@ -183,6 +184,11 @@ class Picture implements FileInterface $this->format = 'gif'; $this->mime = 'image/gif'; return true; + } elseif (file_exists($file_wo_ext . '.webp')) { + $this->file_path = realpath($file_wo_ext . '.webp'); + $this->format = 'webp'; + $this->mime = 'image/webp'; + return true; } return false; } @@ -221,6 +227,9 @@ class Picture implements FileInterface case 'gif': $this->mime = 'image/gif'; break; + case 'webp': + $this->mime = 'image/webp'; + break; } $this->file_path = realpath($file_wo_ext . '.' . $this->format); return true; @@ -376,6 +385,10 @@ class Picture implements FileInterface //return unlink($file_wo_ext . '.gif'); $_file = $file_wo_ext . '.gif'; $success = unlink($_file); + } elseif (file_exists($file_wo_ext . '.webp')) { + //return unlink($file_wo_ext . '.webp'); + $_file = $file_wo_ext . '.webp'; + $success = unlink($_file); } if ($_file !== null && $success !== true) { @@ -717,6 +730,17 @@ class Picture implements FileInterface return false; } break; + case 'webp': + if (!$gdinfo['WebP Support']) { + Analog::log( + '[' . $class . '] GD has no WebP Support - ' . + 'pictures could not be resized!', + Analog::ERROR + ); + return false; + } + break; + default: return false; } @@ -756,6 +780,11 @@ class Picture implements FileInterface imagecopyresampled($thumb, $image, 0, 0, 0, 0, $w, $h, $cur_width, $cur_height); imagegif($thumb, $dest); break; + case 'webp': + $image = imagecreatefromwebp($source); + imagecopyresampled($thumb, $image, 0, 0, 0, 0, $w, $h, $cur_width, $cur_height); + imagewebp($thumb, $dest); + break; } } else { Analog::log( diff --git a/tests/Galette/Core/tests/units/Picture.php b/tests/Galette/Core/tests/units/Picture.php index 4354f6b78..5cb5c1a7a 100644 --- a/tests/Galette/Core/tests/units/Picture.php +++ b/tests/Galette/Core/tests/units/Picture.php @@ -104,13 +104,14 @@ class Picture extends TestCase $this->assertNull($picture->getDestDir()); $this->assertNull($picture->getFileName()); - $expected_exts = ['jpeg', 'jpg', 'png', 'gif']; + $expected_exts = ['jpeg', 'jpg', 'png', 'gif', 'webp']; $this->assertSame(implode(', ', $expected_exts), $picture->getAllowedExts()); $expected_mimes = [ 'jpg' => 'image/jpeg', 'png' => 'image/png', - 'gif' => 'image/gif' + 'gif' => 'image/gif', + 'webp' => 'image/webp' ]; $this->assertSame($expected_mimes, $picture->getAllowedMimeTypes()); @@ -232,7 +233,7 @@ class Picture extends TestCase $this->picture->getErrorMessage(\Galette\Core\Picture::INVALID_FILENAME) ); $this->assertSame( - 'File extension is not allowed, only jpeg, jpg, png, gif files are.', + 'File extension is not allowed, only jpeg, jpg, png, gif, webp files are.', $this->picture->getErrorMessage(\Galette\Core\Picture::INVALID_EXTENSION) ); $this->assertSame(