OP-52: Fix error trying to filter svg image with LiipImagineBundle

This commit is contained in:
Michał Pysiak
2023-03-09 09:28:06 +01:00
parent 2b47d2ac47
commit 1296a893aa
2 changed files with 32 additions and 4 deletions

View File

@@ -35,13 +35,29 @@ final class ImageTransformerSpec extends ObjectBehavior
FilterService $filterService
): void {
$product->getImagesByType('main')->willReturn(new ArrayCollection([$productImage->getWrappedObject()]));
$productImage->getPath()->willReturn('/path-to-image');
$productImage->getPath()->willReturn('/path-to-image.png');
$filterService
->getUrlOfFilteredImage('/path-to-image', 'sylius_shop_product_thumbnail')
->getUrlOfFilteredImage('/path-to-image.png', 'sylius_shop_product_thumbnail')
->shouldBeCalled()
;
$this->transform($product);
}
function it_does_not_transforms_svg_product_images_into_product_thumbnail(
ProductInterface $product,
ImageInterface $productImage,
FilterService $filterService
): void {
$product->getImagesByType('main')->willReturn(new ArrayCollection([$productImage->getWrappedObject()]));
$productImage->getPath()->willReturn('/path-to-image.svg');
$filterService
->getUrlOfFilteredImage('/path-to-image.svg', 'sylius_shop_product_thumbnail')
->shouldNotBeCalled()
;
$this->transform($product);
}
}

View File

@@ -24,9 +24,12 @@ final class ImageTransformer implements TransformerInterface
private FilterService $imagineFilter;
public function __construct(FilterService $imagineFilter)
private string $imagesPath;
public function __construct(FilterService $imagineFilter, string $imagesPath = '/media/image/')
{
$this->imagineFilter = $imagineFilter;
$this->imagesPath = $imagesPath;
}
public function transform(ProductInterface $product): ?string
@@ -40,6 +43,15 @@ final class ImageTransformer implements TransformerInterface
/** @var ImageInterface $productImage */
$productImage = $productThumbnails->first();
return $this->imagineFilter->getUrlOfFilteredImage($productImage->getPath(), self::SYLIUS_THUMBNAIL_FILTER);
if ($this->canImageBeFiltered($productImage->getPath())) {
return $this->imagineFilter->getUrlOfFilteredImage($productImage->getPath(), self::SYLIUS_THUMBNAIL_FILTER);
}
return $this->imagesPath . $productImage->getPath();
}
private function canImageBeFiltered(string $imagePath): bool
{
return !str_ends_with($imagePath, 'svg');
}
}