开发者

Delete unused product images in magento

开发者 https://www.devze.com 2023-01-28 06:48 出处:网络
The image-clean module lists unused images under /media/catalog/product and let you delete them. Is there a script that automatically delete unused images without user interaction?

The image-clean module lists unused images under /media/catalog/product and let you delete them. Is there a script that automatically delete unused images without user interaction? I want to run thi开发者_开发百科s script manually or use a cron job every night.

Thanks


If you take a look at the source for that module's admin controller, you can see the code they use to perform a 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 as $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) were successfully deleted', count($imacleanIds)
                )
            );
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
    }
    $this->_redirect('*/*/index');
}

So, this controller action accepts a number of "imaclean/imaclean" model ids, uses these ids to perform a delete. So, the key code in that action is

$imacleanIds = $this->getRequest()->getParam('imaclean');
$model = Mage::getModel('imaclean/imaclean');
foreach ($imacleanIds as $imacleanId) {
    $model->load($imacleanId);
    unlink('media/catalog/product'. $model->getFilename());
    $model->setId($imacleanId)->delete();
}

So, you could replicated the above code in a stand-alone version with something like

//itterates through all 'imaclean/imaclean' models in the database
$models = Mage::getModel('imaclean/imaclean')->getCollection();
foreach ($models as $model) {
    unlink('media/catalog/product'. $model->getFilename());
    $model->setId($model->getId())->delete();
}

Finally, it looks like an "imaclean/imaclean" models are used to keep track which images are no longer needed. It looks like the module creates these (i.e. Runs a check for unused images), in the newAction with the compareList method of the default helper.

public function newAction(){    
    Mage::helper('imaclean')->compareList();
    $this->_redirect('*/*/');
}

So, we can add that to the start of our script, as well as the de-facto Magento initialization, which should give us what we 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 as $model) {
    unlink('media/catalog/product'. $model->getFilename());
    $model->setId($model->getId())->delete();
}   

That should at least get you started. Good luck!


There is a script here on removing media images, be sure to backup database and media before hand. Also there is a SQL statement on there which removes gallery records which don't have any product assigned to anymore.

http://www.codefuel.co.uk/magento-removing-media-that-doesnt-belong-to-products/ I have used this on magento version 1.8.x and it works great.

0

精彩评论

暂无评论...
验证码 换一张
取 消