Skip to content

Commit

Permalink
[performance] MC-37936: Performance generator for images (#6295)
Browse files Browse the repository at this point in the history
  • Loading branch information
duhon committed Nov 2, 2020
1 parent de8edcf commit 362eba8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/MediaStorage/App/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private function setPlaceholderImage(): void
*/
private function getOriginalImage(string $resizedImagePath): string
{
return preg_replace('|^.*((?:/[^/]+){3})$|', '$1', $resizedImagePath);
return preg_replace('|^.*?((?:/([^/])/([^/])/\2\3)?/?[^/]+$)|', '$1', $resizedImagePath);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/Swatches/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<show_swatch_tooltip>1</show_swatch_tooltip>
</frontend>
</catalog>
<system>
<media_storage_configuration>
<allowed_resources>
<swatches_folder>attribute</swatches_folder>
</allowed_resources>
</media_storage_configuration>
</system>
<general>
<validator_data>
<input_types>
Expand Down
13 changes: 11 additions & 2 deletions setup/src/Magento/Setup/Fixtures/ImagesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\ValidatorException;
use Magento\MediaStorage\Service\ImageResize;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -106,6 +107,10 @@ class ImagesFixture extends Fixture
* @var array
*/
private $tableCache = [];
/**
* @var ImageResize
*/
private $imageResize;

/**
* @param FixtureModel $fixtureModel
Expand All @@ -117,6 +122,7 @@ class ImagesFixture extends Fixture
* @param \Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory
* @param \Magento\Setup\Model\BatchInsertFactory $batchInsertFactory
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param ImageResize $imageResize
*/
public function __construct(
FixtureModel $fixtureModel,
Expand All @@ -127,7 +133,8 @@ public function __construct(
\Magento\Eav\Model\AttributeRepository $attributeRepository,
\Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory,
\Magento\Setup\Model\BatchInsertFactory $batchInsertFactory,
\Magento\Framework\EntityManager\MetadataPool $metadataPool
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
ImageResize $imageResize
) {
parent::__construct($fixtureModel);

Expand All @@ -139,6 +146,7 @@ public function __construct(
$this->expressionFactory = $expressionFactory;
$this->batchInsertFactory = $batchInsertFactory;
$this->metadataPool = $metadataPool;
$this->imageResize = $imageResize;
}

/**
Expand All @@ -147,9 +155,10 @@ public function __construct(
*/
public function execute()
{
if (!$this->checkIfImagesExists()) {
if (!$this->checkIfImagesExists() && $this->getImagesToGenerate()) {
$this->createImageEntities();
$this->assignImagesToProducts();
iterator_to_array($this->imageResize->resizeFromThemes(), false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function __construct(

/**
* Generates image from $data and puts its to /tmp folder
*
* @param array $config
* @return string $imagePath
* @throws \Exception
*/
public function generate($config)
{
Expand Down Expand Up @@ -70,9 +70,14 @@ public function generate($config)
$relativePathToMedia = $mediaDirectory->getRelativePath($this->mediaConfig->getBaseTmpMediaPath());
$mediaDirectory->create($relativePathToMedia);

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

return $imagePath;
Expand Down

0 comments on commit 362eba8

Please sign in to comment.