Skip to content

Commit ac84492

Browse files
committed
Add an option to force convert images to WebP
1 parent 01e1b22 commit ac84492

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

com.woltlab.wcf/option.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,13 @@
531531
<defaultvalue>gd</defaultvalue>
532532
<selectoptions>gd:wcf.acp.option.image_adapter_type.gd
533533
imagick:wcf.acp.option.image_adapter_type.imagick</selectoptions>
534+
</option>
535+
<option name="image_convert_format">
536+
<categoryname>general.system.image</categoryname>
537+
<optiontype>radioButton</optiontype>
538+
<defaultvalue>webp</defaultvalue>
539+
<selectoptions>keep:wcf.acp.option.image_convert_format.keep
540+
webp:wcf.acp.option.image_convert_format.webp</selectoptions>
534541
</option>
535542
<!-- /general.system.image -->
536543
<!-- general.system.search -->

constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,4 @@
227227
\define('SERVICE_WORKER_PRIVATE_KEY', '');
228228
\define('SERVICE_WORKER_PUBLIC_KEY', '');
229229
\define('RECAPTCHA_PRIVATEKEY_V3', '');
230+
\define('IMAGE_CONVERT_FORMAT', 'webp');

wcfsetup/install/files/lib/system/endpoint/controller/core/files/GenerateThumbnails.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res
2929
$file = Helper::fetchObjectFromRequestParameter($variables['id'], File::class);
3030

3131
FileProcessor::getInstance()->generateWebpVariant($file);
32+
$file = FileProcessor::getInstance()->convertImageFormat($file);
3233
FileProcessor::getInstance()->generateThumbnails($file);
3334

3435
$thumbnails = [];
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace wcf\system\file\command;
4+
5+
use wcf\data\file\File;
6+
7+
/**
8+
* Converts an image to its WebP variant by replacing it with it.
9+
*
10+
* @author Alexander Ebert
11+
* @copyright 2001-2025 WoltLab GmbH
12+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13+
* @since 6.2
14+
*/
15+
final class ReplaceWithWebpVariant
16+
{
17+
public function __construct(
18+
private readonly File $file,
19+
) {}
20+
21+
public function __invoke(): File
22+
{
23+
$pathnameWebp = $this->file->getPathnameWebp();
24+
if ($pathnameWebp === null) {
25+
return $this->file;
26+
}
27+
28+
$command = new ReplaceFileSource(
29+
$this->file,
30+
$pathnameWebp,
31+
$this->getNewFilename(),
32+
);
33+
$command();
34+
35+
return new File($this->file->fileID);
36+
}
37+
38+
private function getNewFilename(): string
39+
{
40+
$filename = \preg_replace(
41+
'~\.(?:jpe?g|png)$~i',
42+
'',
43+
$this->file->filename,
44+
);
45+
46+
return \sprintf(
47+
"%s.webp",
48+
$filename,
49+
);
50+
}
51+
}

wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use wcf\system\database\util\PreparedStatementConditionBuilder;
1414
use wcf\system\event\EventHandler;
1515
use wcf\system\exception\SystemException;
16+
use wcf\system\file\command\ReplaceWithWebpVariant;
1617
use wcf\system\file\processor\exception\DamagedImage;
1718
use wcf\system\image\adapter\exception\ImageNotProcessable;
1819
use wcf\system\image\adapter\exception\ImageNotReadable;
@@ -424,6 +425,21 @@ public function copy(File $oldFile, string $objectType): File
424425
return $newFile;
425426
}
426427

428+
public function convertImageFormat(File $file): File
429+
{
430+
switch (\IMAGE_CONVERT_FORMAT) {
431+
case 'keep':
432+
return $file;
433+
434+
case 'webp':
435+
$command = new ReplaceWithWebpVariant($file);
436+
return $command();
437+
438+
default:
439+
throw new \LogicException("Unreachable");
440+
}
441+
}
442+
427443
private function copyThumbnails(int $oldFileID, int $newFileID): void
428444
{
429445
$thumbnailList = new FileThumbnailList();

wcfsetup/install/files/lib/system/worker/FileRebuildDataWorker.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use wcf\data\file\File;
66
use wcf\data\file\FileEditor;
77
use wcf\data\file\FileList;
8+
use wcf\system\file\command\ReplaceWithWebpVariant;
89
use wcf\system\file\processor\exception\DamagedImage;
910
use wcf\system\file\processor\FileProcessor;
1011
use wcf\util\FileUtil;
@@ -49,9 +50,10 @@ public function execute()
4950
$this->fixMimeType();
5051

5152
$damagedFileIDs = [];
52-
foreach ($this->objectList as $file) {
53+
foreach ($this->objectList->getObjects() as $file) {
5354
try {
5455
FileProcessor::getInstance()->generateWebpVariant($file);
56+
$file = FileProcessor::getInstance()->convertImageFormat($file);
5557
FileProcessor::getInstance()->generateThumbnails($file);
5658
} catch (DamagedImage $e) {
5759
logThrowable($e);

0 commit comments

Comments
 (0)