Delete unused product images in magento -
the image-clean module lists unused images under /media/catalog/product , let delete them. there script automatically delete unused images without user interaction? want run script manually or use cron job every night.
thanks
if take @ source module's admin controller, can see code use perform mass delete
#file: app/code/local/mage/imaclean/controllers/adminhtml/imacleancontroller.php public function massdeleteaction() { $imacleanids = $this->getrequest()->getparam('imaclean'); if(!is_array($imacleanids)) { mage::getsingleton('adminhtml/session')->adderror(mage::helper('adminhtml')->__('please select item(s)')); } else { try { $model = mage::getmodel('imaclean/imaclean'); foreach ($imacleanids $imacleanid) { $model->load($imacleanid); unlink('media/catalog/product'. $model->getfilename()); $model->setid($imacleanid)->delete(); } mage::getsingleton('adminhtml/session')->addsuccess( mage::helper('adminhtml')->__( 'total of %d record(s) deleted', count($imacleanids) ) ); } catch (exception $e) { mage::getsingleton('adminhtml/session')->adderror($e->getmessage()); } } $this->_redirect('*/*/index'); } so, controller action accepts number of "imaclean/imaclean" model ids, uses these ids perform delete. so, key code in action is
$imacleanids = $this->getrequest()->getparam('imaclean'); $model = mage::getmodel('imaclean/imaclean'); foreach ($imacleanids $imacleanid) { $model->load($imacleanid); unlink('media/catalog/product'. $model->getfilename()); $model->setid($imacleanid)->delete(); } so, replicated above code in stand-alone version like
//itterates through 'imaclean/imaclean' models in database $models = mage::getmodel('imaclean/imaclean')->getcollection(); foreach ($models $model) { unlink('media/catalog/product'. $model->getfilename()); $model->setid($model->getid())->delete(); } finally, looks "imaclean/imaclean" models used keep track images no longer needed. looks module creates these (i.e. runs check unused images), in newaction comparelist method of default helper.
public function newaction(){ mage::helper('imaclean')->comparelist(); $this->_redirect('*/*/'); } so, can add start of our script, de-facto magento initialization, should give need.
#file: cleanup.php require_once "app/mage.php"; $app = mage::app("default"); mage::helper('imaclean')->comparelist(); $models = mage::getmodel('imaclean/imaclean')->getcollection(); foreach ($models $model) { unlink('media/catalog/product'. $model->getfilename()); $model->setid($model->getid())->delete(); } that should @ least started. luck!
Comments
Post a Comment