Magento – Remove Duplicate product Images in Magento 2

duplicatemagento2product-images

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 \Magento\Framework\App\Bootstrap;
 require __DIR__ . '/app/bootstrap.php';
 $bootstrap = Bootstrap::create(BP, $_SERVER);
 $obj = $bootstrap->getObjectManager(); 
 $app_state = $obj->get('\Magento\Framework\App\State');
 $app_state->setAreaCode('global');
 ini_set('memory_limit','10240M');
 ini_set('max_execution_time', 0); 
 set_time_limit(0); 


 $mediaApi = $obj->create('\Magento\Catalog\Model\Product\Gallery\Processor');
 $storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
 $mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
 $directoryList=$obj->get('\Magento\Framework\App\Filesystem\DirectoryList');
 $path = $directoryList->getPath('media'); 
$productCollection=$obj->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$_products = $productCollection->addAttributeToSelect('*')->load();
$i =0;
$total = count($_products);
$count = 0;
foreach($_products as $_prod)
{
    $_product = $obj->create('Magento\Catalog\Model\Product')->load($_prod->getId());
$_md5_values = array();

$base_image = $_product->getImage();

if($base_image != 'no_selection')
{
    $mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $filepath = $path.'/catalog/product' . $base_image ;
    if(file_exists($filepath))
        $_md5_values[] = md5(file_get_contents($filepath));
}
$i ++;
 // echo "\r\n 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 "\r\n 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

Best Answer

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);
Related Topic