How do I programmatically delete duplicate images? in purple

I run a large online store with a lot of products, and some of the products were not imported correctly and the images were duplicated. products have only 2 identical images. I know that you can delete them manually, but it will take a lifetime.

I searched the internet for some codes that can do this, but they do not work for me. is there anyone who knows a solution for this? I tried to check the codes that I received from the Internet, but I can really get them to work.

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

I tried to check this query in sql database, but this gives no result:

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

enter image description here

+4
source share
3 answers

Here is a small 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/

+5
source

Below is the snippet I used earlier, it 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";
+3
source

, .

, , .

$_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
    }
}

, ( ).

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

, . values2delete .

,

0
source

All Articles