Remove Duplicate Images From The Product Gallery Images

In Magento 2, having duplicate images in the product gallery can lead to confusion for customers and affect the overall user experience on your store. It can also increase the page load time unnecessarily, which may impact SEO and conversion rates. This guide will walk you through the steps to Remove Duplicate images from the product gallery programmatically in Magento 2. In this blog post, I will show you how to Remove Duplicate Images From The Product Gallery Images.

Magento 2 stores product images in the gallery associated with each product. The gallery can sometimes have duplicate images, either from manual upload errors or due to programmatic issues. These duplicates not only clutter the gallery but can also negatively impact the user experience. You can find more details about product images, such as naming conventions and best practices here.

Why You Should Remove Duplicate Images?

  • Improved User Experience:  Clean product galleries give customers a better browsing experience.
  • Faster Page Loading: Removing unnecessary images reduces page load time, which is especially important on product pages.
  • Better SEO: Faster page loads and optimized media galleries can improve SEO rankings.

Method to Remove Duplicate images from the product gallery:

Here we will discuss how to remove duplicate images from the product gallery programmatically. While you can also remove them manually from the admin panel, it is a tedious process.

Use the below code snippet on the appropriate event like product save. In this blog post I will trigger this code on the Product save event.

Step 1: Create an event.xml at app/code/Vendor/Module/etc directory and paste the code below:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="remove_duplicate_image_from_gallery" instance="Vendor\Module\Observer\RemoveDuplicateImage" />
    </event>
</config>

Step 2: Create RemoveDuplicateImage.php file at app/code/Vendor/Module/Observer directory and paste the code below:

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class AddCustomOption implements ObserverInterface
{
	/**
	 * @var ProductRepositoryInterface
	 */
	protected $productRepositoryInterface;

	/**
	 * @param ProductRepositoryInterface $productRepositoryInterface
	 */
	public function __construct(
        ProductRepositoryInterface $productRepositoryInterface
    ) {
    	$this->productRepositoryInterface = $productRepositoryInterface;
  	}

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getProduct();
        $mediaGalleryEntries = $product->getMediaGalleryEntries();
        if (count($mediaGalleryEntries) > 1) {
            $imageHashArray = [];
            $filteredImages = [];
            foreach ($mediaGalleryEntries as $imageKey => $image) {
                $imagePath = $image->getFile();
                $imageHash = hash_file('md5', BP . '/pub/media/catalog/product' . $imagePath);
                if (!in_array($imageHash, $imageHashArray)) {
                    $imageHashArray[] = $imageHash;
                    $filteredImages[] = $image;
                }
            }
            $this->productRepositoryInterface->save($product);
        }   
    }
}

Now once the product has been saved then it will remove all the duplicate images from the gallery. Kudos!.

You may also like this:

Conclusion:

Programmatically removing duplicate images from your Magento 2 product gallery is an efficient solution for maintaining a clean and optimized catalog, especially for stores with a large number of products. By automating this process, you not only save time but also improve the user experience and page load times. While manual removal is an option, it becomes impractical for larger inventories. Implementing a programmatic solution or using available extensions ensures that your product galleries remain free from clutter, contributing to a smoother, faster, and more professional online store.

Rate this post