diff --git a/src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php b/src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php new file mode 100644 index 0000000000..62d9809e45 --- /dev/null +++ b/src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php @@ -0,0 +1,42 @@ +innerVariationPathGenerator = $innerVariationPathGenerator; + $this->filterConfiguration = $filterConfiguration; + } + + public function getVariationPath($originalPath, $filter): string + { + $variationPath = $this->innerVariationPathGenerator->getVariationPath($originalPath, $filter); + $filterConfig = $this->filterConfiguration->get($filter); + + if (!isset($filterConfig['format']) || $filterConfig['format'] !== 'webp') { + return $variationPath; + } + + return $variationPath . '.webp'; + } +} diff --git a/src/bundle/Core/Resources/config/image.yml b/src/bundle/Core/Resources/config/image.yml index c49a7e40d3..ec4cee3348 100644 --- a/src/bundle/Core/Resources/config/image.yml +++ b/src/bundle/Core/Resources/config/image.yml @@ -299,6 +299,12 @@ services: tags: - { name: 'ibexa.media.images.variation.handler', identifier: 'alias' } + Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator: + decorates: ibexa.image_alias.variation_path_generator + arguments: + $innerVariationPathGenerator: '@.inner' + $filterConfiguration: '@liip_imagine.filter.configuration' + # SPI Aliases Ibexa\Contracts\Core\Variation\VariationHandler: '@Ibexa\Bundle\Core\Variation\VariationHandlerResolver' diff --git a/tests/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGeneratorTest.php b/tests/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGeneratorTest.php new file mode 100644 index 0000000000..adcbb83cd1 --- /dev/null +++ b/tests/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGeneratorTest.php @@ -0,0 +1,77 @@ +innerVariationPathGenerator = $this->createMock(VariationPathGenerator::class); + $this->filterConfiguration = $this->createMock(FilterConfiguration::class); + } + + public function testGetVariationPath(): void + { + $this->innerVariationPathGenerator + ->method('getVariationPath') + ->willReturn('tmp/variation/test.jpeg'); + + $this->filterConfiguration + ->method('get') + ->with('large') + ->willReturn([ + 'format' => 'webp', + ]); + + $generator = new WebpFormatVariationPathGenerator( + $this->innerVariationPathGenerator, + $this->filterConfiguration + ); + + self::assertEquals( + 'tmp/variation/test.jpeg.webp', + $generator->getVariationPath('tmp/original/test.jpeg', 'large') + ); + } + + public function testGetVariationNonWebpVariation(): void + { + $this->innerVariationPathGenerator + ->method('getVariationPath') + ->willReturn('tmp/variation/test.jpeg'); + + $this->filterConfiguration + ->method('get') + ->with('large') + ->willReturn([ + 'format' => 'jpeg', + ]); + + $generator = new WebpFormatVariationPathGenerator( + $this->innerVariationPathGenerator, + $this->filterConfiguration + ); + + self::assertEquals( + 'tmp/variation/test.jpeg', + $generator->getVariationPath('tmp/original/test.jpeg', 'large') + ); + } +}