diff --git a/Classes/ViewHelpers/Image/Modify/FormatViewHelper.php b/Classes/ViewHelpers/Image/Modify/FormatViewHelper.php index e03cda8..21a3c4e 100644 --- a/Classes/ViewHelpers/Image/Modify/FormatViewHelper.php +++ b/Classes/ViewHelpers/Image/Modify/FormatViewHelper.php @@ -4,6 +4,9 @@ namespace Sitegeist\MediaComponents\ViewHelpers\Image\Modify; use Sitegeist\MediaComponents\Domain\Model\ImageSource; +use SMS\FluidComponents\Interfaces\ProcessableImage; +use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; class FormatViewHelper extends AbstractViewHelper @@ -17,10 +20,27 @@ public function initializeArguments(): void public function render(): ImageSource { $imageSource = $this->arguments['imageSource'] ?? $this->renderChildren(); + + if (!$this->arguments['format'] + && $imageSource->getOriginalImage() instanceof ProcessableImage + && in_array( + $imageSource->getOriginalImage()->getFile()->getExtension(), + $this->getAutoWebpConversionFormats() + ) + ) { + $this->arguments['format'] = 'webp'; + } + if ($this->arguments['format']) { $imageSourceWithFormat = clone $imageSource; $imageSourceWithFormat->setFormat($this->arguments['format']); } return $imageSourceWithFormat ?? $imageSource; } + + private function getAutoWebpConversionFormats(): array + { + $extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class); + return GeneralUtility::trimExplode(',', $extensionConfiguration->get('media_components', 'autoWebpConversionFormats'), true); + } } diff --git a/README.md b/README.md index 723c1ca..7e3c0e4 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,12 @@ This extension provides ready-to-use [Fluid Components](https://github.com/siteg * Video files * [HTML5 video tag with support for subtitles](./Resources/Private/Components/Video/Video.html) + +## WebP conversion + +As these components are such basic atoms, you could use them to change the format of any image to WebP to reduce file sizes. The extension configuration contains a list of file extensions that will be converted to WebP if not explicitly defined with `format=`. +You colud use `autoWebpConversionFormats` with `gif,jpg,jpeg,tif,tiff,bmp,pcx,tga,png,pdf,ai` to get full conversion to WebP (without `svg` as it already has a smaller file size). + ## Usage We use the public namespace from fluid-components. diff --git a/Tests/Functional/ImageComponentTest.php b/Tests/Functional/ImageComponentTest.php index 5473ee3..29176dc 100644 --- a/Tests/Functional/ImageComponentTest.php +++ b/Tests/Functional/ImageComponentTest.php @@ -15,7 +15,8 @@ public static function imageComponentTestProvider(): array return [ 'Only mandatory data provided' => [ '', - '' + '', + '', ], 'All data provided' => [ 'Alt text', @@ -32,17 +33,33 @@ public static function imageComponentTestProvider(): array title="Title text" lazyload="true" preload="true" - />' - ] + />', + '', + ], + 'Active WebP auto conversion' => [ + '', + '', + 'jpeg, png', + ], + 'Forced Format WebP auto conversion' => [ + '', + '', + 'jpeg, png', + ], + 'Not applying WebP auto conversion' => [ + '', + '', + 'jpeg, ai', + ], ]; } #[Test] #[DataProvider('imageComponentTestProvider')] - public function imageComponentTest(string $expectedResult, string $input): void { + public function imageComponentTest(string $expectedResult, string $input, string $autoWebpConversionFormats): void { + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['media_components']['autoWebpConversionFormats'] = $autoWebpConversionFormats; $view = $this->getTestView($input); $result = $this->cleanUpTestResult($view->render()); - $this->assertStringContainsString($expectedResult, $result); } } diff --git a/ext_conf_template.txt b/ext_conf_template.txt new file mode 100644 index 0000000..c3d6e8b --- /dev/null +++ b/ext_conf_template.txt @@ -0,0 +1,2 @@ +# cat=Format/enable; type=string; label=Auto WebP conversion: for specific formats, "," separated; +autoWebpConversionFormats =