diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/image.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/image.yml index 35b13ccf64..753a868287 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/image.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/image.yml @@ -285,5 +285,11 @@ services: ezpublish.image_alias.variation_path_generator.alias_directory: class: eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator\AliasDirectoryVariationPathGenerator + Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator: + decorates: ezpublish.image_alias.variation_path_generator + arguments: + $innerVariationPathGenerator: '@.inner' + $filterConfiguration: '@liip_imagine.filter.configuration' + # SPI Aliases eZ\Publish\SPI\Variation\VariationHandler: '@ezpublish.image_alias.imagine.variation.imagine_alias_generator' diff --git a/src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php b/src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php new file mode 100644 index 0000000000..0eff5700c8 --- /dev/null +++ b/src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php @@ -0,0 +1,44 @@ +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/tests/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGeneratorTest.php b/tests/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGeneratorTest.php new file mode 100644 index 0000000000..ce36352d4d --- /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') + ); + } +}