@@ -294,7 +294,7 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
294
294
}
295
295
296
296
/**
297
- * Resize image.
297
+ * Resize image if not already resized before
298
298
*
299
299
* @param array $imageParams
300
300
* @param string $originalImagePath
@@ -303,13 +303,48 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
303
303
private function resize (array $ imageParams , string $ originalImagePath , string $ originalImageName )
304
304
{
305
305
unset($ imageParams ['id ' ]);
306
- $ image = $ this ->makeImage ($ originalImagePath , $ imageParams );
307
306
$ imageAsset = $ this ->assertImageFactory ->create (
308
307
[
309
308
'miscParams ' => $ imageParams ,
310
309
'filePath ' => $ originalImageName ,
311
310
]
312
311
);
312
+ $ imageAssetPath = $ imageAsset ->getPath ();
313
+ $ usingDbAsStorage = $ this ->fileStorageDatabase ->checkDbUsage ();
314
+ $ mediaStorageFilename = $ this ->mediaDirectory ->getRelativePath ($ imageAssetPath );
315
+
316
+ $ alreadyResized = $ usingDbAsStorage ?
317
+ $ this ->fileStorageDatabase ->fileExists ($ mediaStorageFilename ) :
318
+ $ this ->mediaDirectory ->isFile ($ imageAssetPath );
319
+
320
+ if (!$ alreadyResized ) {
321
+ $ this ->generateResizedImage (
322
+ $ imageParams ,
323
+ $ originalImagePath ,
324
+ $ imageAssetPath ,
325
+ $ usingDbAsStorage ,
326
+ $ mediaStorageFilename
327
+ );
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Generate resized image
333
+ *
334
+ * @param array $imageParams
335
+ * @param string $originalImagePath
336
+ * @param string $imageAssetPath
337
+ * @param bool $usingDbAsStorage
338
+ * @param string $mediaStorageFilename
339
+ */
340
+ private function generateResizedImage (
341
+ array $ imageParams ,
342
+ string $ originalImagePath ,
343
+ string $ imageAssetPath ,
344
+ bool $ usingDbAsStorage ,
345
+ string $ mediaStorageFilename
346
+ ) {
347
+ $ image = $ this ->makeImage ($ originalImagePath , $ imageParams );
313
348
314
349
if ($ imageParams ['image_width ' ] !== null && $ imageParams ['image_height ' ] !== null ) {
315
350
$ image ->resize ($ imageParams ['image_width ' ], $ imageParams ['image_height ' ]);
@@ -335,11 +370,10 @@ private function resize(array $imageParams, string $originalImagePath, string $o
335
370
$ image ->watermark ($ this ->getWatermarkFilePath ($ imageParams ['watermark_file ' ]));
336
371
}
337
372
338
- $ image ->save ($ imageAsset -> getPath () );
373
+ $ image ->save ($ imageAssetPath );
339
374
340
- if ($ this ->fileStorageDatabase ->checkDbUsage ()) {
341
- $ mediastoragefilename = $ this ->mediaDirectory ->getRelativePath ($ imageAsset ->getPath ());
342
- $ this ->fileStorageDatabase ->saveFile ($ mediastoragefilename );
375
+ if ($ usingDbAsStorage ) {
376
+ $ this ->fileStorageDatabase ->saveFile ($ mediaStorageFilename );
343
377
}
344
378
}
345
379
0 commit comments