TransWikia.com

Remove Duplicate product Images in Magento 2

Magento Asked by jafar pinjar on November 5, 2021

I have created a script file to delete all the duplicate product images.

Here is the code I used.

<?php
 error_reporting(E_ALL);
 use MagentoFrameworkAppBootstrap;
 require __DIR__ . '/app/bootstrap.php';
 $bootstrap = Bootstrap::create(BP, $_SERVER);
 $obj = $bootstrap->getObjectManager(); 
 $app_state = $obj->get('MagentoFrameworkAppState');
 $app_state->setAreaCode('global');
 ini_set('memory_limit','10240M');
 ini_set('max_execution_time', 0); 
 set_time_limit(0); 


 $mediaApi = $obj->create('MagentoCatalogModelProductGalleryProcessor');
 $storeManager = $obj->get('MagentoStoreModelStoreManagerInterface');
 $mediaUrl = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
 $directoryList=$obj->get('MagentoFrameworkAppFilesystemDirectoryList');
 $path = $directoryList->getPath('media'); 
$productCollection=$obj->create('MagentoCatalogModelResourceModelProductCollection');
$_products = $productCollection->addAttributeToSelect('*')->load();
$i =0;
$total = count($_products);
$count = 0;
foreach($_products as $_prod)
{
    $_product = $obj->create('MagentoCatalogModelProduct')->load($_prod->getId());
$_md5_values = array();

$base_image = $_product->getImage();

if($base_image != 'no_selection')
{
    $mediaUrl = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
    $filepath = $path.'/catalog/product' . $base_image ;
    if(file_exists($filepath))
        $_md5_values[] = md5(file_get_contents($filepath));
}
$i ++;
 // echo "rn processing product $i of $total ";

 // Loop through product images
 $_images = $_product->getMediaGalleryImages();

 if($_images)
   {
    foreach($_images as $_image)
    {
        //protected base image
        if($_image->getFile() == $base_image)
            continue;
        $filepath = $path.'/catalog/product' .$_image->getFile();
        if(file_exists($filepath))
            $md5 = md5(file_get_contents($filepath));
        else
            continue;

        if( in_array( $md5, $_md5_values ))
        {
            $mediaApi->removeImage($_product, $_image->getFile());

            echo "rn removed duplicate image from ".$_product->getSku();
            $count++;
        }
        else 
        {
            $_md5_values[] = $md5;
        }
    }
  }
  $_product->save();
}

Above script removes if same image file exist for a product, then only it will remove it,

In my case if same image imported multiple times for a product, that is created as image.jpg and image_1.jpg.

I need to remove all duplicate images(which are _1,_2 etc) for all the products.

Please correct me in the above script, to remove duplicate image for the product. Thanks

3 Answers

If anyone needs a full script to remove duplicate product images, here it is. This will not remove the image with any image type (small image, thumbnail image, etc.) tagged with the image.

<?php
error_reporting(E_ALL);
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager(); 
$app_state = $obj->get('MagentoFrameworkAppState');
$app_state->setAreaCode('global');
ini_set('memory_limit','10240M');
ini_set('max_execution_time', 0); 
set_time_limit(0);

$mediaApi = $obj->create('MagentoCatalogModelProductGalleryProcessor');
$storeManager = $obj->get('MagentoStoreModelStoreManagerInterface');
$storeManager->setCurrentStore(0);
$mediaUrl = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
$directoryList=$obj->get('MagentoFrameworkAppFilesystemDirectoryList');
$path = $directoryList->getPath('media'); 
$productCollection=$obj->create('MagentoCatalogModelResourceModelProductCollection');
$_products = $productCollection->addAttributeToSelect('*')->load();

$productRepository = $obj->get('MagentoCatalogModelProductRepository');
$i =0;
$total = count($_products);
$count = 0;
$productCount = array();
foreach($_products as $_prod) {
    $_product = $productRepository->getById($_prod->getId());
    $_product->setStoreId(0);
    $_md5_values = array();
    $base_image = $_product->getImage();
    
    if($base_image != 'no_selection') {
        $mediaUrl = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
        $filepath = $path.'/catalog/product' . $base_image ;
        if (file_exists($filepath)) {
            $_md5_values[] = md5(file_get_contents($filepath));            
        }
        $i++;

        echo "rn processing product $i of $total ";
        // Loop through product images
        $gallery = $_product->getMediaGalleryEntries();
        if ($gallery) {
            foreach ($gallery as $key => $galleryImage) {
                //protected base image
                if($galleryImage->getFile() == $base_image) {
                    continue;
                }
                $filepath = $path.'/catalog/product' .$galleryImage->getFile();

                if(file_exists($filepath)) {
                    $md5 = md5(file_get_contents($filepath));
                } else {
                    continue;
                }

                if( in_array( $md5, $_md5_values )) {
                    if (count($galleryImage->getTypes()) > 0) {
                        continue;
                    }
                    unset($gallery[$key]);
                    echo "rn removed duplicate image from ".$_product->getSku();
                    $count++;
                } else {
                    $_md5_values[] = $md5;
                }
            }
            
            $_product->setMediaGalleryEntries($gallery);
            $productRepository->save($_product);            
        }
        //$_product->save();
    }
}
die("Script Completed");

Answered by Kazim Noorani on November 5, 2021

You can Buy Extension available on MP that can clean your unused Catalog Images as well as Duplicate entries You can Clean theme manually or using CRON

Answered by Real Magento Man on November 5, 2021

Make sure you are working on the admin store view:

        $this->storeManager->setCurrentStore(0);
        $product->setStoreId(0);

Use the Media Gallery 'Entries' instead:

    $gallery = $product->getMediaGalleryEntries();

Iterate with:

    foreach ($gallery as $key => $galleryImage) {

You can access the files with:

        $galleryImage->getFile()

Then when you have a duplicate:

                unset($gallery[$key]);

Then set the gallery to your revised one:

        $product->setMediaGalleryEntries($gallery);

Then save the product using the product repository:

        $this->productRepository->save($product);

Answered by Mavis Bacon on November 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP