Magento Module: Image Cleaner

A fully-automated system for purging unused catalog images.

Details

This module was developed to curb a rapidly growing media storage of catalog images. Because Magento does not natively provide a way to manage detached or unused images, this module steps in to manage the cleanup process.

Screenshots

Image cleaning management console

Technical Features

Code Sample

public function getCategoryImageIds()
{
    $fileIds    = array();
    $transport  = new Varien_Object(array('attributes' => $this->_categoryImageAttributes));

    Mage::dispatchEvent(
        'image_cleaner_prepare_category_image_attribute_codes',
        array('transport' => $transport)
    );

    $attributes = $transport->getAttributes();

    foreach ($attributes as $attributeCode) {
        $select     = $this->_getReadAdapter()
            ->select()
            ->from(array('main_table' => $this->getTable('catalog/category')));
        $attribute  = Mage::getSingleton('eav/config')->getAttribute(
            Mage_Catalog_Model_Category::ENTITY,
            $attributeCode
        );
        $tableAlias = "{$attributeCode}_table";

        $select->join(
            array($tableAlias => $attribute->getBackendTable()),
            new Zend_Db_Expr("main_table.entity_id = {$tableAlias}.entity_id && {$tableAlias}.attribute_id={$attribute->getId()}"),
            array($attributeCode => 'value')
        );

        $results    = $this->_getReadAdapter()->fetchAll($select);
        $basePath   = rtrim(Mage::getBaseDir('media'), DS) . DS . 'catalog' . DS . 'category';

        foreach ($results as $result) {
            if (!empty($result[$attributeCode])) {
                $fileIds[] = $this->generateFileId( $basePath . $result[$attributeCode] );
            }
        }
    }

    return array_filter( ( array_unique($fileIds) ) );
}

Static Analysis