From 25f41e0c5948b18aec26022dd12ca89871f6f084 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 10 May 2021 19:08:01 +0200 Subject: [PATCH] Generate WebP variants of users' cover photos See #4187 --- .../cover/photo/IWebpUserCoverPhoto.class.php | 20 +++++++++++++++++++ .../user/cover/photo/UserCoverPhoto.class.php | 17 +++++++++++++++- .../worker/UserRebuildDataWorker.class.php | 13 ++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 wcfsetup/install/files/lib/data/user/cover/photo/IWebpUserCoverPhoto.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 index 0000000000..e2a3dcac57 --- /dev/null +++ b/wcfsetup/install/files/lib/data/user/cover/photo/IWebpUserCoverPhoto.class.php @@ -0,0 +1,20 @@ + + * @package WoltLabSuite\Core\Data\User\Cover\Photo + * @since 5.4 + */ +interface IWebpUserCoverPhoto extends IUserCoverPhoto +{ + /** + * @return null|bool + */ + public function createWebpVariant(); +} diff --git a/wcfsetup/install/files/lib/data/user/cover/photo/UserCoverPhoto.class.php b/wcfsetup/install/files/lib/data/user/cover/photo/UserCoverPhoto.class.php index eafa4967d4..ddb00a1ac9 100644 --- a/wcfsetup/install/files/lib/data/user/cover/photo/UserCoverPhoto.class.php +++ b/wcfsetup/install/files/lib/data/user/cover/photo/UserCoverPhoto.class.php @@ -13,7 +13,7 @@ use wcf\util\ImageUtil; * @license GNU Lesser General Public License * @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. * diff --git a/wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php b/wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php index f55276b0ce..10745049e3 100644 --- a/wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php +++ b/wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php @@ -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(); + } + } + } } } } -- 2.20.1