Skip to content

Commit 6a694a9

Browse files
author
Aliaksei Yakimovich2
committed
MC-17218: php bin/magento catalog:image:resize error if image is missing
- Fixed an issue with resize command;
1 parent 1656409 commit 6a694a9

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

app/code/Magento/MediaStorage/Console/Command/ImagesResizeCommand.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\Console\Helper\ProgressBar;
1616
use Magento\Framework\ObjectManagerInterface;
1717

18+
/**
19+
* Class for catalog image resizing via CLI
20+
*/
1821
class ImagesResizeCommand extends \Symfony\Component\Console\Command\Command
1922
{
2023
/**
@@ -49,7 +52,7 @@ public function __construct(
4952
}
5053

5154
/**
52-
* {@inheritdoc}
55+
* @inheritdoc
5356
*/
5457
protected function configure()
5558
{
@@ -58,19 +61,23 @@ protected function configure()
5861
}
5962

6063
/**
61-
* {@inheritdoc}
64+
* @inheritdoc
6265
*/
6366
protected function execute(InputInterface $input, OutputInterface $output)
6467
{
6568
try {
69+
$errors = [];
6670
$this->appState->setAreaCode(Area::AREA_GLOBAL);
6771
$generator = $this->resize->resizeFromThemes();
6872

6973
/** @var ProgressBar $progress */
70-
$progress = $this->objectManager->create(ProgressBar::class, [
71-
'output' => $output,
72-
'max' => $generator->current()
73-
]);
74+
$progress = $this->objectManager->create(
75+
ProgressBar::class,
76+
[
77+
'output' => $output,
78+
'max' => $generator->current()
79+
]
80+
);
7481
$progress->setFormat(
7582
"%current%/%max% [%bar%] %percent:3s%% %elapsed% %memory:6s% \t| <info>%message%</info>"
7683
);
@@ -79,8 +86,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
7986
$progress->setOverwrite(false);
8087
}
8188

89+
// phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
8290
for (; $generator->valid(); $generator->next()) {
83-
$progress->setMessage($generator->key());
91+
$resizeInfo = $generator->key();
92+
$error = $resizeInfo['error'];
93+
$filename = $resizeInfo['filename'];
94+
95+
if ($error !== '') {
96+
$errors[$filename] = $error;
97+
}
98+
99+
$progress->setMessage($filename);
84100
$progress->advance();
85101
}
86102
} catch (\Exception $e) {
@@ -90,6 +106,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
90106
}
91107

92108
$output->write(PHP_EOL);
93-
$output->writeln("<info>Product images resized successfully</info>");
109+
if (count($errors)) {
110+
$output->writeln("<info>Product images resized with errors:</info>");
111+
foreach ($errors as $error) {
112+
$output->writeln("<error>{$error}</error>");
113+
}
114+
} else {
115+
$output->writeln("<info>Product images resized successfully</info>");
116+
}
94117
}
95118
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,21 @@ public function resizeFromThemes(array $themes = null): \Generator
161161
$viewImages = $this->getViewImages($themes ?? $this->getThemesInUse());
162162

163163
foreach ($productImages as $image) {
164+
$error = '';
164165
$originalImageName = $image['filepath'];
165166
$originalImagePath = $this->mediaDirectory->getAbsolutePath(
166167
$this->imageConfig->getMediaPath($originalImageName)
167168
);
168-
foreach ($viewImages as $viewImage) {
169-
$this->resize($viewImage, $originalImagePath, $originalImageName);
169+
170+
if ($this->mediaDirectory->isFile($originalImagePath)) {
171+
foreach ($viewImages as $viewImage) {
172+
$this->resize($viewImage, $originalImagePath, $originalImageName);
173+
}
174+
} else {
175+
$error = __('Cannot resize image "%1" - original image not found', $originalImagePath);
170176
}
171-
yield $originalImageName => $count;
177+
178+
yield ['filename' => $originalImageName, 'error' => $error] => $count;
172179
}
173180
}
174181

@@ -202,10 +209,12 @@ private function getViewImages(array $themes): array
202209
$viewImages = [];
203210
/** @var \Magento\Theme\Model\Theme $theme */
204211
foreach ($themes as $theme) {
205-
$config = $this->viewConfig->getViewConfig([
206-
'area' => Area::AREA_FRONTEND,
207-
'themeModel' => $theme,
208-
]);
212+
$config = $this->viewConfig->getViewConfig(
213+
[
214+
'area' => Area::AREA_FRONTEND,
215+
'themeModel' => $theme,
216+
]
217+
);
209218
$images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE);
210219
foreach ($images as $imageId => $imageData) {
211220
$uniqIndex = $this->getUniqueImageIndex($imageData);
@@ -226,7 +235,7 @@ private function getUniqueImageIndex(array $imageData): string
226235
{
227236
ksort($imageData);
228237
unset($imageData['type']);
229-
return md5(json_encode($imageData));
238+
return hash('sha256', json_encode($imageData));
230239
}
231240

232241
/**

0 commit comments

Comments
 (0)