Magento – Trying to get the URL to images from the cache, in the admin panel – “Image file not found”

adminmagento-1.9

What I'm actually trying to do here is generate blog posts automatically based on some custom criteria, and include some product results in them via a custom layout. The data I can't seem to get at is a full path to a product's image in the cache, like this:

media/catalog/product/cache/1/small_image/210x/9df78eab33525d08d6e5fb8d27136e95/s/t/sts52090.jpg

Because I want images that are consistent width/height for a grid layout, and I want to use the already-generated ones from the cache.

I'm generating these from inside a controller in the Admin UI, and these two lines always create a "Image file was not found" error:

$thisMageProduct = Mage::getModel('catalog/product')->load($entity_id);
$thisMageImage = Mage::helper('catalog/image')->init($thisMageProduct, 'small_image');

I have confirmed the first line works – I can return other info about the product besides the image, such as the correct SKU. When I add the second line, it throws the exception.

This is how I have instantiated Mage:

$mageFilename = $MAGROOT . 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

I have confirmed the images do really exist, and that write permissions are set on the media/catalog directory.

What else is there to check??


Editing since I can't respond to your comments:

Your code doesn't work for me. The only way to prevent the exception is to comment out your line.

$product  = Mage::getModel('catalog/product')->loadByAttribute('sku', $productrow['sku']);

if (is_object($product)) {

  //  $smallimg = Mage::helper('catalog/image')->init($product, 'small_image')->resize(210, 210);
    $smallimg = "broken";

} else {

    $smallimg = "/media/catalog/product/cache/1/small_image/130x130/9df78eab33525d08d6e5fb8d27136e95/placeholder/stores/1/no-image-available_2.jpg";

}

Is there something weird about the Admin area that makes these classes work differently?


Edit:

I tried both of these methods, also, and both throw exceptions:

$smallimg = Mage::helper('catalog/image')->init($thismageproduct, 'small_image');
$smallimg = $thismageproduct->getMediaGalleryImages()->getItemByColumnValue('label', 'small_image')->getUrl();

Best Answer

Never did get this to work, but I figured out it doesn't really matter - the hash is the same for all the cached images of the same size, so I just hardcoded it in.

                  $thisimagepath = $WEBPATH
                  . "media/catalog/product/cache/1/small_image/210x/"
                  . "9df78eab33525d08d6e5fb8d27136e95"
                  . strtolower($productrow['small_image']);

Not elegant, not future-proof... but working. (:

Related Topic