How to Resize Specific SKU Images in Magento 2 Using CSV
As we all know, Magento 2 comes standard with an image resize command. However, when we run the image resize command, it will resize all of the product images, which will take too long if the site has a larger catalog size, and in this situation, most developers will have to wait and spend too much time on the image resize process. As a solution, we've written this article to assist you in resizing specific SKU images using CSV.
Let's get started!!
Create the ImageResize.php
file at the <Magento Root Folder Path>/pub/
and paste the below code.
saveCopyzoom_out_map<?php error_reporting(E_ALL); ini_set('display_errors', 1); use Magento\Framework\App\Bootstrap; require dirname(__DIR__) . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP,$_SERVER); $objectManager = $bootstrap->getObjectManager(); $registry = $objectManager->get('Magento\Framework\Registry'); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $registry->register('isSecureArea',true); $file = '/<Magento Root Folder Path>/pub/sku.csv'; $csvData = $objectManager->get('\Magento\Framework\File\Csv')->getData($file); if(empty($argv[1]) || $argv[1]!= 'start'){ echo 'Access Denied'."\n";exit; } echo 'Total Record: '.count($csvData)."\n"; foreach ($csvData as $row => $data) { if($row > 0){ $sku = $data[0]; try { $product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->get($sku); $resize = $objectManager->get('\Magento\MediaStorage\Service\ImageResize'); if ($product->getData()) { $images = $product->getMediaGalleryImages(); if($images){ foreach ($images as $key => $image) { if($image['disabled'] == 0){ $resize->resizeFromImageName($image['file']); echo 'Row:'.$row.' '.$sku.' '.$image['file'].'\n'; } } } } } catch(\Exception $e) { echo 'Row:'.$row.' '.$sku.': is not available.'.'\n'; } } } echo 'Done'; die(); ?>
In above code you need to replace the <Magento Root Folder Path>
path in $file
variable with your actual path.
You must also prepare a CSV file with the column name
SKU
and add all of the product SKUs for which you wish to resize images.
Finally, we are done, To run this script you need to open your terminal and go to the /<Magento Root Folder Path>/pub/
path and run the below command.
saveCopyzoom_out_mapphp ImageResize.php start
That's it!!
You'll see in the code that we've added a start
parameter for security purposes. If this script is accidentally launched in a browser, it will halt execution immediately after the condition that we verified on code.
We hope this Magento article helped you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this post with your team.
Review other articles maybe it'll help you too.
- How to Add Link on Store Configuration Fields Comment in Magento 2
- How to Add Custom Validation Rule in Magento 2
- How to Get Product Collection
- How to Setup Magento 2 PWA Studio
- Get Product Collection By Cetegory ID
- How to Get Current Category
- How to Get Current Product
- Get Product Collection with Filters
- How to Create And Use Plugin In Magento 2