Created
June 7, 2019 08:59
-
-
Save jigar48/302264b1d58bcd1906c14fde919f578f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
### 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.