|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cloudinary\Cloudinary\Plugin\Catalog\Block\Product; |
| 6 | + |
| 7 | +use Cloudinary\Cloudinary\Core\ConfigurationInterface; |
| 8 | +use Cloudinary\Cloudinary\Core\Image\ImageFactory as CloudinaryImageFactory; |
| 9 | +use Cloudinary\Cloudinary\Core\Image\Transformation; |
| 10 | +use Cloudinary\Cloudinary\Core\Image\Transformation\Crop; |
| 11 | +use Cloudinary\Cloudinary\Core\Image\Transformation\Dimensions; |
| 12 | +use Cloudinary\Cloudinary\Core\UrlGenerator; |
| 13 | +use Cloudinary\Cloudinary\Model\Transformation as TransformationModel; |
| 14 | +use Cloudinary\Cloudinary\Model\TransformationFactory; |
| 15 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 16 | +use Magento\Catalog\Block\Product\Image as ImageBlock; |
| 17 | +use Magento\Catalog\Block\Product\ImageFactory as CatalogImageFactory; |
| 18 | +use Magento\Catalog\Model\Product; |
| 19 | + |
| 20 | +class ImageFactory |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CloudinaryImageFactory |
| 24 | + */ |
| 25 | + private $cloudinaryImageFactory; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var UrlGenerator |
| 29 | + */ |
| 30 | + private $urlGenerator; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ProductInterface |
| 34 | + */ |
| 35 | + private $product; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Dimensions |
| 39 | + */ |
| 40 | + private $dimensions; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ConfigurationInterface |
| 44 | + */ |
| 45 | + private $configuration; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + private $imageFile; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var bool |
| 54 | + */ |
| 55 | + private $keepFrame; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var TransformationModel |
| 59 | + */ |
| 60 | + private $transformationModel; |
| 61 | + |
| 62 | + /** |
| 63 | + * @param CloudinaryImageFactory $cloudinaryImageFactory |
| 64 | + * @param UrlGenerator $urlGenerator |
| 65 | + * @param ConfigurationInterface $configuration |
| 66 | + */ |
| 67 | + public function __construct( |
| 68 | + CloudinaryImageFactory $cloudinaryImageFactory, |
| 69 | + UrlGenerator $urlGenerator, |
| 70 | + ConfigurationInterface $configuration, |
| 71 | + TransformationFactory $transformationFactory |
| 72 | + ) { |
| 73 | + $this->cloudinaryImageFactory = $cloudinaryImageFactory; |
| 74 | + $this->urlGenerator = $urlGenerator; |
| 75 | + $this->configuration = $configuration; |
| 76 | + $this->transformationModel = $transformationFactory->create(); |
| 77 | + $this->dimensions = null; |
| 78 | + $this->imageFile = null; |
| 79 | + $this->keepFrame = true; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create image block from product |
| 84 | + * @param CatalogImageFactory $catalogImageFactory |
| 85 | + * @param callable $proceed |
| 86 | + * @param Product $product |
| 87 | + * @param string $imageId |
| 88 | + * @param array|null $attributes |
| 89 | + * @return ImageBlock |
| 90 | + */ |
| 91 | + public function aroundCreate(CatalogImageFactory $catalogImageFactory, callable $proceed, Product $product, string $imageId, array $attributes = null): ImageBlock |
| 92 | + { |
| 93 | + $imageBlock = $proceed($product, $imageId, $attributes); |
| 94 | + if (!$this->configuration->isEnabled()) { |
| 95 | + return $imageBlock; |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + if (strpos($imageBlock->getImageUrl(), $this->configuration->getMediaBaseUrl()) === 0) { |
| 100 | + $imagePath = preg_replace('/^' . preg_quote($this->configuration->getMediaBaseUrl(), '/') . '/', '', $imageBlock->getImageUrl()); |
| 101 | + $imagePath = preg_replace('/\/cache\/[a-f0-9]{32}\//', '/', $imagePath); |
| 102 | + $image = $this->cloudinaryImageFactory->build( |
| 103 | + $imagePath, |
| 104 | + function () use ($imageBlock) { |
| 105 | + return $imageBlock->getImageUrl(); |
| 106 | + } |
| 107 | + ); |
| 108 | + $generatedImageUrl = $this->urlGenerator->generateFor( |
| 109 | + $image, |
| 110 | + $this->transformationModel->addFreeformTransformationForImage( |
| 111 | + $this->createTransformation($imageBlock), |
| 112 | + $imagePath |
| 113 | + ) |
| 114 | + ); |
| 115 | + $imageBlock->setOriginalImageUrl($imageBlock->setImageUrl()); |
| 116 | + $imageBlock->setImageUrl($generatedImageUrl); |
| 117 | + } |
| 118 | + } catch (\Exception $e) { |
| 119 | + $imageBlock = $proceed($product, $imageId, $attributes); |
| 120 | + } |
| 121 | + |
| 122 | + return $imageBlock; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param ImageBlock $imageBlock |
| 127 | + * @return Transformation |
| 128 | + */ |
| 129 | + private function createTransformation(ImageBlock $imageBlock) |
| 130 | + { |
| 131 | + $dimensions = $this->dimensions ?: Dimensions::fromWidthAndHeight($imageBlock->getWidth(), $imageBlock->getHeight()); |
| 132 | + |
| 133 | + $transform = $this->configuration->getDefaultTransformation()->withDimensions($dimensions); |
| 134 | + |
| 135 | + if ($this->keepFrame) { |
| 136 | + $transform->withCrop(Crop::fromString('lpad')) |
| 137 | + ->withDimensions(Dimensions::squareMissingDimension($dimensions)); |
| 138 | + } else { |
| 139 | + $transform->withCrop(Crop::fromString('fit')); |
| 140 | + } |
| 141 | + |
| 142 | + return $transform; |
| 143 | + } |
| 144 | +} |
0 commit comments