Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImagesResizeCommand.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Helper\ProgressBar;
17 
18 class ImagesResizeCommand extends \Symfony\Component\Console\Command\Command
19 {
23  private $resize;
24 
28  private $appState;
29 
33  private $objectManager;
34 
40  public function __construct(
41  State $appState,
42  ImageResize $resize,
43  ObjectManagerInterface $objectManager
44  ) {
45  parent::__construct();
46  $this->resize = $resize;
47  $this->appState = $appState;
48  $this->objectManager = $objectManager;
49  }
50 
54  protected function configure()
55  {
56  $this->setName('catalog:images:resize')
57  ->setDescription('Creates resized product images');
58  }
59 
63  protected function execute(InputInterface $input, OutputInterface $output)
64  {
65  try {
66  $this->appState->setAreaCode(Area::AREA_GLOBAL);
67  $generator = $this->resize->resizeFromThemes();
68 
70  $progress = $this->objectManager->create(ProgressBar::class, [
71  'output' => $output,
72  'max' => $generator->current()
73  ]);
74  $progress->setFormat(
75  "%current%/%max% [%bar%] %percent:3s%% %elapsed% %memory:6s% \t| <info>%message%</info>"
76  );
77 
78  if ($output->getVerbosity() !== OutputInterface::VERBOSITY_NORMAL) {
79  $progress->setOverwrite(false);
80  }
81 
82  for (; $generator->valid(); $generator->next()) {
83  $progress->setMessage($generator->key());
84  $progress->advance();
85  }
86  } catch (\Exception $e) {
87  $output->writeln("<error>{$e->getMessage()}</error>");
88  // we must have an exit code higher than zero to indicate something was wrong
89  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
90  }
91 
92  $output->write(PHP_EOL);
93  $output->writeln("<info>Product images resized successfully</info>");
94  }
95 }
$objectManager
Definition: bootstrap.php:17
__construct(State $appState, ImageResize $resize, ObjectManagerInterface $objectManager)