@@ -1852,12 +1852,14 @@ private function saveProductMediaGalleryPhase(
18521852 * must be unique in scope of one product.
18531853 */
18541854 $ position = 0 ;
1855+ $ imagesByHash = [];
18551856 foreach ($ rowImages as $ column => $ columnImages ) {
18561857 foreach ($ columnImages as $ columnImageKey => $ columnImage ) {
18571858 $ uploadedFile = $ this ->findImageByColumnImage (
18581859 $ productMediaPath ,
18591860 $ rowExistingImages ,
1860- $ columnImage
1861+ $ columnImage ,
1862+ $ imagesByHash
18611863 );
18621864 if (!$ uploadedFile && !isset ($ uploadedImages [$ columnImage ])) {
18631865 $ uploadedFile = $ this ->uploadMediaFiles ($ columnImage );
@@ -3320,21 +3322,22 @@ private function getRowExistingStockItem(array $rowData): StockItemInterface
33203322 * @param string $productMediaPath
33213323 * @param array $images
33223324 * @param string $columnImage
3325+ * @param array $imagesByHash
33233326 * @return string
33243327 */
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 {
33273334 $ content = filter_var ($ columnImage , FILTER_VALIDATE_URL )
33283335 ? $ this ->getRemoteFileContent ($ columnImage )
33293336 : $ this ->getFileContent ($ this ->joinFilePaths ($ this ->getUploader ()->getTmpDir (), $ columnImage ));
33303337 if (!$ content ) {
33313338 return '' ;
33323339 }
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 );
33383341 }
33393342
33403343 /**
@@ -3343,11 +3346,19 @@ private function findImageByColumnImage(string $productMediaPath, array &$images
33433346 * @param string $productMediaPath
33443347 * @param array $images
33453348 * @param string $content
3349+ * @param array $imagesByHash
33463350 * @return string
33473351 */
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 {
33503358 $ hash = hash ($ this ->hashAlgorithm , $ content );
3359+ if (!empty ($ imagesByHash [$ hash ])) {
3360+ return $ imagesByHash [$ hash ];
3361+ }
33513362 foreach ($ images as &$ image ) {
33523363 if (!isset ($ image ['hash ' ])) {
33533364 $ imageContent = $ this ->getFileContent ($ this ->joinFilePaths ($ productMediaPath , $ image ['value ' ]));
@@ -3356,6 +3367,7 @@ private function findImageByColumnImageUsingHash(string $productMediaPath, array
33563367 continue ;
33573368 }
33583369 $ image ['hash ' ] = hash ($ this ->hashAlgorithm , $ imageContent );
3370+ $ imagesByHash [$ image ['hash ' ]] = $ image ['value ' ];
33593371 }
33603372 if (!empty ($ image ['hash ' ]) && $ image ['hash ' ] === $ hash ) {
33613373 return $ image ['value ' ];
@@ -3364,46 +3376,6 @@ private function findImageByColumnImageUsingHash(string $productMediaPath, array
33643376 return '' ;
33653377 }
33663378
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-
34073379 /**
34083380 * Returns product media
34093381 *
0 commit comments