Skip to content

Commit

Permalink
improved image extension for use with imgproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin committed Nov 7, 2024
1 parent bc3635b commit 6475c43
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/Twig/ImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Shopsys\ReadModelBundle\Twig;

use Shopsys\FrameworkBundle\Component\Image\Exception\ImageNotFoundException;
use Shopsys\FrameworkBundle\Component\Image\ImageUrlWithSizeHelper;
use Shopsys\FrameworkBundle\Twig\ImageExtension as BaseImageExtension;
use Shopsys\ReadModelBundle\Image\ImageView;

Expand Down Expand Up @@ -42,24 +44,31 @@ public function getImageHtml($imageView, array $attributes = []): string
}

/**
* @param \Shopsys\FrameworkBundle\Component\Image\Image|\Shopsys\ReadModelBundle\Image\ImageView|object $imageView
* @param string|null $type
* @param \Shopsys\FrameworkBundle\Component\Image\Image|\Shopsys\ReadModelBundle\Image\ImageView|object $imageOrEntity
* @param array $attributes
* @return string
*/
protected function getImageUrl($imageView, ?string $type = null): string
protected function getImageUrl($imageOrEntity, array $attributes): string
{
if ($imageView instanceof ImageView) {
$entityName = $imageView->getEntityName();
$width = null;
$height = null;

return $this->imageFacade->getImageUrlFromAttributes(
$this->domain->getCurrentDomainConfig(),
$imageView->getId(),
$imageView->getExtension(),
$entityName,
$type,
);
if (array_key_exists('width', $attributes)) {
$width = (int)$attributes['width'];
}

if (array_key_exists('height', $attributes)) {
$height = (int)$attributes['height'];
}

return parent::getImageUrl($imageView, $type);
try {
return ImageUrlWithSizeHelper::limitSizeInImageUrl($this->imageFacade->getImageUrl(
$this->domain->getCurrentDomainConfig(),
$imageOrEntity,
$attributes['type'],
), $width, $height);
} catch (ImageNotFoundException $e) {
return $this->getEmptyImageUrl();
}
}
}

0 comments on commit 6475c43

Please sign in to comment.