Skip to content

Commit

Permalink
IBX-4647: Added WebpFormatVariationPathGenerator handling .webp images (
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx authored Feb 15, 2023
1 parent b787ccb commit 0d4e86d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Core\Imagine\VariationPathGenerator;

use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;

/**
* Decorates VariationPathGenerator with .webp extension if image variation is configured for this format.
*/
final class WebpFormatVariationPathGenerator implements VariationPathGenerator
{
/** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator */
private $innerVariationPathGenerator;

/** @var \Liip\ImagineBundle\Imagine\Filter\FilterConfiguration */
private $filterConfiguration;

public function __construct(
VariationPathGenerator $innerVariationPathGenerator,
FilterConfiguration $filterConfiguration
) {
$this->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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Bundle\Core\Imagine\VariationPathGenerator;

use eZ\Bundle\EzPublishCoreBundle\Imagine\Filter\FilterConfiguration;
use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator;
use Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator;
use PHPUnit\Framework\TestCase;

final class WebpFormatVariationPathGeneratorTest extends TestCase
{
/** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator|\PHPUnit\Framework\MockObject\MockObject */
private $innerVariationPathGenerator;

/** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\Filter\FilterConfiguration|\PHPUnit\Framework\MockObject\MockObject */
private $filterConfiguration;

protected function setUp(): void
{
$this->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')
);
}
}

0 comments on commit 0d4e86d

Please sign in to comment.