]> git.agnieray.net Git - galette.git/commitdiff
Add WebP images support; closes #1681
authorJohan Cwiklinski <johan@x-tnd.be>
Thu, 8 Jun 2023 20:17:21 +0000 (22:17 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Thu, 8 Jun 2023 20:26:47 +0000 (22:26 +0200)
galette/lib/Galette/Core/Picture.php
tests/Galette/Core/tests/units/Picture.php

index 17987d4c5f19b9b5c14150c300df7ebcd09e2c68..cf5339db0afd705f5ef7a34a57cad507b526ced1 100644 (file)
@@ -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(
index 4354f6b7866e0de967c0f72fbe5241d56e0e116e..5cb5c1a7a408c62705aa50ceed21e2f60a6de33b 100644 (file)
@@ -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(