From 4cc0a9b2ecc266408197ea08500ad75a4952ee12 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 29 Jan 2021 13:38:06 +0100 Subject: [PATCH] Dynamically create WebP variants for style imports --- .../files/lib/data/style/Style.class.php | 2 +- .../lib/data/style/StyleEditor.class.php | 32 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/wcfsetup/install/files/lib/data/style/Style.class.php b/wcfsetup/install/files/lib/data/style/Style.class.php index 15f58cc77e..41c6a549e1 100644 --- a/wcfsetup/install/files/lib/data/style/Style.class.php +++ b/wcfsetup/install/files/lib/data/style/Style.class.php @@ -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 diff --git a/wcfsetup/install/files/lib/data/style/StyleEditor.class.php b/wcfsetup/install/files/lib/data/style/StyleEditor.class.php index 6978568180..42a52d982d 100644 --- a/wcfsetup/install/files/lib/data/style/StyleEditor.class.php +++ b/wcfsetup/install/files/lib/data/style/StyleEditor.class.php @@ -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 -- 2.20.1