0

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();
1

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. (:

  • Feel free to accept your own answer, so this question doesn't count as unanswered anymore – Fabian Schmengler Jul 22 '15 at 10:47
0

I used the following code to retrieve images from Magento in a sort of external image server to serve images to a third party remailer.

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

if (is_object($product)) {

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

} else {

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

}

Note that it calls ->resize() with size of image you're expecting which does two things,

a. creates the 130x130 image in the cache in the small_image/130x130/ image path if it doesn't exist

b. directs it to go to the small_image/130x130/ image path if it already exists and gets the URI

Use your image size of choice (200) in the requested resize.

  • That sure looks the same, other than you looked up your product by sku instead of ID. I tried it that way first with the same result. All of my products have images but I'll try that logic and see, maybe I think they do but they don't. – BNash Mar 4 '15 at 6:11
  • I don't understand how to post code into this comment box UGH – BNash Mar 4 '15 at 6:19
  • Since the comment box is weird I edit my original - short story, your code throws an exception for me exactly as you wrote it. Something weird in the admin area - different classes in use maybe? – BNash Mar 4 '15 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.