This Post is about cleaning out extra media images that are created by Magento’s functionality.
For better understanding, consider a case where we upload CSV of 1000 products, each containing one main image and also one thumb image. So, there would 2000 images uploaded in media folder at location ‘media/catalog/product/…’
Imagine if you set up a cron that uploads products from some CSV file everyday at midnight. It’s not the case that you are going to add/update/delete products everyday. So, if you have not updated anything for one week in CSV file, which is going to be run everyday using cron and products are going to be imported everyday for that whole week. But as the product_id would be same for the products that are not changed in any means, duplicate product would not be created. But the images would be duplicated and copied to ‘media/catalog/product/…’
Consider a case, for a product with images abc.jpg and abc_thumb.jpg. Now, when the same product is uploaded again, then abc_1.jpg and abc_thumb_1.jpg are created in ‘media/catalog/product/…’. Likewise after the whole week, images would be created as abc_2.jpg and abc_thumb_2.jpg and it continues until the last day of the week.
So, after 1 week, 2000 images would be now 14000 images. Just imagine what it would after 1 month, if old products are still there in CSV file that is uploaded by cron everyday. Ultimately, the size of media folder would increase day by day and server memory for our site would also increase. This may cause many serious problems.So, to avoid such problems, extra images from ‘media/catalog/product/…’ should be deleted of those images, whose entries are not found in db.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
Declare following variables inside your class // image clean variables protected $result = array(); public $valdir = array(); (CODE inside your controller action) /* delete duplicated images via image clean */ $this->compareList(); (CODE inside your controller action) (METHOD inside your controller) // delete extra images of 'media/catalog/product/' public function compareList(){ $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $read->select() ->from('catalog_product_entity_media_gallery','*') ->group(array('value_id')); $flushImages = $read->fetchAll($select); echo count($flushImages); $array = array(); foreach ($flushImages as $item1){ $array[] = $item1['value']; } $valores = $array; $pepe = 'media' . DS . 'catalog' . DS . 'product'; $leer = $this->listDirectories($pepe); foreach ($leer as $item){ try { $item = strtr($item,'\\','/'); if(!in_array($item, $valores)){ $valdir[]['filename'] = $item; unlink('media/catalog/product'. $item); } } catch(Zend_Db_Exception $e){ } catch(Exception $e){ //Mage::log($e->getMessage()); } } } (METHOD inside your controller) // list directories public function listDirectories($path){ if(is_dir($path)){ if ($dir = opendir($path)) { while (($entry = readdir($dir)) !== false) { if(preg_match('/^\./',$entry) != 1){ if (is_dir($path . DS . $entry) && !in_array($entry,array('cache','watermark')) ){ $this->listDirectories($path . DS . $entry); } elseif(!in_array($entry,array('cache','watermark')) && (strpos($entry,'.') != 0)) { $this->result[] = substr($path . DS . $entry, 21); } } } closedir($dir); } } return $this->result; } |
Now, there is one pair of image for each product that is uploaded repetitively.
Further flushing out the images of cache folder that is created by Magento at ‘media/catalog/product/cache’,
1 2 3 |
(CODE inside your controller action) /* flush catalog images cache */ Mage::getModel('catalog/product_image')->clearCache(); Mage::dispatchEvent('clean_catalog_images_cache_after'); |
Above code would delete the ‘cache’ folder from ‘media/catalog/product/’. So, now, extra catalog images that are created due to Magento cache would be deleted every time.
While uploading process of images using .csv file, Poeple usually create some temporary folder. So, after the images are copied to Magento directory structure, that temporary folder is not needed. To save memory space on server, it is a good practice to delete, all the extra folders, images, etc. So, to delete that temporary folder.
For example temporary folder named ‘import’ in ‘media’ folder of Magento and it contains .csv file and other images that must be deleted at the end.
It can be done through adding following code:
1 2 3 4 |
(CODE inside your controller action) /* remove import directory after import */ $import = Mage::getBaseDir('media').DS.'import'.DS; system('/bin/rm -rf ' . escapeshellarg($import)); |
Issue Fixed By: Nimit Shah
Guided By: Sunil Patel