Skip to content

Commit 262aa7d

Browse files
committedAug 18, 2024
CLOUDINARY-519 - fix image proportions when loading the swatch image
1 parent 98711e9 commit 262aa7d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed
 

‎Plugin/SwatchImageUrlPlugin.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Cloudinary\Cloudinary\Plugin;
33

44

5+
use Magento\Framework\App\Filesystem\DirectoryList;
56
use Magento\Framework\Exception\LocalizedException;
67
use Magento\Swatches\Helper\Media as SwatchMediaHelper;
78
use Magento\Framework\UrlInterface;
@@ -10,6 +11,7 @@
1011
use Cloudinary\Cloudinary\Model\Configuration;
1112
class SwatchImageUrlPlugin
1213
{
14+
public const SWATCH_MEDIA_PATH = 'attribute/swatch';
1315
/**
1416
* @var StoreManagerInterface
1517
*/
@@ -33,23 +35,30 @@ class SwatchImageUrlPlugin
3335
*/
3436
protected $imageConfig;
3537

38+
protected $imageFactory;
39+
3640
/**
37-
* Constructor
3841
* @param StoreManagerInterface $storeManager
3942
* @param UrlInterface $urlBuilder
4043
* @param Configuration $configuration
4144
* @param \Magento\Framework\View\ConfigInterface $configInterface
45+
* @param \Magento\Framework\Image\Factory $imageFactory
46+
* @param \Magento\Framework\Filesystem $filesystem
4247
*/
4348
public function __construct(
4449
StoreManagerInterface $storeManager,
4550
UrlInterface $urlBuilder,
4651
Configuration $configuration,
4752
\Magento\Framework\View\ConfigInterface $configInterface,
53+
\Magento\Framework\Image\Factory $imageFactory,
54+
\Magento\Framework\Filesystem $filesystem,
4855
) {
4956
$this->storeManager = $storeManager;
5057
$this->urlBuilder = $urlBuilder;
5158
$this->_configuration = $configuration;
5259
$this->viewConfig = $configInterface;
60+
$this->imageFactory = $imageFactory;
61+
$this->mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::PUB);
5362
}
5463

5564
/**
@@ -67,6 +76,15 @@ public function getImageConfig()
6776
return $this->imageConfig;
6877
}
6978

79+
public function getSwatchCachePath($swatchType)
80+
{
81+
return self::SWATCH_MEDIA_PATH . '/' . $swatchType . '/';
82+
}
83+
84+
protected function getAbsolutePath($swatchType)
85+
{
86+
return $this->mediaDirectory->getAbsolutePath($this->getSwatchCachePath($swatchType));
87+
}
7088
/**
7189
* @param SwatchMediaHelper $subject
7290
* @param $result
@@ -80,14 +98,17 @@ public function afterGetSwatchAttributeImage(SwatchMediaHelper $subject, $result
8098
// Check if the file path is valid
8199
if (in_array($swatchType, $swatchTypes) && strpos($result, '/') !== false) {
82100

101+
$parsedUrl = parse_url($result, PHP_URL_PATH);
102+
$absolutePath = $this->mediaDirectory->getAbsolutePath() . $parsedUrl;
83103
$imageConfig = $this->getImageConfig();
84104

105+
$image = $this->imageFactory->create($absolutePath);
85106
$imageId = pathinfo($result, PATHINFO_FILENAME);
86107
$transformations = [
87108
'fetch_format' => $this->_configuration->getFetchFormat(),
88109
'quality' => $this->_configuration->getImageQuality(),
89-
'width' => $imageConfig[$swatchType]['width'] ?? null,
90-
'height' => $imageConfig[$swatchType]['height'] ?? null
110+
'width' => $image->getOriginalWidth() ?? $imageConfig[$swatchType]['width'],
111+
'height' => $image->getOriginalHeight() ?? $imageConfig[$swatchType]['height']
91112
];
92113
try {
93114
$image = Media::fromParams(

0 commit comments

Comments
 (0)