Skip to content

Commit c4a103e

Browse files
MAGETWO-85545: 8204: catalog:images:resize = getimagesize(): Read error! #1000
2 parents b264180 + 6b027a5 commit c4a103e

File tree

4 files changed

+142
-11
lines changed

4 files changed

+142
-11
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Console\Command;
77

8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
811
class ImagesResizeCommand extends \Symfony\Component\Console\Command\Command
912
{
1013
/**
@@ -58,10 +61,8 @@ protected function configure()
5861
/**
5962
* {@inheritdoc}
6063
*/
61-
protected function execute(
62-
\Symfony\Component\Console\Input\InputInterface $input,
63-
\Symfony\Component\Console\Output\OutputInterface $output
64-
) {
64+
protected function execute(InputInterface $input, OutputInterface $output)
65+
{
6566
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
6667

6768
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
@@ -73,6 +74,7 @@ protected function execute(
7374
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
7475
}
7576

77+
$errorMessage = '';
7678
try {
7779
foreach ($productIds as $productId) {
7880
try {
@@ -82,9 +84,13 @@ protected function execute(
8284
continue;
8385
}
8486

85-
/** @var \Magento\Catalog\Model\Product\Image\Cache $imageCache */
86-
$imageCache = $this->imageCacheFactory->create();
87-
$imageCache->generate($product);
87+
try {
88+
/** @var \Magento\Catalog\Model\Product\Image\Cache $imageCache */
89+
$imageCache = $this->imageCacheFactory->create();
90+
$imageCache->generate($product);
91+
} catch (\Magento\Framework\Exception\RuntimeException $e) {
92+
$errorMessage = $e->getMessage();
93+
}
8894

8995
$output->write(".");
9096
}
@@ -95,6 +101,12 @@ protected function execute(
95101
}
96102

97103
$output->write("\n");
98-
$output->writeln("<info>Product images resized successfully</info>");
104+
$output->writeln("<info>Product images resized successfully.</info>");
105+
106+
if ($errorMessage !== '') {
107+
$output->writeln("<comment>{$errorMessage}</comment>");
108+
}
109+
110+
return 0;
99111
}
100112
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
1111
use Magento\Framework\App\Area;
1212
use Magento\Framework\View\ConfigInterface;
13+
use Psr\Log\LoggerInterface;
1314

1415
class Cache
1516
{
@@ -33,19 +34,29 @@ class Cache
3334
*/
3435
protected $data = [];
3536

37+
/**
38+
* Logger.
39+
*
40+
* @var LoggerInterface
41+
*/
42+
private $logger;
43+
3644
/**
3745
* @param ConfigInterface $viewConfig
3846
* @param ThemeCollection $themeCollection
3947
* @param ImageHelper $imageHelper
48+
* @param LoggerInterface $logger
4049
*/
4150
public function __construct(
4251
ConfigInterface $viewConfig,
4352
ThemeCollection $themeCollection,
44-
ImageHelper $imageHelper
53+
ImageHelper $imageHelper,
54+
LoggerInterface $logger = null
4555
) {
4656
$this->viewConfig = $viewConfig;
4757
$this->themeCollection = $themeCollection;
4858
$this->imageHelper = $imageHelper;
59+
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class);
4960
}
5061

5162
/**
@@ -74,21 +85,37 @@ protected function getData()
7485
}
7586

7687
/**
77-
* Resize product images and save results to image cache
88+
* Resize product images and save results to image cache.
7889
*
7990
* @param Product $product
91+
*
8092
* @return $this
93+
* @throws \Exception
8194
*/
8295
public function generate(Product $product)
8396
{
97+
$isException = false;
8498
$galleryImages = $product->getMediaGalleryImages();
8599
if ($galleryImages) {
86100
foreach ($galleryImages as $image) {
87101
foreach ($this->getData() as $imageData) {
88-
$this->processImageData($product, $imageData, $image->getFile());
102+
try {
103+
$this->processImageData($product, $imageData, $image->getFile());
104+
} catch (\Exception $e) {
105+
$isException = true;
106+
$this->logger->error($e->getMessage());
107+
$this->logger->error(__('The image could not be resized: ') . $image->getPath());
108+
}
89109
}
90110
}
91111
}
112+
113+
if ($isException === true) {
114+
throw new \Magento\Framework\Exception\RuntimeException(
115+
__('Some images could not be resized. See log file for details.')
116+
);
117+
}
118+
92119
return $this;
93120
}
94121

app/code/Magento/Catalog/Test/Unit/Console/Command/ImagesResizeCommandTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,44 @@ protected function prepareImageCache()
208208
->method('create')
209209
->willReturn($this->imageCache);
210210
}
211+
212+
public function testExecuteWithExceptionInGenerate()
213+
{
214+
$productsIds = [1, 2];
215+
216+
$this->appState->expects($this->once())
217+
->method('setAreaCode')
218+
->with(Area::AREA_GLOBAL)
219+
->willReturnSelf();
220+
221+
$this->productCollection->expects($this->once())
222+
->method('getAllIds')
223+
->willReturn($productsIds);
224+
225+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
226+
->disableOriginalConstructor()
227+
->getMock();
228+
229+
$this->productRepository->expects($this->at(0))
230+
->method('getById')
231+
->with($productsIds[0])
232+
->willReturn($productMock);
233+
$this->productRepository->expects($this->at(1))
234+
->method('getById')
235+
->with($productsIds[1])
236+
->willThrowException(new NoSuchEntityException());
237+
238+
$this->imageCache->expects($this->exactly(count($productsIds) - 1))
239+
->method('generate')
240+
->with($productMock)
241+
->willThrowException(new \Magento\Framework\Exception\RuntimeException(__('Some text is here.')));
242+
243+
$commandTester = new CommandTester($this->command);
244+
$commandTester->execute([]);
245+
246+
$this->assertContains(
247+
'Some text is here.',
248+
$commandTester->getDisplay()
249+
);
250+
}
211251
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Image/CacheTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,58 @@ public function testGenerate()
189189
$this->model->generate($this->product);
190190
}
191191

192+
/**
193+
* @expectedException \Magento\Framework\Exception\RuntimeException
194+
*/
195+
public function testGenerateWithException()
196+
{
197+
$imageFile = 'image.jpg';
198+
$imageItem = $this->objectManager->getObject(
199+
\Magento\Framework\DataObject::class,
200+
[
201+
'data' => ['file' => $imageFile]
202+
]
203+
);
204+
$this->mediaGalleryCollection->expects($this->once())
205+
->method('getIterator')
206+
->willReturn(new \ArrayIterator([$imageItem]));
207+
208+
$this->product->expects($this->atLeastOnce())
209+
->method('getMediaGalleryImages')
210+
->willReturn($this->mediaGalleryCollection);
211+
212+
$data = $this->getTestData();
213+
$this->config->expects($this->once())
214+
->method('getMediaEntities')
215+
->with('Magento_Catalog')
216+
->willReturn($data);
217+
218+
$themeMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
219+
->disableOriginalConstructor()
220+
->getMock();
221+
$themeMock->expects($this->exactly(3))
222+
->method('getCode')
223+
->willReturn('Magento\theme');
224+
225+
$this->themeCollection->expects($this->once())
226+
->method('loadRegisteredThemes')
227+
->willReturn([$themeMock]);
228+
229+
$this->viewConfig->expects($this->once())
230+
->method('getViewConfig')
231+
->with([
232+
'area' => Area::AREA_FRONTEND,
233+
'themeModel' => $themeMock,
234+
])
235+
->willReturn($this->config);
236+
237+
$this->imageHelper->expects($this->exactly(3))
238+
->method('init')
239+
->willThrowException(new \Exception('Some text '));
240+
241+
$this->model->generate($this->product);
242+
}
243+
192244
/**
193245
* @return array
194246
*/

0 commit comments

Comments
 (0)