Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Duplicate images after import
 
liquidstones
Jr. Member
 
Total Posts:  4
Joined:  2012-03-14
 

Hi folks,

Recently we discoverd that magento duplicates images in our webshop.
These images are getting names like : image_1_1 to image_1_32 we suspect this happened while updating our webshop.
The images are located at : Media/Catalog/Product .
How can we prevent the duplication of multiple images in our future imports?
And also what can be a good way to rename these images without having to manually change them all? (because there are thousands of images).

Thanks in advance,

Liquid Stones

 
Magento Community Magento Community
Magento Community
Magento Community
 
VarunK
Jr. Member
 
Total Posts:  1
Joined:  2008-11-19
 

I’m also facing similar issue.
I want to remove images from store that are duplicated due to multiple dataflow imports.

Below is the code that I found after doing some research over this, but never tried.  Because it is removing unused images from directory.

<?php
/** 
 * Delete Unused Product Images. 
 *
 */

if ( ! isset($_SERVER['DOCUMENT_ROOT') ) {
    $_SERVER[
'DOCUMENT_ROOT'str_replace'\\',DSsubstr($_SERVER['SCRIPT_FILENAME']00-strlen($_SERVER['PHP_SELF']) ) );
}

define
('PATH_TO_MAGENTO'rtrim($_SERVER['DOCUMENT_ROOT'],'/')); // update accordingly
require_once PATH_TO_MAGENTO.'/app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

$connection Mage::getSingleton('core/resource')->getConnection('core_write');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<
head>
    <
title>Delete Unused Product Images</title>
    <
meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</
head>
<
body>
<
h1>Delete Unused Product Images</h1>
<
p><a href="?delete=1" onclick="return confirm('Do you want to delete all unused Product Images?')">Delete Images</a></p>
<
div style="white-space: pre">
<?php
if(isset($_GET['delete']) && $_GET['delete'=== '1'{
    $root 
getcwd() . '/media/catalog/product';
    
$delFileCount 1;
    
deleteUnusedImages($root$root$connection);
    if(
$delFileCount == 1{
        
echo 'No images to delete';
    

}

function deleteUnusedImages($d$root$connection$l 0{
    
global $delFileCount;
    
    
$x 'jpg|gif';
    
$files = array();
    foreach(
array_diff(scandir($d), array('.','..')) as $f{
        
if($f != 'cache'
            
if(is_file($d .DS $f) && ( ($x) ereg($x '$'$f) : 1) ) {
                $files[] 
$f;
            
else if(is_dir($d .DS $f)) {
                deleteUnusedImages
($d .DS $f$root$connection$l 1);
                
// If the dir is empty delete it
                
if(!count(array_diff(scandir($d .DS $f), array('.','..')))) {
                    
echo 'Delete empty dir ' $d .DS $f;
                   
// rmdir($d .DS . $f);
                    
echo '. OK.' "\n";
                
}
            }
        }
    }
    
    
if(count($files)) {
        $sql 
'select * from (';
    
           foreach(
$files as $i => $f{
            $sql 
.= "select '" mysql_real_escape_string(str_replace($root''$d)) .DS $f "' as value ";
            if(
$i count($files) - 1{
                $sql 
.= ' union all ';
            
}
        }
        $sql 
.= ' ) as b ' .
        
'where value not in ( ' .
        
'    select value '.
        
'    from catalog_product_entity_media_gallery ' .
        
')';

        
$rows $connection->fetchAll($sql);

        if(
count($rows)) {
            
foreach($rows as $f{
                
// replace the wrong separator
                
$del str_replace((DS == '/') ? '\\' '/'DS$root $f['value']);
                echo 
$delFileCount++ . ' ' $del;
               
// unlink($del);
                
echo '. OK' "\n";
            
}
        }
    }
    flush
();
}
?>
</div>
</
body>
</
html>
File Attachments
img.php  (File Size: 3KB - Downloads: 30)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Store_Manager
Enthusiast
 
Avatar
Total Posts:  867
Joined:  2012-07-09
 

Hello,

Actually, what I can offer you is the following.
There is desktop application - Store Manager for Magento. You can use it to re-import your images. If you have the same image names in the file for that import as images which already exist at your store, the software in any case will overwrite your existing images. So you will have correct images assigned to correct products.

After that in Store Manager run Store Diagnostics and you will see the images which are not linked to any products. After that from there you will be able in bulk to delete unnecessary images.

If you are interested, here you can download free 14-days trial version of Store Manager for Magento -
http://www.magentocommerce.com/magento-connect/store-manager-for-magento.html

It is an alternative solution, but it might be helpful.

 
Magento Community Magento Community
Magento Community
Magento Community
 
diazwatson
Member
 
Avatar
Total Posts:  35
Joined:  2010-04-23
Spain
 

Is there any news regarding this?
I actually tested this script but does not seems to remove any image

 
Magento Community Magento Community
Magento Community
Magento Community
 
BlueclawEcomm
Member
 
Avatar
Total Posts:  35
Joined:  2010-06-25
Leeds, UK
 

You have probably sorted this by now, but in case anyone else comes across this thread then here is a script I wrote to resolve the issue.

deleting duplicate magento product images

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top