-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-4647: Added WebpFormatVariationPathGenerator handling .webp images (
#361)
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); | ||
} | ||
} |