Skip to content

Commit

Permalink
[FEATURE] add ext_conf to globally change formats
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichmathes committed Jan 2, 2025
1 parent f40fce2 commit 0906819
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Classes/ViewHelpers/Image/Modify/FormatViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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->getForcedWebpFormats()
)
) {
$this->arguments['format'] = 'webp';
}

if ($this->arguments['format']) {
$imageSourceWithFormat = clone $imageSource;
$imageSourceWithFormat->setFormat($this->arguments['format']);
}
return $imageSourceWithFormat ?? $imageSource;
}

private function getForcedWebpFormats(): array
{
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
return GeneralUtility::trimExplode(',', $extensionConfiguration->get('media_components', 'autoWebpConversionFormats'), true);
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=`.
By default the `autoWebpConversionFormats` are `tif,tiff,bmp,pcx,tga,ai`, which you could extend with `gif,jpg,jpeg,png` 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.
Expand Down
2 changes: 2 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cat=Format/enable; type=string; label=Auto WebP conversion: for specific formats, "," separated;
autoWebpConversionFormats = tif,tiff,bmp,pcx,tga,ai

0 comments on commit 0906819

Please sign in to comment.