Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i programaticly delete duplicate images? in magento

I manage a big web store with alot of products, and some of the products weren't correctly imported and the images got duplicated. the products just have 2 of the same images. i know that you can delete them by hand but that would take up all my life of time.

i have searched the internet for some of the codes that can do it but they dont work for me. is there anybody who know a solution for this? I have tried to examine the codes that i got from the internet but i really can make them work.

this is one of the solutions that didn't work for me: http://dltr.org/blog/magento/556/Magento-product-images-duplicate-issue-with-CSV-product-importer

i have tried to test this query in the sql database but that does not give any result:

SELECT * FROM `catalog_product_entity_media_gallery` WHERE value_id != value_id AND value=value

enter image description here

like image 864
Djeroen Avatar asked Feb 12 '23 03:02

Djeroen


2 Answers

Here is a little script to find and delete all duplicate images in Magento.

//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();

    //protect 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;
            }
        }
    }
}

http://www.aadil.co/how-to-delete-duplicate-product-images-in-magento/

like image 165
Adam Avatar answered Feb 16 '23 03:02

Adam


Below is a snippet I have used earlier, this works like a charm

Actual link : http://blueclawecommerce.co.uk/blog/removing-duplicate-product-images-in-magento/

    include('app/Mage.php');  
//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;
            }    
 
        }
    } 
     
}
echo "\r\n\r\n finished removed $count duplicated images";
like image 39
Munjal Avatar answered Feb 16 '23 03:02

Munjal