Magento not finding or showing right product images

Problem: the wrong image is shown compared to the image you inserted. Possible errors:

  • Check per product all the store view images. Maybe it crosses with your desired image.
  • Rebuild the image cache
  • Check permissions on the directory
  • Check whether there is an image with the same name already in the media file. Also check whether the problem is due to case insensitivity
  • Reindex the catalog product structure. This makes sure that the right product image is linked and shown

UPDATE: Magento showing image in frontend, but not in backend

Solution: I probably found the solution now. The image is shown in the frontend, but not in the backend. Probably this is a solution: http://www.blog.magepsycho.com/how-to-fix-the-issue-product-images-missing-in-backend-but-not-in-frontend/ . It is a script that checks for ‘damages’ in the table, which is the reason why an image doesn’t show.

UPDATE2: Multistore shows wrong/different images per store

How to get back the images and reset all the images

An extension on the above update is my own customization on the script. It gets back images that do show up in the frontend but not in the backend or when wrong/different images are shown among the various storeviews.

update_missing_files.php

 * @website     http://www.magepsycho.com edited by http://www.devproblems.com
 * @category    Export / Import
 */
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

set_time_limit(0);
ini_set('memory_limit','1024M');

/***************** UTILITY FUNCTIONS ********************/
function _log($message, $file = 'update_missing_images.log'){
    Mage::log($message, null, $file);
}

function _getIndex($field) {
    global $fields;
    $result = array_search($field, $fields);
    if($result === false){
        $result = -1;
    }
    return $result;
}

function _getConnection($type = 'core_read'){
    return Mage::getSingleton('core/resource')->getConnection($type);
}

function _getTableName($tableName){
    return Mage::getSingleton('core/resource')->getTableName($tableName);
}

function _getAttributeId($attribute_code = 'price'){
    $connection = _getConnection('core_read');
    $sql = "SELECT attribute_id
                FROM " . _getTableName('eav_attribute') . "
            WHERE
                entity_type_id = ?
                AND attribute_code = ?";
    $entity_type_id = _getEntityTypeId();
    return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}

function _getEntityTypeId($entity_type_code = 'catalog_product'){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
    return $connection->fetchOne($sql, array($entity_type_code));
}

function _getIdFromSku($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, array($sku));
}

function _checkIfSkuExists($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    $count      = $connection->fetchOne($sql, array($sku));
    if($count > 0){
        return true;
    }else{
        return false;
    }
}

function _checkIfRowExists($productId, $attributeId, $value){
    $tableName  = _getTableName('catalog_product_entity_media_gallery');
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName($tableName) . " WHERE entity_id = ? AND attribute_id = ?  AND value = ?";
    $count      = $connection->fetchOne($sql, array($productId, $attributeId, $value));
    if($count > 0){
        return true;
    }else{
        return false;
    }
}

function _insertRow($productId, $attributeId, $value){
    $connection             = _getConnection('core_write');
    $tableName              = _getTableName('catalog_product_entity_media_gallery');

    $sql = "INSERT INTO " . $tableName . " (attribute_id, entity_id, value) VALUES (?, ?, ?)";
    $connection->query($sql, array($attributeId, $productId, $value));
}

function _deleteWrongRows($productId, $smallImageId, $imageId, $thumbnailId){
    $connection             = _getConnection('core_write');
    $tableName              = _getTableName('catalog_product_entity_varchar');

    $sql = "DELETE FROM " . $tableName . " WHERE `entity_id` = ?
	AND store_id != ?
	AND (
	attribute_id = ?
	OR attribute_id = ?
	OR attribute_id = ?
	)";
	//printf($sql, $productId, 0, $smallImageId, $imageId, $thumbnailId);
    $connection->query($sql, array($productId, 0, $smallImageId, $imageId, $thumbnailId));
}

function _updateMissingImages($count, $productId, $data){
    $connection             = _getConnection('core_read');
    $smallImageId           = _getAttributeId('small_image');
    $imageId                = _getAttributeId('image');
    $thumbnailId            = _getAttributeId('thumbnail');
    $mediaGalleryId         = _getAttributeId('media_gallery');

    //getting small, base, thumbnail images from catalog_product_entity_varchar for a product
    $sql    = "SELECT * FROM " . _getTableName('catalog_product_entity_varchar') . " WHERE attribute_id IN (?, ?, ?) AND entity_id = ? AND `value` != 'no_selection'";
    $rows   = $connection->fetchAll($sql, array($imageId, $smallImageId, $thumbnailId, $productId));
    if(!empty($rows)){
        foreach($rows as $_image){
            //check if that images exist in catalog_product_entity_media_gallery table or not
            if(!_checkIfRowExists($productId, $mediaGalleryId, $_image['value'])){
                //insert that image in catalog_product_entity_media_gallery if it doesn't exist
                _insertRow($productId, $mediaGalleryId, $_image['value']);
                /* Output / Logs */
                $missingImageUpdates = $count . '> Updated for:: $productId=' . $productId . ', $image=' . $_image['value'];
                echo $missingImageUpdates.'
';
                _log($missingImageUpdates);
            }

            // delete wrong product selected images storeview rows
            if(_deleteWrongRows($productId, $smallImageId, $imageId, $thumbnailId)){
            	$ImageUpdates = $count . '> Deleted wrong rows for:: $productId=' . $productId . ', $image=' . $_image['value'];
                echo $ImageUpdates.'
';
                _log($ImageUpdates);
            }
        }
        $separator = str_repeat('=', 100);
        _log($separator);
        echo $separator . '
';
    }
}
/***************** UTILITY FUNCTIONS ********************/

$messages           = array();
$csv                = new Varien_File_Csv();
$data               = $csv->getData('update_missing_images.csv'); //path to csv
$fields             = array_shift($data);
#print_r($fields); print_r($data); exit;

$message = '

<hr />

'; $count = 1; foreach($data as $_data){ $sku = isset($_data[_getIndex('sku')]) ? trim($_data[_getIndex('sku')]) : ''; if(_checkIfSkuExists($sku)){ try{ $productId = _getIdFromSku($sku); _updateMissingImages($count, $productId, $_data); $message .= $count . '&gt; Success:: While Updating Images of Sku (' . $sku . ').
'; }catch(Exception $e){ $message .= $count .'&gt; Error:: While Upating Images of Sku (' . $sku . ') =&gt; '.$e-&gt;getMessage().'
'; } }else{ $message .= $count .'&gt; Error:: Product with Sku (' . $sku . ') does\'t exist.
'; } $count++; } echo $message; //$process = Mage::getSingleton('index/indexer')-&gt;getProcessByCode('catalog_product_flat'); ?&gt;

Make a file update_missing_images.csv like:

sku
SKU1234
SKU4312
ETC

USE THIS SCRIPT AT YOUR OWN RISK