Skip to content

Commit 362eba8

Browse files
authored
[performance] MC-37936: Performance generator for images (#6295)
1 parent de8edcf commit 362eba8

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private function setPlaceholderImage(): void
255255
*/
256256
private function getOriginalImage(string $resizedImagePath): string
257257
{
258-
return preg_replace('|^.*((?:/[^/]+){3})$|', '$1', $resizedImagePath);
258+
return preg_replace('|^.*?((?:/([^/])/([^/])/\2\3)?/?[^/]+$)|', '$1', $resizedImagePath);
259259
}
260260

261261
/**

app/code/Magento/Swatches/etc/config.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
<show_swatch_tooltip>1</show_swatch_tooltip>
1515
</frontend>
1616
</catalog>
17+
<system>
18+
<media_storage_configuration>
19+
<allowed_resources>
20+
<swatches_folder>attribute</swatches_folder>
21+
</allowed_resources>
22+
</media_storage_configuration>
23+
</system>
1724
<general>
1825
<validator_data>
1926
<input_types>

setup/src/Magento/Setup/Fixtures/ImagesFixture.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\Exception\ValidatorException;
11+
use Magento\MediaStorage\Service\ImageResize;
1112
use Symfony\Component\Console\Output\OutputInterface;
1213

1314
/**
@@ -106,6 +107,10 @@ class ImagesFixture extends Fixture
106107
* @var array
107108
*/
108109
private $tableCache = [];
110+
/**
111+
* @var ImageResize
112+
*/
113+
private $imageResize;
109114

110115
/**
111116
* @param FixtureModel $fixtureModel
@@ -117,6 +122,7 @@ class ImagesFixture extends Fixture
117122
* @param \Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory
118123
* @param \Magento\Setup\Model\BatchInsertFactory $batchInsertFactory
119124
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
125+
* @param ImageResize $imageResize
120126
*/
121127
public function __construct(
122128
FixtureModel $fixtureModel,
@@ -127,7 +133,8 @@ public function __construct(
127133
\Magento\Eav\Model\AttributeRepository $attributeRepository,
128134
\Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory,
129135
\Magento\Setup\Model\BatchInsertFactory $batchInsertFactory,
130-
\Magento\Framework\EntityManager\MetadataPool $metadataPool
136+
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
137+
ImageResize $imageResize
131138
) {
132139
parent::__construct($fixtureModel);
133140

@@ -139,6 +146,7 @@ public function __construct(
139146
$this->expressionFactory = $expressionFactory;
140147
$this->batchInsertFactory = $batchInsertFactory;
141148
$this->metadataPool = $metadataPool;
149+
$this->imageResize = $imageResize;
142150
}
143151

144152
/**
@@ -147,9 +155,10 @@ public function __construct(
147155
*/
148156
public function execute()
149157
{
150-
if (!$this->checkIfImagesExists()) {
158+
if (!$this->checkIfImagesExists() && $this->getImagesToGenerate()) {
151159
$this->createImageEntities();
152160
$this->assignImagesToProducts();
161+
iterator_to_array($this->imageResize->resizeFromThemes(), false);
153162
}
154163
}
155164

setup/src/Magento/Setup/Fixtures/ImagesGenerator/ImagesGenerator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public function __construct(
3636

3737
/**
3838
* Generates image from $data and puts its to /tmp folder
39-
*
4039
* @param array $config
4140
* @return string $imagePath
41+
* @throws \Exception
4242
*/
4343
public function generate($config)
4444
{
@@ -70,9 +70,14 @@ public function generate($config)
7070
$relativePathToMedia = $mediaDirectory->getRelativePath($this->mediaConfig->getBaseTmpMediaPath());
7171
$mediaDirectory->create($relativePathToMedia);
7272

73-
$absolutePathToMedia = $mediaDirectory->getAbsolutePath($this->mediaConfig->getBaseTmpMediaPath());
74-
$imagePath = $absolutePathToMedia . DIRECTORY_SEPARATOR . $config['image-name'];
75-
imagejpeg($image, $imagePath, 100);
73+
$imagePath = $relativePathToMedia . DIRECTORY_SEPARATOR . $config['image-name'];
74+
$memory = fopen('php://memory', 'r+');
75+
if(!imagejpeg($image, $memory)) {
76+
throw new \Exception('Could not create picture ' . $imagePath);
77+
}
78+
$mediaDirectory->writeFile($imagePath, stream_get_contents($memory, -1, 0));
79+
fclose($memory);
80+
imagedestroy($image);
7681
// phpcs:enable
7782

7883
return $imagePath;

0 commit comments

Comments
 (0)