4

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

Djeroen
  • 571
  • 6
  • 21

3 Answers3

5

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/

MackieeE
  • 11,751
  • 4
  • 39
  • 56
Adam
  • 73
  • 1
  • 9
  • 1
    Does this make sure there is no 'duplicate' image for the same product or is it looking for duplicates int the entire database? So if I have 2 product with the same images it will delete one of them. I need it so it only looks at individual produts. – Bill Murray Oct 24 '16 at 20:46
3

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";
Munjal
  • 936
  • 1
  • 8
  • 19
  • thanks, Munjal. do i need to putt this code in mage.php (because it says "include mage.php") or do i need to putt it in a seperate file? the original outher says the second one but then it wont recognize it as a php script. – Djeroen Sep 01 '14 at 06:42
  • wrong link, the actual working link is: http://www.blueclawsearch.co.uk/blog/2013/09/12/removing-duplicate-product-images-in-magento/ but it's the same code that the script above... – netusco Feb 11 '16 at 10:08
0

I have a solution that you can adapt.

To delete them physically you need to run it with an import to be sure to have the good images at the end.

$_images = $product->getMediaGalleryImages();
if($_images){
    foreach($_images as $_image){
       $filepath =  Mage::getBaseDir('media') .'/catalog/product' . $_image->getFile()  ;
       check if the both images (the one to import and the one that is present on the server) have the same size else
    }
}

To delete from the database the image linked to a product (still during an import).

$query = "select value_id from catalog_product_entity_media_gallery where entity_id = ?";
$st = $cnx->prepare($query);
$st->execute(array($productId));
$row = $st->fetch();
while ($row !== false) {
    $values2delete[] = $row['value_id'];
    $row = $st->fetch();
}
$query = "delete from catalog_product_entity_media_gallery where value_id IN (". implode(',',$values2delete).")";
$st = $cnx->prepare($query);
$st->execute();

I do not use this two things in the same import so you need to marry them together. You can also fill the values2delete array during the check of the bad files.

I hope it helps

Christophe Ferreboeuf
  • 1,048
  • 14
  • 26
  • where do i putt the first code? and doesnt there need to be a – Djeroen Aug 29 '14 at 08:37
  • If you use the basic import tool of magento, forget about my answer it is made for an improved import tool. I would say that to apply it you need a good knowledge of php and magento. If you have, I can try to explain you further. Do you ? – Christophe Ferreboeuf Aug 29 '14 at 10:05
  • i dont have the best knowledge, i can understand the codes but i cannot write them yet (not the correct syntax) im used to c# – Djeroen Aug 29 '14 at 10:09
  • OK. To remove them from the database, you can try to use a solution like this one : http://stackoverflow.com/a/3312066/2660794 putting the unique index on entity_id if there is one image by product. Else, you need to find a way to add unique index that would permit you to delete correctly the wrong images. – Christophe Ferreboeuf Aug 29 '14 at 10:25
  • thank you this is actually what i want but i think my knowledge let me down here because i do not know what to do with that code, is it a query a php? or something else – Djeroen Aug 29 '14 at 10:38
  • you just run that mysql query in your mysql database manager and it will be good – Christophe Ferreboeuf Aug 29 '14 at 12:07