Generate WebP variants of users' cover photos
authorAlexander Ebert <ebert@woltlab.com>
Mon, 10 May 2021 17:08:01 +0000 (19:08 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 10 May 2021 17:08:01 +0000 (19:08 +0200)
See #4187

wcfsetup/install/files/lib/data/user/cover/photo/IWebpUserCoverPhoto.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/data/user/cover/photo/UserCoverPhoto.class.php
wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php

diff --git a/wcfsetup/install/files/lib/data/user/cover/photo/IWebpUserCoverPhoto.class.php b/wcfsetup/install/files/lib/data/user/cover/photo/IWebpUserCoverPhoto.class.php
new file mode 100644 (file)
index 0000000..e2a3dca
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+namespace wcf\data\user\cover\photo;
+
+/**
+ * Any displayable cover photo type should implement this class.
+ * 
+ * @author Alexander Ebert
+ * @copyright 2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\Data\User\Cover\Photo
+ * @since 5.4
+ */
+interface IWebpUserCoverPhoto extends IUserCoverPhoto
+{
+    /**
+     * @return null|bool
+     */
+    public function createWebpVariant();
+}
index eafa4967d4fa4a972792945c138333a71704095d..ddb00a1ac97f6d9b992704c40aaa49dbcb88b651 100644 (file)
@@ -13,7 +13,7 @@ use wcf\util\ImageUtil;
  * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package WoltLabSuite\Core\Data\User\Cover\Photo
  */
-class UserCoverPhoto implements IUserCoverPhoto
+class UserCoverPhoto implements IWebpUserCoverPhoto
 {
     /**
      * file extension
@@ -105,6 +105,21 @@ class UserCoverPhoto implements IUserCoverPhoto
         ) . '/' . $this->userID . '-' . $this->coverPhotoHash . '.' . ($useWebP ? 'webp' : $this->coverPhotoExtension);
     }
 
+    /**
+     * @inheritDoc
+     */
+    public function createWebpVariant()
+    {
+        if ($this->coverPhotoHasWebP) {
+            return;
+        }
+
+        $sourceLocation = WCF_DIR . $this->getFilename($this->coverPhotoExtension === 'webp');
+        $outputFilenameWithoutExtension = \preg_replace('~\.[a-z]+$~', '', $sourceLocation);
+
+        return ImageUtil::createWebpVariant($sourceLocation, $outputFilenameWithoutExtension);
+    }
+
     /**
      * Returns the minimum and maximum dimensions for cover photos.
      *
index f55276b0ce9b403cdbd3332cb16de09e075e5acb..10745049e3fcb7b17bcaffd83be39828376ed5d9 100644 (file)
@@ -6,11 +6,13 @@ use wcf\data\reaction\type\ReactionTypeCache;
 use wcf\data\user\avatar\UserAvatar;
 use wcf\data\user\avatar\UserAvatarEditor;
 use wcf\data\user\avatar\UserAvatarList;
+use wcf\data\user\cover\photo\IWebpUserCoverPhoto;
 use wcf\data\user\User;
 use wcf\data\user\UserEditor;
 use wcf\data\user\UserList;
 use wcf\data\user\UserProfileAction;
 use wcf\system\bbcode\BBCodeHandler;
+use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\SystemException;
 use wcf\system\html\input\HtmlInputProcessor;
@@ -279,6 +281,17 @@ class UserRebuildDataWorker extends AbstractRebuildDataWorker
                     'height' => $height,
                 ]);
             }
+
+            // Create WebP variants of existing cover photos.
+            $userProfiles = UserProfileRuntimeCache::getInstance()->getObjects($userIDs);
+            foreach ($userProfiles as $userProfile) {
+                if ($userProfile->coverPhotoHash) {
+                    $coverPhoto = $userProfile->getCoverPhoto(true);
+                    if ($coverPhoto instanceof IWebpUserCoverPhoto) {
+                        $coverPhoto->createWebpVariant();
+                    }
+                }
+            }
         }
     }
 }