9
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
10
use Magento \Framework \App \ObjectManager ;
11
11
use Magento \Framework \Exception \LocalizedException ;
12
+ use Magento \Backend \Model \Image \UploadResizeConfigInterface ;
13
+ use Psr \Log \LoggerInterface ;
12
14
13
15
/**
14
16
* The product gallery upload controller
17
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
18
*/
16
19
class Upload extends \Magento \Backend \App \Action implements HttpPostActionInterface
17
20
{
@@ -52,19 +55,32 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf
52
55
*/
53
56
private $ productMediaConfig ;
54
57
58
+ /**
59
+ * @var UploadResizeConfigInterface
60
+ */
61
+ private $ imageUploadConfig ;
62
+
63
+ /**
64
+ * @var LoggerInterface
65
+ */
66
+ private $ _logger ;
67
+
55
68
/**
56
69
* @param \Magento\Backend\App\Action\Context $context
57
70
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
58
- * @param \Magento\Framework\Image\AdapterFactory $adapterFactory
59
- * @param \Magento\Framework\Filesystem $filesystem
60
- * @param \Magento\Catalog\Model\Product\Media\Config $productMediaConfig
71
+ * @param \Magento\Framework\Image\AdapterFactory|null $adapterFactory
72
+ * @param \Magento\Framework\Filesystem|null $filesystem
73
+ * @param \Magento\Catalog\Model\Product\Media\Config|null $productMediaConfig
74
+ * @param UploadResizeConfigInterface|null $imageUploadConfig
75
+ * @throws \Magento\Framework\Exception\FileSystemException
61
76
*/
62
77
public function __construct (
63
78
\Magento \Backend \App \Action \Context $ context ,
64
79
\Magento \Framework \Controller \Result \RawFactory $ resultRawFactory ,
65
- ?\Magento \Framework \Image \AdapterFactory $ adapterFactory = null ,
66
- ?\Magento \Framework \Filesystem $ filesystem = null ,
67
- ?\Magento \Catalog \Model \Product \Media \Config $ productMediaConfig = null
80
+ \Magento \Framework \Image \AdapterFactory $ adapterFactory = null ,
81
+ \Magento \Framework \Filesystem $ filesystem = null ,
82
+ \Magento \Catalog \Model \Product \Media \Config $ productMediaConfig = null ,
83
+ UploadResizeConfigInterface $ imageUploadConfig = null
68
84
) {
69
85
parent ::__construct ($ context );
70
86
$ this ->resultRawFactory = $ resultRawFactory ;
@@ -74,6 +90,8 @@ public function __construct(
74
90
->get (\Magento \Framework \Filesystem::class);
75
91
$ this ->productMediaConfig = $ productMediaConfig ?: ObjectManager::getInstance ()
76
92
->get (\Magento \Catalog \Model \Product \Media \Config::class);
93
+ $ this ->imageUploadConfig = $ imageUploadConfig
94
+ ?: ObjectManager::getInstance ()->get (UploadResizeConfigInterface::class);
77
95
}
78
96
79
97
/**
@@ -97,6 +115,12 @@ public function execute()
97
115
$ result = $ uploader ->save (
98
116
$ mediaDirectory ->getAbsolutePath ($ this ->productMediaConfig ->getBaseTmpMediaPath ())
99
117
);
118
+ // Resize the image if needed
119
+ $ this ->processImage (
120
+ $ imageAdapter ,
121
+ $ mediaDirectory ->getAbsolutePath ($ this ->productMediaConfig ->getBaseTmpMediaPath ()),
122
+ $ result ['file ' ]
123
+ );
100
124
$ this ->_eventManager ->dispatch (
101
125
'catalog_product_gallery_upload_image_after ' ,
102
126
['result ' => $ result , 'action ' => $ this ]
@@ -124,6 +148,48 @@ public function execute()
124
148
return $ response ;
125
149
}
126
150
151
+ /**
152
+ * Resize the image
153
+ *
154
+ * @param \Magento\Framework\Image\AdapterFactory $imageAdapter
155
+ * @param string $path
156
+ * @param string $file
157
+ * @return bool
158
+ */
159
+ private function processImage ($ imageAdapter , $ path , $ file ): bool
160
+ {
161
+ try {
162
+ $ filePath = $ path . DIRECTORY_SEPARATOR . $ file ;
163
+
164
+ // Open the image file
165
+ $ imageAdapter ->open ($ filePath );
166
+
167
+ // Get current dimensions
168
+ $ imageWidth = $ imageAdapter ->getOriginalWidth ();
169
+ $ imageHeight = $ imageAdapter ->getOriginalHeight ();
170
+
171
+ // Fetch resizing configurations
172
+ $ maxWidth = $ this ->imageUploadConfig ->getMaxWidth ();
173
+ $ maxHeight = $ this ->imageUploadConfig ->getMaxHeight ();
174
+
175
+ // Check if resizing is necessary
176
+ if ($ this ->imageUploadConfig ->isResizeEnabled ()
177
+ && ($ imageWidth > $ maxWidth || $ imageHeight > $ maxHeight )) {
178
+ // Maintain aspect ratio and resize
179
+ $ imageAdapter ->keepAspectRatio (true );
180
+ $ imageAdapter ->resize ($ maxWidth , $ maxHeight );
181
+
182
+ // Save the resized image
183
+ $ imageAdapter ->save ($ filePath );
184
+ }
185
+
186
+ return true ;
187
+ } catch (\Exception $ e ) {
188
+ $ this ->_logger ->error ($ e ->getMessage ());
189
+ return false ;
190
+ }
191
+ }
192
+
127
193
/**
128
194
* Get the set of allowed file extensions.
129
195
*
0 commit comments