Skip to content

Commit 51aa677

Browse files
Merge pull request #2 from pykettk/configuration
Make Image Dimensions Configurable
2 parents de70bf6 + 123a2a6 commit 51aa677

File tree

7 files changed

+262
-6
lines changed

7 files changed

+262
-6
lines changed

Model/Config/Source/SizeMode.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Basecom\LiveSearchImageUrls\Model\Config\Source;
5+
6+
use Magento\Framework\Data\OptionSourceInterface;
7+
8+
class SizeMode implements OptionSourceInterface
9+
{
10+
public const MODE_AUTO = 'automatic';
11+
public const MODE_MANUAL = 'manual';
12+
13+
public function toOptionArray(): array
14+
{
15+
return [
16+
['value' => self::MODE_AUTO, 'label' => __(ucwords(self::MODE_AUTO))],
17+
['value' => self::MODE_MANUAL, 'label' => __(ucwords(self::MODE_MANUAL))],
18+
];
19+
}
20+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Basecom\LiveSearchImageUrls\Model\Config\Source;
5+
6+
use Magento\Framework\Data\OptionSourceInterface;
7+
use Magento\Framework\StdLib\ArrayManager;
8+
use Magento\Framework\View\ConfigInterface;
9+
10+
class ViewXmlImageTypes implements OptionSourceInterface
11+
{
12+
public function __construct(
13+
private readonly ArrayManager $arrayManager,
14+
private readonly ConfigInterface $viewConfig
15+
) {
16+
}
17+
18+
public function toOptionArray(): array
19+
{
20+
$imageTypes = [];
21+
$viewConfig = $this->viewConfig->getViewConfig(['area' => 'frontend'])->read() ?? [];
22+
$imageConfig = $this->arrayManager->get('media/Magento_Catalog/images', $viewConfig) ?? [];
23+
24+
foreach ($imageConfig as $type => $config) {
25+
$suffix = '';
26+
27+
if (array_key_exists('width', $config)) {
28+
$widthSuffix = 'W: ' . $config['width'] . 'px';
29+
$heightSuffix = array_key_exists('height', $config)
30+
? 'H: ' . $config['height'] . 'px'
31+
: 'H: auto';
32+
33+
$suffix = sprintf('(%s, %s)', $widthSuffix, $heightSuffix);
34+
}
35+
36+
$imageTypes[] = [
37+
'value' => $type,
38+
'label' => ucwords(str_replace('_', ' ', $type)) . $suffix,
39+
];
40+
}
41+
42+
return $imageTypes;
43+
}
44+
}

Plugin/SetCachedImageUrls.php

+58-6
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,40 @@
33

44
namespace Basecom\LiveSearchImageUrls\Plugin;
55

6+
use Basecom\LiveSearchImageUrls\Model\Config\Source\SizeMode;
7+
use Basecom\LiveSearchImageUrls\System\ModuleConfig;
68
use Magento\Catalog\Helper\Image as ImageHelper;
79
use Magento\Catalog\Model\Product;
810
use Magento\Catalog\Model\ProductFactory;
911
use Magento\CatalogDataExporter\Model\Provider\Product\Formatter\ImageFormatter;
12+
use Magento\Framework\Stdlib\ArrayManager;
13+
use Magento\Framework\View\ConfigInterface;
1014

1115
class SetCachedImageUrls
1216
{
1317
/**
1418
* @var Product|null
1519
*/
1620
private ?Product $product = null;
21+
private array $frontendViewConfig = [];
1722

1823
/**
24+
* @param ModuleConfig $moduleConfig
1925
* @param ImageHelper $imageHelper
2026
* @param ProductFactory $productFactory
27+
* @param ArrayManager $arrayManager
28+
* @param ConfigInterface $viewConfig
29+
* @param int|null $fallbackHeight
30+
* @param int|null $fallbackWidth
2131
*/
2232
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,
2540
) {
2641
}
2742

@@ -84,9 +99,46 @@ private function getResizedImageUrl(string $imageFile): string
8499
return $imageHelper->getDefaultPlaceholderUrl('thumbnail');
85100
}
86101

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;
91143
}
92144
}

System/ModuleConfig.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Basecom\LiveSearchImageUrls\System;
5+
6+
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Store\Model\ScopeInterface;
8+
9+
class ModuleConfig
10+
{
11+
private const XML_PATH_RESIZE_MODE = 'storefront_features/image_dimensions/size_mode';
12+
private const XML_PATH_RESIZE_IMAGE_ID = 'storefront_features/image_dimensions/image_id';
13+
private const XML_PATH_RESIZE_HEIGHT = 'storefront_features/image_dimensions/resize_height';
14+
private const XML_PATH_RESIZE_WIDTH = 'storefront_features/image_dimensions/resize_width';
15+
16+
public function __construct(
17+
private readonly ScopeConfigInterface $scopeConfig
18+
) {
19+
}
20+
21+
public function getResizeMode($scopeId = null): ?string
22+
{
23+
return $this->scopeConfig->getValue(self::XML_PATH_RESIZE_MODE, ScopeInterface::SCOPE_STORE, $scopeId);
24+
}
25+
26+
public function getImageId($scopeId = null): ?string
27+
{
28+
return $this->scopeConfig->getValue(self::XML_PATH_RESIZE_IMAGE_ID, ScopeInterface::SCOPE_STORE, $scopeId);
29+
}
30+
31+
public function getResizeHeight($scopeId = null): ?int
32+
{
33+
return (int)$this->scopeConfig->getValue(self::XML_PATH_RESIZE_HEIGHT, ScopeInterface::SCOPE_STORE, $scopeId)
34+
?? null;
35+
}
36+
37+
public function getResizeWidth($scopeId = null): ?int
38+
{
39+
return (int)$this->scopeConfig->getValue(self::XML_PATH_RESIZE_WIDTH, ScopeInterface::SCOPE_STORE, $scopeId)
40+
?? null;
41+
}
42+
}

etc/adminhtml/system.xml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
4+
<system>
5+
<section id="storefront_features">
6+
<group id="image_dimensions"
7+
translate="label"
8+
sortOrder="100"
9+
showInDefault="1"
10+
showInWebsite="1"
11+
showInStore="1">
12+
<label>Image Dimensions</label>
13+
<field id="size_mode"
14+
translate="label comment"
15+
type="select"
16+
sortOrder="10"
17+
showInDefault="1"
18+
showInWebsite="1"
19+
showInStore="1"
20+
canRestore="1">
21+
<label>Resize Mode</label>
22+
<comment>
23+
<![CDATA[
24+
Only works on Hyvä Storefronts!
25+
<br>
26+
Auto: Read from theme's view.xml file (recommended).
27+
<br>
28+
Manual: Define custom width and height values.
29+
]]>
30+
</comment>
31+
<source_model>Basecom\LiveSearchImageUrls\Model\Config\Source\SizeMode</source_model>
32+
</field>
33+
<field id="image_id"
34+
translate="label"
35+
type="select"
36+
sortOrder="20"
37+
showInDefault="1"
38+
showInWebsite="1"
39+
showInStore="1"
40+
canRestore="1">
41+
<label>Image ID</label>
42+
<source_model>Basecom\LiveSearchImageUrls\Model\Config\Source\ViewXmlImageTypes</source_model>
43+
<depends>
44+
<field id="storefront_features/image_dimensions/size_mode">automatic</field>
45+
</depends>
46+
</field>
47+
<field id="resize_height"
48+
translate="label"
49+
type="text"
50+
sortOrder="30"
51+
showInDefault="1"
52+
showInWebsite="1"
53+
showInStore="1"
54+
canRestore="1">
55+
<label>Height (px)</label>
56+
<validate>required-entry integer</validate>
57+
<depends>
58+
<field id="storefront_features/image_dimensions/size_mode">manual</field>
59+
</depends>
60+
</field>
61+
<field id="resize_width"
62+
translate="label"
63+
type="text"
64+
sortOrder="40"
65+
showInDefault="1"
66+
showInWebsite="1"
67+
showInStore="1"
68+
canRestore="1">
69+
<label>Width (px)</label>
70+
<validate>required-entry integer</validate>
71+
<depends>
72+
<field id="storefront_features/image_dimensions/size_mode">manual</field>
73+
</depends>
74+
</field>
75+
</group>
76+
</section>
77+
</system>
78+
</config>

etc/config.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
4+
<default>
5+
<storefront_features>
6+
<image_dimensions>
7+
<size_mode>automatic</size_mode>
8+
<image_id>product_page_image_small</image_id>
9+
<resize_height>90</resize_height>
10+
<resize_width>90</resize_width>
11+
</image_dimensions>
12+
</storefront_features>
13+
</default>
14+
</config>

etc/di.xml

+6
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
sortOrder="10"
88
/>
99
</type>
10+
<type name="Basecom\LiveSearchImageUrls\Plugin\SetCachedImageUrls">
11+
<arguments>
12+
<argument name="fallbackHeight" xsi:type="number">90</argument>
13+
<argument name="fallbackWidth" xsi:type="number">90</argument>
14+
</arguments>
15+
</type>
1016
</config>

0 commit comments

Comments
 (0)