Support for WebP images (#3861)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / image / adapter / GDImageAdapter.class.php
index ec600d89c50beb8f7f288cc69e335bef94b051b1..9d61e08f67ac7047db35861509ea6eb8331742f5 100644 (file)
@@ -111,9 +111,16 @@ class GDImageAdapter implements IImageAdapter {
                                }
                        break;
                        
+                       case IMAGETYPE_WEBP:
+                               // suppress warnings and properly handle errors
+                               $this->image = @imagecreatefromwebp($file);
+                               if ($this->image === false) {
+                                       throw new SystemException("Could not read webp image '".$file."'.");
+                               }
+                       break;
+                       
                        default:
                                throw new SystemException("Could not read image '".$file."', format is not recognized.");
-                       break;
                }
        }
        
@@ -133,12 +140,12 @@ class GDImageAdapter implements IImageAdapter {
        /**
         * @inheritDoc
         */
-       public function createThumbnail($maxWidth, $maxHeight, $obtainDimensions = true) {
+       public function createThumbnail($maxWidth, $maxHeight, $preserveAspectRatio = true) {
                $x = $y = 0;
                $sourceWidth = $this->width;
                $sourceHeight = $this->height;
                
-               if ($obtainDimensions) {
+               if ($preserveAspectRatio) {
                        if ($maxWidth / $this->width < $maxHeight / $this->height) {
                                $width = $maxWidth;
                                $height = round($this->height * ($width / $this->width));
@@ -348,6 +355,9 @@ class GDImageAdapter implements IImageAdapter {
                else if ($this->type == IMAGETYPE_PNG) {
                        imagepng($image);
                }
+               else if ($this->type == IMAGETYPE_WEBP) {
+                       imagewebp($image);
+               }
                else if (function_exists('imageJPEG')) {
                        imagejpeg($image, null, 90);
                }
@@ -429,7 +439,7 @@ class GDImageAdapter implements IImageAdapter {
         * @param       integer         $pct            opacity percent
         * @return      boolean
         */
-       private function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
+       private function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) { // phpcs:ignore
                if (!isset($pct)) {
                        return false;
                }