@@ -1852,12 +1852,14 @@ private function saveProductMediaGalleryPhase(
1852
1852
* must be unique in scope of one product.
1853
1853
*/
1854
1854
$ position = 0 ;
1855
+ $ imagesByHash = [];
1855
1856
foreach ($ rowImages as $ column => $ columnImages ) {
1856
1857
foreach ($ columnImages as $ columnImageKey => $ columnImage ) {
1857
1858
$ uploadedFile = $ this ->findImageByColumnImage (
1858
1859
$ productMediaPath ,
1859
1860
$ rowExistingImages ,
1860
- $ columnImage
1861
+ $ columnImage ,
1862
+ $ imagesByHash
1861
1863
);
1862
1864
if (!$ uploadedFile && !isset ($ uploadedImages [$ columnImage ])) {
1863
1865
$ uploadedFile = $ this ->uploadMediaFiles ($ columnImage );
@@ -3320,21 +3322,22 @@ private function getRowExistingStockItem(array $rowData): StockItemInterface
3320
3322
* @param string $productMediaPath
3321
3323
* @param array $images
3322
3324
* @param string $columnImage
3325
+ * @param array $imagesByHash
3323
3326
* @return string
3324
3327
*/
3325
- private function findImageByColumnImage (string $ productMediaPath , array &$ images , string $ columnImage ): string
3326
- {
3328
+ private function findImageByColumnImage (
3329
+ string $ productMediaPath ,
3330
+ array &$ images ,
3331
+ string $ columnImage ,
3332
+ array &$ imagesByHash
3333
+ ): string {
3327
3334
$ content = filter_var ($ columnImage , FILTER_VALIDATE_URL )
3328
3335
? $ this ->getRemoteFileContent ($ columnImage )
3329
3336
: $ this ->getFileContent ($ this ->joinFilePaths ($ this ->getUploader ()->getTmpDir (), $ columnImage ));
3330
3337
if (!$ content ) {
3331
3338
return '' ;
3332
3339
}
3333
- if ($ this ->shouldUseHash ($ images )) {
3334
- return $ this ->findImageByColumnImageUsingHash ($ productMediaPath , $ images , $ content );
3335
- } else {
3336
- return $ this ->findImageByColumnImageUsingContent ($ productMediaPath , $ images , $ content );
3337
- }
3340
+ return $ this ->findImageByColumnImageUsingHash ($ productMediaPath , $ images , $ content , $ imagesByHash );
3338
3341
}
3339
3342
3340
3343
/**
@@ -3343,11 +3346,19 @@ private function findImageByColumnImage(string $productMediaPath, array &$images
3343
3346
* @param string $productMediaPath
3344
3347
* @param array $images
3345
3348
* @param string $content
3349
+ * @param array $imagesByHash
3346
3350
* @return string
3347
3351
*/
3348
- private function findImageByColumnImageUsingHash (string $ productMediaPath , array &$ images , string $ content ): string
3349
- {
3352
+ private function findImageByColumnImageUsingHash (
3353
+ string $ productMediaPath ,
3354
+ array &$ images ,
3355
+ string $ content ,
3356
+ array &$ imagesByHash
3357
+ ): string {
3350
3358
$ hash = hash ($ this ->hashAlgorithm , $ content );
3359
+ if (!empty ($ imagesByHash [$ hash ])) {
3360
+ return $ imagesByHash [$ hash ];
3361
+ }
3351
3362
foreach ($ images as &$ image ) {
3352
3363
if (!isset ($ image ['hash ' ])) {
3353
3364
$ imageContent = $ this ->getFileContent ($ this ->joinFilePaths ($ productMediaPath , $ image ['value ' ]));
@@ -3356,6 +3367,7 @@ private function findImageByColumnImageUsingHash(string $productMediaPath, array
3356
3367
continue ;
3357
3368
}
3358
3369
$ image ['hash ' ] = hash ($ this ->hashAlgorithm , $ imageContent );
3370
+ $ imagesByHash [$ image ['hash ' ]] = $ image ['value ' ];
3359
3371
}
3360
3372
if (!empty ($ image ['hash ' ]) && $ image ['hash ' ] === $ hash ) {
3361
3373
return $ image ['value ' ];
@@ -3364,46 +3376,6 @@ private function findImageByColumnImageUsingHash(string $productMediaPath, array
3364
3376
return '' ;
3365
3377
}
3366
3378
3367
- /**
3368
- * Returns image that matches the provided image content using content
3369
- *
3370
- * @param string $productMediaPath
3371
- * @param array $images
3372
- * @param string $content
3373
- * @return string
3374
- */
3375
- private function findImageByColumnImageUsingContent (
3376
- string $ productMediaPath ,
3377
- array &$ images ,
3378
- string $ content
3379
- ): string {
3380
- foreach ($ images as &$ image ) {
3381
- if (!isset ($ image ['content ' ])) {
3382
- $ image ['content ' ] = $ this ->getFileContent (
3383
- $ this ->joinFilePaths ($ productMediaPath , $ image ['value ' ])
3384
- );
3385
- }
3386
- if ($ content === $ image ['content ' ]) {
3387
- return $ image ['value ' ];
3388
- }
3389
- }
3390
- return '' ;
3391
- }
3392
-
3393
- /**
3394
- * Returns true if we should use hash instead of just comparing content
3395
- *
3396
- * @param array $images
3397
- * @return bool
3398
- */
3399
- private function shouldUseHash (array $ images ): bool
3400
- {
3401
- if (count ($ images ) > 100 ) {
3402
- return true ;
3403
- }
3404
- return false ;
3405
- }
3406
-
3407
3379
/**
3408
3380
* Returns product media
3409
3381
*
0 commit comments