Dynamically create WebP variants for style imports
authorAlexander Ebert <ebert@woltlab.com>
Fri, 29 Jan 2021 12:38:06 +0000 (13:38 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Jan 2021 12:46:59 +0000 (13:46 +0100)
wcfsetup/install/files/lib/data/style/Style.class.php
wcfsetup/install/files/lib/data/style/StyleEditor.class.php

index 15f58cc77e4d4daf2342b6a17e6d6f8c2d82d0a1..41c6a549e1904f790dfa9b47d7422b447139572e 100644 (file)
@@ -296,7 +296,7 @@ class Style extends DatabaseObject
     /**
      * Serve the WebP variant of the cover photo if the browser supports
      * it and the original cover photo is not a GIF.
-     * 
+     *
      * @since 5.4
      */
     protected function useWebP($forceWebP = null): bool
index 69785681807a4a345cd03e1931999eaa3006eb25..42a52d982d7f5627c3356fa094ea590927ccfbe4 100644 (file)
@@ -27,6 +27,7 @@ use wcf\system\style\StyleHandler;
 use wcf\system\WCF;
 use wcf\util\DateUtil;
 use wcf\util\FileUtil;
+use wcf\util\ImageUtil;
 use wcf\util\StringUtil;
 use wcf\util\XML;
 use wcf\util\XMLWriter;
@@ -173,6 +174,16 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
         return $variableNames;
     }
 
+    public function createCoverPhotoVariant(?string $sourceLocation = null): void
+    {
+        if ($sourceLocation === null) {
+            $sourceLocation = $this->getCoverPhotoLocation(false);
+        }
+
+        $outputFilenameWithoutExtension = \preg_replace('~\.[a-z]+$~', '', $sourceLocation);
+        ImageUtil::createWebpVariant($sourceLocation, $outputFilenameWithoutExtension);
+    }
+
     /**
      * Reads the data of a style exchange format file.
      *
@@ -694,18 +705,23 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
             $fileExtension = \pathinfo($data['coverPhoto'], \PATHINFO_EXTENSION);
             $index = $tar->getIndexByFilename($data['coverPhoto']);
             if ($index !== false && \in_array($fileExtension, self::VALID_IMAGE_EXTENSIONS)) {
-                $filename = $style->getAssetPath() . 'coverPhoto.' . $fileExtension;
-                $tar->extract($index, $filename);
-                FileUtil::makeWritable($filename);
+                $coverPhoto = "{$style->getAssetPath()}coverPhoto.{$fileExtension}";
+                $tar->extract($index, $coverPhoto);
+                FileUtil::makeWritable($coverPhoto);
 
-                if (\file_exists($filename)) {
+                if (\file_exists($coverPhoto)) {
                     try {
-                        if (($imageData = \getimagesize($filename)) !== false) {
+                        if (($imageData = \getimagesize($coverPhoto)) !== false) {
                             switch ($imageData[2]) {
                                 case \IMAGETYPE_PNG:
                                 case \IMAGETYPE_JPEG:
                                 case \IMAGETYPE_GIF:
                                     $style->update(['coverPhotoExtension' => $fileExtension]);
+
+                                    // Reload the style editor to include the cover photo.
+                                    $style = new self(new Style($style->styleID));
+                                    $style->createCoverPhotoVariant();
+                                    break;
                             }
                         }
                     } catch (SystemException $e) {
@@ -836,7 +852,7 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
         }
 
         // append cover photo
-        $coverPhoto = ($this->coverPhotoExtension) ? $this->getAssetPath() . 'coverPhoto.' . $this->coverPhotoExtension : '';
+        $coverPhoto = $this->coverPhotoExtension ? $this->getCoverPhotoLocation(false) : '';
         if ($coverPhoto && @\file_exists($coverPhoto)) {
             $styleTar->add($coverPhoto, '', FileUtil::addTrailingSlash(\dirname($coverPhoto)));
         }
@@ -1146,10 +1162,6 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
         StyleCompiler::getInstance()->compile($this->getDecoratedObject());
     }
 
-    public function createCoverPhotoVariant(): void
-    {
-    }
-
     /**
      * @inheritDoc
      * @return  Style