Skip to content

Instantly share code, notes, and snippets.

@jigar48
Created June 7, 2019 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jigar48/302264b1d58bcd1906c14fde919f578f to your computer and use it in GitHub Desktop.
Save jigar48/302264b1d58bcd1906c14fde919f578f to your computer and use it in GitHub Desktop.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $productCollection->addAttributeToSelect('*')->load();
$fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
$mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
$productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$i =0;
$total = count($collection);
$count = 0;
foreach ($collection as $product){
$_md5_values = array();
$base_image = $product->getImage();
if($base_image != 'no_selection')
{
$filepath = $mediaPath .'catalog/product'. $base_image ;
if(file_exists($filepath))
$_md5_values[] = md5(file_get_contents($filepath));
}
$i ++;
echo "\r\n processing product $i of $total ";
$productModel = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
$_images = $productModel->getMediaGalleryImages();
$existingMediaGalleryEntries = $productModel->getMediaGalleryEntries();
if($existingMediaGalleryEntries){
foreach($existingMediaGalleryEntries as $key => $entry){
//protected base image
if($entry->getFile() == $base_image)
continue;
$filepath = $mediaPath .'catalog/product' . $entry->getFile() ;
if(file_exists($filepath))
$md5 = md5(file_get_contents($filepath));
else
continue;
if(in_array($md5, $_md5_values))
{
unset($existingMediaGalleryEntries[$key]);
echo "\r\n removed duplicate image from ".$product->getSku();
$count++;
} else {
$_md5_values[] = $md5;
}
}
$productModel->setMediaGalleryEntries($existingMediaGalleryEntries);
}
$productRepository->save($productModel);
}
?>
@jigar48
Copy link
Author

jigar48 commented Jun 7, 2019

### This Scripts help you to Remove Duplicate Product Images from the Magento 2 product collection.

Upload this script to Magento 2 root folder and then run : php -f remove-duplicate-img.php

It will automatically remove all duplicate images from the product.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment