15
15
use Magento \Framework \App \ObjectManager ;
16
16
use Magento \Framework \Exception \NotFoundException ;
17
17
use Magento \Framework \Filesystem ;
18
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
18
19
use Magento \Framework \Image ;
19
20
use Magento \Framework \Image \Factory as ImageFactory ;
20
21
use Magento \Catalog \Model \Product \Media \ConfigInterface as MediaConfig ;
21
22
use Magento \Framework \App \State ;
22
23
use Magento \Framework \View \ConfigInterface as ViewConfig ;
23
- use \ Magento \Catalog \Model \ResourceModel \Product \Image as ProductImage ;
24
+ use Magento \Catalog \Model \ResourceModel \Product \Image as ProductImage ;
24
25
use Magento \Store \Model \StoreManagerInterface ;
25
26
use Magento \Theme \Model \Config \Customization as ThemeCustomizationConfig ;
26
- use Magento \Theme \Model \ResourceModel \Theme \Collection ;
27
+ use Magento \Theme \Model \ResourceModel \Theme \Collection as ThemeCollection ;
27
28
use Magento \Framework \App \Filesystem \DirectoryList ;
28
- use Magento \MediaStorage \Helper \File \Storage \Database ;
29
+ use Magento \MediaStorage \Helper \File \Storage \Database as FileStorageDatabase ;
29
30
use Magento \Theme \Model \Theme ;
30
31
31
32
/**
@@ -76,24 +77,20 @@ class ImageResize
76
77
private $ themeCustomizationConfig ;
77
78
78
79
/**
79
- * @var Collection
80
+ * @var ThemeCollection
80
81
*/
81
82
private $ themeCollection ;
82
83
83
84
/**
84
- * @var Filesystem
85
+ * @var WriteInterface
85
86
*/
86
87
private $ mediaDirectory ;
87
88
88
89
/**
89
- * @var Filesystem
90
- */
91
- private $ filesystem ;
92
-
93
- /**
94
- * @var Database
90
+ * @var FileStorageDatabase
95
91
*/
96
92
private $ fileStorageDatabase ;
93
+
97
94
/**
98
95
* @var StoreManagerInterface
99
96
*/
@@ -108,9 +105,9 @@ class ImageResize
108
105
* @param ViewConfig $viewConfig
109
106
* @param AssertImageFactory $assertImageFactory
110
107
* @param ThemeCustomizationConfig $themeCustomizationConfig
111
- * @param Collection $themeCollection
108
+ * @param ThemeCollection $themeCollection
112
109
* @param Filesystem $filesystem
113
- * @param Database $fileStorageDatabase
110
+ * @param FileStorageDatabase $fileStorageDatabase
114
111
* @param StoreManagerInterface $storeManager
115
112
* @throws \Magento\Framework\Exception\FileSystemException
116
113
* @internal param ProductImage $gallery
@@ -125,9 +122,9 @@ public function __construct(
125
122
ViewConfig $ viewConfig ,
126
123
AssertImageFactory $ assertImageFactory ,
127
124
ThemeCustomizationConfig $ themeCustomizationConfig ,
128
- Collection $ themeCollection ,
125
+ ThemeCollection $ themeCollection ,
129
126
Filesystem $ filesystem ,
130
- Database $ fileStorageDatabase = null ,
127
+ FileStorageDatabase $ fileStorageDatabase = null ,
131
128
StoreManagerInterface $ storeManager = null
132
129
) {
133
130
$ this ->appState = $ appState ;
@@ -140,9 +137,8 @@ public function __construct(
140
137
$ this ->themeCustomizationConfig = $ themeCustomizationConfig ;
141
138
$ this ->themeCollection = $ themeCollection ;
142
139
$ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
143
- $ this ->filesystem = $ filesystem ;
144
140
$ this ->fileStorageDatabase = $ fileStorageDatabase ?:
145
- ObjectManager::getInstance ()->get (Database ::class);
141
+ ObjectManager::getInstance ()->get (FileStorageDatabase ::class);
146
142
$ this ->storeManager = $ storeManager ?? ObjectManager::getInstance ()->get (StoreManagerInterface::class);
147
143
}
148
144
@@ -294,7 +290,7 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
294
290
}
295
291
296
292
/**
297
- * Resize image.
293
+ * Resize image if not already resized before
298
294
*
299
295
* @param array $imageParams
300
296
* @param string $originalImagePath
@@ -303,13 +299,48 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
303
299
private function resize (array $ imageParams , string $ originalImagePath , string $ originalImageName )
304
300
{
305
301
unset($ imageParams ['id ' ]);
306
- $ image = $ this ->makeImage ($ originalImagePath , $ imageParams );
307
302
$ imageAsset = $ this ->assertImageFactory ->create (
308
303
[
309
304
'miscParams ' => $ imageParams ,
310
305
'filePath ' => $ originalImageName ,
311
306
]
312
307
);
308
+ $ imageAssetPath = $ imageAsset ->getPath ();
309
+ $ usingDbAsStorage = $ this ->fileStorageDatabase ->checkDbUsage ();
310
+ $ mediaStorageFilename = $ this ->mediaDirectory ->getRelativePath ($ imageAssetPath );
311
+
312
+ $ alreadyResized = $ usingDbAsStorage ?
313
+ $ this ->fileStorageDatabase ->fileExists ($ mediaStorageFilename ) :
314
+ $ this ->mediaDirectory ->isFile ($ imageAssetPath );
315
+
316
+ if (!$ alreadyResized ) {
317
+ $ this ->generateResizedImage (
318
+ $ imageParams ,
319
+ $ originalImagePath ,
320
+ $ imageAssetPath ,
321
+ $ usingDbAsStorage ,
322
+ $ mediaStorageFilename
323
+ );
324
+ }
325
+ }
326
+
327
+ /**
328
+ * Generate resized image
329
+ *
330
+ * @param array $imageParams
331
+ * @param string $originalImagePath
332
+ * @param string $imageAssetPath
333
+ * @param bool $usingDbAsStorage
334
+ * @param string $mediaStorageFilename
335
+ */
336
+ private function generateResizedImage (
337
+ array $ imageParams ,
338
+ string $ originalImagePath ,
339
+ string $ imageAssetPath ,
340
+ bool $ usingDbAsStorage ,
341
+ string $ mediaStorageFilename
342
+ ) {
343
+ $ image = $ this ->makeImage ($ originalImagePath , $ imageParams );
313
344
314
345
if ($ imageParams ['image_width ' ] !== null && $ imageParams ['image_height ' ] !== null ) {
315
346
$ image ->resize ($ imageParams ['image_width ' ], $ imageParams ['image_height ' ]);
@@ -335,11 +366,10 @@ private function resize(array $imageParams, string $originalImagePath, string $o
335
366
$ image ->watermark ($ this ->getWatermarkFilePath ($ imageParams ['watermark_file ' ]));
336
367
}
337
368
338
- $ image ->save ($ imageAsset -> getPath () );
369
+ $ image ->save ($ imageAssetPath );
339
370
340
- if ($ this ->fileStorageDatabase ->checkDbUsage ()) {
341
- $ mediastoragefilename = $ this ->mediaDirectory ->getRelativePath ($ imageAsset ->getPath ());
342
- $ this ->fileStorageDatabase ->saveFile ($ mediastoragefilename );
371
+ if ($ usingDbAsStorage ) {
372
+ $ this ->fileStorageDatabase ->saveFile ($ mediaStorageFilename );
343
373
}
344
374
}
345
375
0 commit comments