This is a Magento 1.x shell script that goes trough all the products and tries to identify duplicate images from the media gallery.
A check to not remove base image was not built in.
| <?php | |
| require 'abstract.php'; | |
| /** | |
| * @category Shell | |
| * @package Nudorm | |
| * @author Haydar Ciftci <[email protected]> | |
| * @copyright 2016 Nudorm | |
| */ | |
| class Nudorm_Deduplicate_Product_Images extends Mage_Shell_Abstract | |
| { | |
| /** | |
| * @var Mage_Catalog_Model_Resource_Product_Collection | |
| */ | |
| protected $productsCollection = array(); | |
| /** | |
| * @var Mage_Catalog_Model_Product_Attribute_Media_Api | |
| */ | |
| protected $mediaApi; | |
| /** | |
| * Run script | |
| */ | |
| public function run() | |
| { | |
| $this->productsCollection = Mage::getModel('catalog/product')->getCollection(); | |
| $this->mediaApi = Mage::getModel("catalog/product_attribute_media_api"); | |
| foreach ($this->productsCollection as $dirtyProduct) { | |
| /** @var Mage_Catalog_Model_Product $product */ | |
| $product = Mage::getModel('catalog/product')->load($dirtyProduct->getId()); | |
| $mediaGalleryImages = $product->getMediaGalleryImages(); | |
| if ($mediaGalleryImages) { | |
| $md5sums = array(); | |
| foreach ($mediaGalleryImages as $galleryImage) { | |
| $imagePath = Mage::getBaseDir('media') . '/catalog/product' . $galleryImage->getFile(); | |
| $md5sum = md5(file_get_contents($imagePath)); | |
| if (in_array($md5sum, $md5sums)) { | |
| $this->mediaApi->remove($product->getId(), $galleryImage->getFile()); | |
| Mage::log('Removed ' . $galleryImage->getFile() . ' from product ' . $product->getId(), null, 'dedupe.log'); | |
| } else { | |
| $md5sums[] = $md5sum; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| $dedupe = new Nudorm_Deduplicate_Product_Images(); | |
| $dedupe->run(); |