How To

Magento 2: Regenerate Catalog Product Cache Images Programmatically

Hello Magento Folks?,

Welcome to Magento How-To Blog. Today I will illustrate Magento 2 Regenerate Catalog Product Cache Images Programmatically. Lastly, we learned How To Get Formatted Price With Currency In Magento 2. Let’s Get In?.

Introduction:

Basically, Magento 2 generates different types of catalog images while creating some products from admin. e.g : small_image , thumbnail_image , base_image etc. It also generates some images with a cache folder too. But in some cases, many cache images are not generated e.g. while upgrading the Magento version, server transfer and especially use third party media storage. The reason is Magento 2 does not generate product page cache images while we are using third party media storage hence for that we need to run the resize command. But it is a very time-consuming process. And to overcome this problem here we have come up with an easy solution. You can regenerate catalog product cache images programmatically as per your need.

Steps to  Regenerate Catalog Product Cache Images Programmatically:

Step 1: Firstly, we require to just update the gallery.phtml file at the following path,

vendor\magento\module-catalog\view\frontend\templates\product\view and add below code at the First line of file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$resizeImage = $objectManager->get('\Magento\MediaStorage\Service\ImageResize');

$_product = $block->getProduct();

$galleryImages = $_product->getMediaGalleryImages();

if ($galleryImages) 

{

foreach ($galleryImages as $image) {

$resizeImage->resizeFromImageName($image->getFile());

}

}

Note: One can update the file path based on your theme.

Step 2: After that refresh the cache.

That’s It.

Over to you:

Hopefully, all have implemented successfully the above steps for Regenerate Catalog Product Cache Images Programmatically in Magento 2. But if you face any problems in this do write it in the comment section below. If you find the article helpful then share it with your Magento developer friends. Let’s meet in the next blog.

Happy Developing?

Click to rate this post!
[Total: 8 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

23 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

24 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago