2
2
namespace Cloudinary \Cloudinary \Plugin ;
3
3
4
4
5
+ use Magento \Framework \App \Filesystem \DirectoryList ;
5
6
use Magento \Framework \Exception \LocalizedException ;
6
7
use Magento \Swatches \Helper \Media as SwatchMediaHelper ;
7
8
use Magento \Framework \UrlInterface ;
10
11
use Cloudinary \Cloudinary \Model \Configuration ;
11
12
class SwatchImageUrlPlugin
12
13
{
14
+ public const SWATCH_MEDIA_PATH = 'attribute/swatch ' ;
13
15
/**
14
16
* @var StoreManagerInterface
15
17
*/
@@ -33,23 +35,30 @@ class SwatchImageUrlPlugin
33
35
*/
34
36
protected $ imageConfig ;
35
37
38
+ protected $ imageFactory ;
39
+
36
40
/**
37
- * Constructor
38
41
* @param StoreManagerInterface $storeManager
39
42
* @param UrlInterface $urlBuilder
40
43
* @param Configuration $configuration
41
44
* @param \Magento\Framework\View\ConfigInterface $configInterface
45
+ * @param \Magento\Framework\Image\Factory $imageFactory
46
+ * @param \Magento\Framework\Filesystem $filesystem
42
47
*/
43
48
public function __construct (
44
49
StoreManagerInterface $ storeManager ,
45
50
UrlInterface $ urlBuilder ,
46
51
Configuration $ configuration ,
47
52
\Magento \Framework \View \ConfigInterface $ configInterface ,
53
+ \Magento \Framework \Image \Factory $ imageFactory ,
54
+ \Magento \Framework \Filesystem $ filesystem ,
48
55
) {
49
56
$ this ->storeManager = $ storeManager ;
50
57
$ this ->urlBuilder = $ urlBuilder ;
51
58
$ this ->_configuration = $ configuration ;
52
59
$ this ->viewConfig = $ configInterface ;
60
+ $ this ->imageFactory = $ imageFactory ;
61
+ $ this ->mediaDirectory = $ filesystem ->getDirectoryRead (DirectoryList::PUB );
53
62
}
54
63
55
64
/**
@@ -67,6 +76,15 @@ public function getImageConfig()
67
76
return $ this ->imageConfig ;
68
77
}
69
78
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
+ }
70
88
/**
71
89
* @param SwatchMediaHelper $subject
72
90
* @param $result
@@ -80,14 +98,17 @@ public function afterGetSwatchAttributeImage(SwatchMediaHelper $subject, $result
80
98
// Check if the file path is valid
81
99
if (in_array ($ swatchType , $ swatchTypes ) && strpos ($ result , '/ ' ) !== false ) {
82
100
101
+ $ parsedUrl = parse_url ($ result , PHP_URL_PATH );
102
+ $ absolutePath = $ this ->mediaDirectory ->getAbsolutePath () . $ parsedUrl ;
83
103
$ imageConfig = $ this ->getImageConfig ();
84
104
105
+ $ image = $ this ->imageFactory ->create ($ absolutePath );
85
106
$ imageId = pathinfo ($ result , PATHINFO_FILENAME );
86
107
$ transformations = [
87
108
'fetch_format ' => $ this ->_configuration ->getFetchFormat (),
88
109
'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 ' ]
91
112
];
92
113
try {
93
114
$ image = Media::fromParams (
0 commit comments