Skip to content

Commit 65c602f

Browse files
committed
ACP2E-3927: Dynamic image generation generate large number of images
1 parent 6e9c180 commit 65c602f

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
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: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Magento\MediaStorage\Model\File\Storage\Response;
2727
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
2828
use Magento\MediaStorage\Service\ImageResize;
29-
use Magento\Store\Model\StoreManagerInterface;
3029

3130
/**
3231
* The class resize original images
@@ -106,11 +105,6 @@ class Media implements AppInterface
106105
*/
107106
private $mediaUrlFormat;
108107

109-
/**
110-
* @var StoreManagerInterface
111-
*/
112-
private $storeManager;
113-
114108
/**
115109
* @param ConfigFactory $configFactory
116110
* @param SynchronizationFactory $syncFactory
@@ -125,7 +119,6 @@ class Media implements AppInterface
125119
* @param ImageResize $imageResize
126120
* @param File $file
127121
* @param CatalogMediaConfig $catalogMediaConfig
128-
* @param StoreManagerInterface|null $storeManager
129122
* @throws \Magento\Framework\Exception\FileSystemException
130123
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131124
*/
@@ -142,8 +135,7 @@ public function __construct(
142135
State $state,
143136
ImageResize $imageResize,
144137
File $file,
145-
?CatalogMediaConfig $catalogMediaConfig = null,
146-
?StoreManagerInterface $storeManager = null,
138+
?CatalogMediaConfig $catalogMediaConfig = null
147139
) {
148140
$this->response = $response;
149141
$this->isAllowed = $isAllowed;
@@ -170,7 +162,6 @@ public function __construct(
170162

171163
$catalogMediaConfig = $catalogMediaConfig ?: App\ObjectManager::getInstance()->get(CatalogMediaConfig::class);
172164
$this->mediaUrlFormat = $catalogMediaConfig->getMediaUrlFormat();
173-
$this->storeManager = $storeManager ?? App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
174165
}
175166

176167
/**
@@ -228,10 +219,7 @@ private function createLocalCopy(): void
228219
}
229220

230221
if ($this->mediaUrlFormat === CatalogMediaConfig::HASH) {
231-
$this->imageResize->resizeFromImageName(
232-
$this->getOriginalImage($this->relativeFileName),
233-
[(int) $this->storeManager->getStore()->getWebsiteId()]
234-
);
222+
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName), true);
235223
if (!$this->directoryPub->isReadable($this->relativeFileName)) {
236224
$synchronizer->synchronize($this->relativeFileName);
237225
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ public function __construct(
151151
* Create resized images of different sizes from an original image.
152152
*
153153
* @param string $originalImageName
154-
* @param array $websiteIds
154+
* @param bool $skipHiddenImages
155155
* @throws NotFoundException
156156
*/
157-
public function resizeFromImageName(string $originalImageName, array $websiteIds = [])
157+
public function resizeFromImageName(string $originalImageName, bool $skipHiddenImages = false)
158158
{
159159
$mediastoragefilename = $this->imageConfig->getMediaPath($originalImageName);
160160
$originalImagePath = $this->mediaDirectory->getAbsolutePath($mediastoragefilename);
@@ -170,7 +170,8 @@ public function resizeFromImageName(string $originalImageName, array $websiteIds
170170
}
171171

172172
$viewImages = $this->getViewImages($this->getThemesInUse());
173-
if ($websiteIds) {
173+
if ($skipHiddenImages) {
174+
$websiteIds = $this->productImage->getRelatedWebsiteIds($originalImageName);
174175
$viewImages = array_filter(
175176
$viewImages,
176177
fn (string $index) => array_intersect($websiteIds, $this->paramsWebsitesMap[$index]),

app/code/Magento/MediaStorage/Test/Unit/App/MediaTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
/**
3-
* Copyright 2013 Adobe
4-
* All Rights Reserved.
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
77

8+
89
namespace Magento\MediaStorage\Test\Unit\App;
910

1011
use Exception;
@@ -26,7 +27,6 @@
2627
use Magento\MediaStorage\Model\File\Storage\Synchronization;
2728
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
2829
use Magento\MediaStorage\Service\ImageResize;
29-
use Magento\Store\Model\StoreManagerInterface;
3030
use PHPUnit\Framework\MockObject\MockObject;
3131
use PHPUnit\Framework\TestCase;
3232

@@ -81,11 +81,6 @@ class MediaTest extends TestCase
8181
*/
8282
private $directoryMediaMock;
8383

84-
/**
85-
* @var StoreManagerInterface|MockObject
86-
*/
87-
private $storeManagerMock;
88-
8984
/**
9085
* @var Read|MockObject
9186
*/
@@ -98,13 +93,12 @@ protected function setUp(): void
9893
{
9994
$this->configMock = $this->createMock(Config::class);
10095
$this->sync = $this->createMock(Synchronization::class);
101-
$this->configFactoryMock = $this->createMock(ConfigFactory::class);
96+
$this->configFactoryMock = $this->createPartialMock(ConfigFactory::class, ['create']);
10297
$this->responseMock = $this->createMock(Response::class);
103-
$this->syncFactoryMock = $this->createMock(SynchronizationFactory::class);
98+
$this->syncFactoryMock = $this->createPartialMock(SynchronizationFactory::class, ['create']);
10499
$this->filesystemMock = $this->createMock(Filesystem::class);
105-
$this->directoryPubMock = $this->createMock(WriteInterface::class);
106-
$this->directoryMediaMock = $this->createMock(WriteInterface::class);
107-
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
100+
$this->directoryPubMock = $this->getMockForAbstractClass(WriteInterface::class);
101+
$this->directoryMediaMock = $this->getMockForAbstractClass(WriteInterface::class);
108102

109103
$this->configFactoryMock->method('create')
110104
->willReturn($this->configMock);
@@ -294,8 +288,7 @@ protected function createMediaModel(bool $isAllowed = true): Media
294288
$this->createMock(State::class),
295289
$this->createMock(ImageResize::class),
296290
$driverFile,
297-
$this->createMock(CatalogMediaConfig::class),
298-
$this->storeManagerMock,
291+
$this->createMock(CatalogMediaConfig::class)
299292
);
300293
}
301294
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,13 @@ public function testResizeFromImageNameWithAssignedWebsite()
463463
->willReturn(
464464
['0' => []]
465465
);
466+
$this->productImageMock->expects($this->once())->method('getRelatedWebsiteIds')->willReturn([2]);
466467
$imageMock = $this->createMock(Image::class);
467468
$this->imageFactoryMock->expects($this->once())
468469
->method('create')
469470
->willReturn($imageMock);
470471

471-
$this->service->resizeFromImageName($this->testfilename, [2]);
472+
$this->service->resizeFromImageName($this->testfilename, true);
472473
}
473474

474475
public function testResizeFromImageNameWithNotAssignedWebsite()
@@ -490,10 +491,11 @@ public function testResizeFromImageNameWithNotAssignedWebsite()
490491
->willReturn(
491492
['0' => []]
492493
);
494+
$this->productImageMock->expects($this->once())->method('getRelatedWebsiteIds')->willReturn([3]);
493495
$this->imageFactoryMock->expects($this->never())
494496
->method('create');
495497

496-
$this->service->resizeFromImageName($this->testfilename, [3]);
498+
$this->service->resizeFromImageName($this->testfilename, true);
497499
}
498500

499501
public function testSkipResizingAlreadyResizedImageOnDisk()

0 commit comments

Comments
 (0)