Created
January 20, 2016 10:52
-
-
Save dannevang/3bd31fd451cf75d25729 to your computer and use it in GitHub Desktop.
Script to delete duplicate gallery images from magento. HT: aadil.co
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 | |
/* | |
* DELETE DUPLICATE GALLERY IMAGES | |
* 1. Edit ln 9 to match your magento install | |
* 2. Run from terminal: php /{your_path}/duplicate-delete.php | |
* | |
*/ | |
include('app/Mage.php'); | |
umask(0); | |
//Mage::App('default'); | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
error_reporting(E_ALL | E_STRICT); | |
Mage::setIsDeveloperMode(true); | |
ini_set('display_errors', 1); | |
ob_implicit_flush(1); | |
$mediaApi = Mage::getModel("catalog/product_attribute_media_api"); | |
$_products = Mage::getModel('catalog/product')->getCollection(); | |
$i = 0; | |
$total = count($_products); | |
$count = 0; | |
foreach ($_products as $_prod) { | |
$_product = Mage::getModel('catalog/product')->load($_prod->getId()); | |
$_md5_values = array(); | |
//protected base image | |
$base_image = $_product->getImage(); | |
if ($base_image != 'no_selection') { | |
$filepath = Mage::getBaseDir('media') . '/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 = Mage::getBaseDir('media') . '/catalog/product' . $_image->getFile(); | |
if (file_exists($filepath)) | |
$md5 = md5(file_get_contents($filepath)); | |
else | |
continue; | |
if (in_array($md5, $_md5_values)) { | |
$mediaApi->remove($_product->getId(), $_image->getFile()); | |
echo "\r\n removed duplicate image from " . $_product->getSku(); | |
$count++; | |
} else { | |
$_md5_values[] = $md5; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment