-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
ImagesResizeCommand.php
109 lines (96 loc) · 3.56 KB
/
ImagesResizeCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Console\Command;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Image\Cache as ImageCache;
use Magento\Catalog\Model\Product\Image\CacheFactory as ImageCacheFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\NoSuchEntityException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ImagesResizeCommand extends Command
{
/**
* @var AppState
*/
protected $appState;
/**
* @var ProductCollectionFactory
*/
protected $productCollectionFactory;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var ImageCacheFactory
*/
protected $imageCacheFactory;
/**
* @param AppState $appState
* @param ProductCollectionFactory $productCollectionFactory
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param ImageCacheFactory $imageCacheFactory
*/
public function __construct(
AppState $appState,
ProductCollectionFactory $productCollectionFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
ImageCacheFactory $imageCacheFactory
) {
$this->appState = $appState;
$this->productCollectionFactory = $productCollectionFactory;
$this->productRepository = $productRepository;
$this->imageCacheFactory = $imageCacheFactory;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('catalog:images:resize')
->setDescription('Creates resized product images');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->appState->setAreaCode('catalog');
/** @var ProductCollection $productCollection */
$productCollection = $this->productCollectionFactory->create();
$productIds = $productCollection->getAllIds();
if (!count($productIds)) {
$output->writeln("<info>No product images to resize</info>");
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}
try {
foreach ($productIds as $productId) {
try {
/** @var Product $product */
$product = $this->productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
continue;
}
/** @var ImageCache $imageCache */
$imageCache = $this->imageCacheFactory->create();
$imageCache->generate($product);
$output->write(".");
}
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
$output->write("\n");
$output->writeln("<info>Product images resized successfully</info>");
}
}