|
3 | 3 |
|
4 | 4 | namespace Basecom\LiveSearchImageUrls\Plugin;
|
5 | 5 |
|
| 6 | +use Basecom\LiveSearchImageUrls\Model\Config\Source\SizeMode; |
| 7 | +use Basecom\LiveSearchImageUrls\System\ModuleConfig; |
6 | 8 | use Magento\Catalog\Helper\Image as ImageHelper;
|
7 | 9 | use Magento\Catalog\Model\Product;
|
8 | 10 | use Magento\Catalog\Model\ProductFactory;
|
9 | 11 | use Magento\CatalogDataExporter\Model\Provider\Product\Formatter\ImageFormatter;
|
| 12 | +use Magento\Framework\Stdlib\ArrayManager; |
| 13 | +use Magento\Framework\View\ConfigInterface; |
10 | 14 |
|
11 | 15 | class SetCachedImageUrls
|
12 | 16 | {
|
13 | 17 | /**
|
14 | 18 | * @var Product|null
|
15 | 19 | */
|
16 | 20 | private ?Product $product = null;
|
| 21 | + private array $frontendViewConfig = []; |
17 | 22 |
|
18 | 23 | /**
|
| 24 | + * @param ModuleConfig $moduleConfig |
19 | 25 | * @param ImageHelper $imageHelper
|
20 | 26 | * @param ProductFactory $productFactory
|
| 27 | + * @param ArrayManager $arrayManager |
| 28 | + * @param ConfigInterface $viewConfig |
| 29 | + * @param int|null $fallbackHeight |
| 30 | + * @param int|null $fallbackWidth |
21 | 31 | */
|
22 | 32 | public function __construct(
|
23 |
| - private readonly ImageHelper $imageHelper, |
24 |
| - private readonly ProductFactory $productFactory |
| 33 | + private readonly ModuleConfig $moduleConfig, |
| 34 | + private readonly ImageHelper $imageHelper, |
| 35 | + private readonly ProductFactory $productFactory, |
| 36 | + private readonly ArrayManager $arrayManager, |
| 37 | + private readonly ConfigInterface $viewConfig, |
| 38 | + private readonly ?int $fallbackHeight = null, |
| 39 | + private readonly ?int $fallbackWidth = null, |
25 | 40 | ) {
|
26 | 41 | }
|
27 | 42 |
|
@@ -84,9 +99,46 @@ private function getResizedImageUrl(string $imageFile): string
|
84 | 99 | return $imageHelper->getDefaultPlaceholderUrl('thumbnail');
|
85 | 100 | }
|
86 | 101 |
|
87 |
| - /** |
88 |
| - * Resize image to the default 90 x 90 px dimensions provided in the view.xml file |
89 |
| - */ |
90 |
| - return $imageHelper->setImageFile($imageFile)->resize(90, 90)->getUrl(); |
| 102 | + $imageDimensions = $this->getImageDimensions(); |
| 103 | + |
| 104 | + return $imageHelper->setImageFile($imageFile) |
| 105 | + ->resize($imageDimensions['width'], $imageDimensions['height']) |
| 106 | + ->getUrl(); |
| 107 | + } |
| 108 | + |
| 109 | + private function getImageDimensions(): array |
| 110 | + { |
| 111 | + if ($this->moduleConfig->getResizeMode() === SizeMode::MODE_MANUAL) { |
| 112 | + return [ |
| 113 | + 'height' => $this->moduleConfig->getResizeHeight(), |
| 114 | + 'width' => $this->moduleConfig->getResizeWidth(), |
| 115 | + ]; |
| 116 | + } |
| 117 | + |
| 118 | + if ($imageId = $this->moduleConfig->getImageId()) { |
| 119 | + $imageConfig = $this->arrayManager->get( |
| 120 | + sprintf('media/Magento_Catalog/images/%s', $imageId), |
| 121 | + $this->getFrontendViewConfig() |
| 122 | + ) ?? []; |
| 123 | + |
| 124 | + return [ |
| 125 | + 'height' => $this->arrayManager->get('height', $imageConfig), |
| 126 | + 'width' => $this->arrayManager->get('width', $imageConfig), |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
| 130 | + return [ |
| 131 | + 'height' => $this->fallbackHeight, |
| 132 | + 'width' => $this->fallbackWidth, |
| 133 | + ]; |
| 134 | + } |
| 135 | + |
| 136 | + private function getFrontendViewConfig(): array |
| 137 | + { |
| 138 | + if (!$this->frontendViewConfig) { |
| 139 | + $this->frontendViewConfig = $this->viewConfig->getViewConfig(['area' => 'frontend'])->read() ?? []; |
| 140 | + } |
| 141 | + |
| 142 | + return $this->frontendViewConfig; |
91 | 143 | }
|
92 | 144 | }
|
0 commit comments