Skip to content

Commit af2ebc9

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-3927' into PR_2025_07_10_chittima
2 parents a947b30 + 238e4ac commit af2ebc9

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,22 @@ private function getUsedImagesSelect(): Select
184184

185185
return $select;
186186
}
187+
188+
/**
189+
* Get related website IDs for a given image file path.
190+
*
191+
* @param string $filepath
192+
* @return array
193+
*/
194+
public function getRelatedWebsiteIds(string $filepath): array
195+
{
196+
$select = $this->getUsedImagesSelect();
197+
$select->where('images.value = ?', $filepath);
198+
$result = array_map(
199+
fn ($ids) => array_map('intval', explode(',', $ids)),
200+
$this->connection->fetchPairs($select)
201+
);
202+
203+
return $result[$filepath] ?? [];
204+
}
187205
}

app/code/Magento/MediaStorage/App/Media.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\MediaStorage\App;
@@ -24,7 +24,6 @@
2424
use Magento\MediaStorage\Model\File\Storage\Config;
2525
use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
2626
use Magento\MediaStorage\Model\File\Storage\Response;
27-
use Magento\MediaStorage\Model\File\Storage\Synchronization;
2827
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
2928
use Magento\MediaStorage\Service\ImageResize;
3029

@@ -220,7 +219,7 @@ private function createLocalCopy(): void
220219
}
221220

222221
if ($this->mediaUrlFormat === CatalogMediaConfig::HASH) {
223-
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
222+
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName), true);
224223
if (!$this->directoryPub->isReadable($this->relativeFileName)) {
225224
$synchronizer->synchronize($this->relativeFileName);
226225
}

app/code/Magento/MediaStorage/Service/ImageResize.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ public function __construct(
151151
* Create resized images of different sizes from an original image.
152152
*
153153
* @param string $originalImageName
154+
* @param bool $skipHiddenImages
154155
* @throws NotFoundException
155156
*/
156-
public function resizeFromImageName(string $originalImageName)
157+
public function resizeFromImageName(string $originalImageName, bool $skipHiddenImages = false)
157158
{
158159
$mediastoragefilename = $this->imageConfig->getMediaPath($originalImageName);
159160
$originalImagePath = $this->mediaDirectory->getAbsolutePath($mediastoragefilename);
@@ -167,7 +168,17 @@ public function resizeFromImageName(string $originalImageName)
167168
if (!$this->mediaDirectory->isFile($originalImagePath)) {
168169
throw new NotFoundException(__('Cannot resize image "%1" - original image not found', $originalImagePath));
169170
}
170-
foreach ($this->getViewImages($this->getThemesInUse()) as $viewImage) {
171+
172+
$viewImages = $this->getViewImages($this->getThemesInUse());
173+
if ($skipHiddenImages) {
174+
$websiteIds = $this->productImage->getRelatedWebsiteIds($originalImageName);
175+
$viewImages = array_filter(
176+
$viewImages,
177+
fn (string $index) => array_intersect($websiteIds, $this->paramsWebsitesMap[$index]),
178+
ARRAY_FILTER_USE_KEY
179+
);
180+
}
181+
foreach ($viewImages as $viewImage) {
171182
$this->resize($viewImage, $originalImagePath, $originalImageName);
172183
}
173184
}

app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,60 @@ public function testResizeFromImageNameMediaStorageDatabase()
444444
$this->service->resizeFromImageName($this->testfilename);
445445
}
446446

447+
public function testResizeFromImageNameWithAssignedWebsite()
448+
{
449+
$this->databaseMock->expects($this->atLeastOnce())
450+
->method('checkDbUsage')
451+
->willReturn(false);
452+
$this->mediaDirectoryMock->expects($this->exactly(2))
453+
->method('isFile')
454+
->with($this->testfilepath)
455+
->willReturnOnConsecutiveCalls(true, false);
456+
$this->themeCollectionMock->expects($this->once())
457+
->method('loadRegisteredThemes')
458+
->willReturn(
459+
[ new DataObject(['id' => '0']) ]
460+
);
461+
$this->themeCustomizationConfigMock->expects($this->once())
462+
->method('getStoresByThemes')
463+
->willReturn(
464+
['0' => []]
465+
);
466+
$this->productImageMock->expects($this->once())->method('getRelatedWebsiteIds')->willReturn([2]);
467+
$imageMock = $this->createMock(Image::class);
468+
$this->imageFactoryMock->expects($this->once())
469+
->method('create')
470+
->willReturn($imageMock);
471+
472+
$this->service->resizeFromImageName($this->testfilename, true);
473+
}
474+
475+
public function testResizeFromImageNameWithNotAssignedWebsite()
476+
{
477+
$this->databaseMock->expects($this->atLeastOnce())
478+
->method('checkDbUsage')
479+
->willReturn(false);
480+
$this->mediaDirectoryMock->expects($this->once())
481+
->method('isFile')
482+
->with($this->testfilepath)
483+
->willReturn(true);
484+
$this->themeCollectionMock->expects($this->once())
485+
->method('loadRegisteredThemes')
486+
->willReturn(
487+
[ new DataObject(['id' => '0']) ]
488+
);
489+
$this->themeCustomizationConfigMock->expects($this->once())
490+
->method('getStoresByThemes')
491+
->willReturn(
492+
['0' => []]
493+
);
494+
$this->productImageMock->expects($this->once())->method('getRelatedWebsiteIds')->willReturn([3]);
495+
$this->imageFactoryMock->expects($this->never())
496+
->method('create');
497+
498+
$this->service->resizeFromImageName($this->testfilename, true);
499+
}
500+
447501
public function testSkipResizingAlreadyResizedImageOnDisk()
448502
{
449503
$this->databaseMock->expects($this->atLeastOnce())

0 commit comments

Comments
 (0)