Hello Magento Friends,
With this Magento 2 tutorial, I will provide the solution for Resizing Specific Product Image using Command Line in Magento 2.
Product images attract the user’s attention. If they are not displayed properly, it will create a bad user experience. Hence, to make sure the product images fit correctly and display properly, you may need to resize them.
Magento 2 provides an image resize command which will resize all the product images at once. If you want to resize only a specific product image, the below steps will help you to accomplish it.
Steps to Resize Specific Product Image using Command Line in Magento 2:
Step 1: Create a di.xml file at the given path
app/code/Vendor/Extension/etc/di.xml
Now add the code as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandListInterface"> <arguments> <argument name="commands" xsi:type="array"> <item name="imagesResizeCommand" xsi:type="object"> Vendor\Extension\Console\Command\ImagesResizeCommand </item> </argument> </arguments> </type> <type name="Vendor\Extension\Console\Command\ImagesResizeCommand"> <arguments> <argument name="imageResizeScheduler" xsi:type="object"> Magento\MediaStorage\Service\ImageResizeScheduler\Proxy </argument> </arguments> </type> </config> |
Step 2: Now, you need to create the ImagesResizeCommand.php file at the below path
app/code/Vendor/Extension/Console/Command/ImagesResizeCommand.php
Then add the following code
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
<?php declare(strict_types=1); namespace Vendor\Extension\Console\Command; use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\Console\Cli; use Magento\MediaStorage\Service\ImageResize; use Magento\MediaStorage\Service\ImageResizeScheduler; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBarFactory; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Magento\Framework\DB\Query\Generator; use Magento\Framework\DB\Select; use Magento\Framework\App\ResourceConnection; use Magento\Catalog\Model\ResourceModel\Product\Gallery; use Magento\Catalog\Model\ResourceModel\Product\Image as Produc-tImage; /** * Resizes product images according to theme view definitions. */ class ImagesResizeCommand extends Command { /** * Asynchronous image resize mode */ const ASYNC_RESIZE = 'async'; /** * @var ImageResizeScheduler */ private $imageResizeScheduler; /** * @var ImageResize */ private $imageResize; /** * @var State */ private $appState; /** * @var ProgressBarFactory */ private $progressBarFactory; /** * @var ProductImage */ private $productImage; /** * @var ResourceConnection */ private $resourceConnection; public function __construct( State $appState, ImageResize $imageResize, ImageResizeScheduler $imageResizeScheduler, ProgressBarFactory $progressBarFactory, ProductImage $productImage, Generator $generator, ResourceConnection $resourceConnection ) { parent::__construct(); $this->appState = $appState; $this->imageResize = $imageResize; $this->imageResizeScheduler = $imageResizeScheduler; $this->progressBarFactory = $progressBarFactory; $this->productImage = $productImage; $this->batchQueryGenerator = $generator; $this->resourceConnection = $resourceConnection; } /** * @inheritdoc */ protected function configure() { $this->setName('test:product:resize') ->setDefinition($this->getOptionsList()); $this->addOption( 'product_id', null, InputOption::VALUE_REQUIRED, 'Add Product ID' ); } /** * Image resize command options list * * @return array */ private function getOptionsList() : array { return [ new InputOption( self::ASYNC_RESIZE, 'a', InputOption::VALUE_NONE, 'Resize image in asynchronous mode' ), ]; } /** * @inheritdoc * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInter-face $output) { $product_id = $input->getOption('product_id'); $result = $this->executeAsync($output, $product_id); return $result; } /** * Schedule asynchronous image resizing * * @param OutputInterface $output * @param int $product_id * @return int */ private function executeAsync(OutputInterface $output, $prod-uct_id): int { try { $errors = []; $this->appState->setAreaCode(Area::AREA_GLOBAL); /** @var ProgressBar $progress */ $progress = $this->progressBarFactory->create( [ 'output' => $output, 'max' => $this->getCountUsedProductImages($product_id) ] ); $progress->setFormat( "%current%/%max% [%bar%] %percent:3s%% %elapsed% %memory:6s% \t| <info>%message%</info>" ); if ($output->getVerbosity() !== OutputInter-face::VERBOSITY_NORMAL) { $progress->setOverwrite(false); } $productImages = $this->getUsedProductImages($product_id); foreach ($productImages as $image) { $result = $this->imageResizeScheduler->schedule($image['filepath']); if (!$result) { $errors[$image['filepath']] = 'Error image scheduling: ' . $im-age['filepath']; } $progress->setMessage($image['filepath']); $progress->advance(); } } catch (\Exception $e) { $output->writeln("<error>{$e->getMessage()}</error>"); return Cli::RETURN_FAILURE; } $output->write(PHP_EOL); if (count($errors)) { $output->writeln("<info>Product images resized with er-rors:</info>"); foreach ($errors as $error) { $output->writeln("<error>{$error}</error>"); } } else { $output->writeln("<info>Product images scheduled successful-ly</info>"); } return Cli::RETURN_SUCCESS; } /** * Get used product images. * * @param int $product_id * @return \Generator */ private function getUsedProductImages($product_id): \Generator { $batchSelectIterator = $this->batchQueryGenerator->generate( 'value_id', $this->getUsedImagesSelect($product_id), 100, \Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR ); foreach ($batchSelectIterator as $select) { foreach ($this->resourceConnection->getConnection()->fetchAll($select) as $key => $value) { yield $key => $value; } } } /** * Return select to fetch all used product images. * * @param int $product_id * @return Select */ private function getUsedImagesSelect($product_id): Select { $query = 'images.disabled = 0 AND image_value.disabled = 0 AND image_value.entity_id = '.$product_id; return $this->resourceConnection->getConnection()->select()->distinct() ->from( ['images' => $this->resourceConnection->getTableName(Gallery::GALLERY_TABLE)], 'value as filepath' )->joinInner( ['image_value' => $this->resourceConnection->getTableName(Gallery::GALLERY_VALUE_TABLE)], 'images.value_id = image_value.value_id', [] )->where($query); } /** * Get the number of unique and used images of products. * * @param int $product_id * @return int */ private function getCountUsedProductImages($product_id): int { $select = $this->getUsedImagesSelect($product_id) ->reset('columns') ->reset('distinct') ->columns( new \Zend_Db_Expr('count(distinct value)') ); return (int) $this->resourceConnection->getConnection()->fetchOne($select); } } |
Step 3: After deploying this module, you need to execute this below command to resize a specific product image.
1 |
php bin/magento test:product:resize --product_id=1 |
Here, test:product:resize is custom command and product_id is an argument where you need to pass your product id.
Conclusion:
Using the above method, you can easily resize specific product images in Magento 2 via the command line. Moreover, Image Optimizer Extension for Magento 2 helps to reduce the image size without losing its quality.
Happy Coding!