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
SKUand 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.
AI-Powered Recommended Articles
How to Resize Product Images Programmatically in Magento 2
Learn how to resize product images programmatically in Magento 2 for faster loading times and better performance.
How To Get Resize Image in Custom Module
Learn how to retrieve and resize images in your custom module in Magento 2.
How to Update Bulk Product SKU using CSV in Magento 2
Learn how to bulk update product SKUs using CSV files in Magento 2 to streamline product management processes.
Magento 2 Add Image on Admin UI Grid
Learn how to add product images to the admin grid in Magento 2 to enhance product visibility in the backend.
How to Add Magento Ui Image Uploader on Store Configuration in Magento 2
Enable image uploads in the store configuration section of Magento 2 for a more versatile admin experience.
How to Install Module or Run CLI Command Without SSH in Magento 2
Learn how to install modules and run CLI commands in Magento 2 without needing SSH access.