0

I'm working to troubleshoot an issue with a 3rd party extension which we have slightly modified. The stock extension has as model that holds an instance of a product and offers methods related to that product. One method is to retrieve the image URLs, which it does so via string concatenation and always uses the original image file. We have overridden this method in order to have it return a scaled/cached image rather than the original.

The updated code works when its action is invoked via a click in the admin panel, but when run automatically via cron the URLs it returns are empty (and/or the entire result is null--I haven't been able to directly capture the output).

Here's trimmed-down versions of the code of the two:

The Original: String Concatenation return the original product Image URL

    $galleryImages = $this->getProduct()->getData('media_gallery');

    foreach ($galleryImages['images'] as $galleryImage) {

        $images[] = Mage::app()->getStore($this->getStoreId())
                        ->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA, false).
                        'catalog/product/'.ltrim($galleryImage['file'],'/');
    }

    return $images;

The Override: The catalog/image helper returns resized URLs

    $galleryImages = $product->getMediaGalleryImages();

    foreach ($galleryImages as $image) {

        $images[] = (string) Mage::helper('catalog/image')
            ->init($product, 'image', $image->getData('file'))
            ->keepFrame(false)
            ->constrainOnly(true)
            ->resize(700, 700);
    }

    return $images;

What would cause the override to produce null or empty results when invoked via cron, but produce the expected output when invoked by UI interaction with an active session?

  • Could be a path issue. How do you execute your cron job? Is it part of a module or is it a standalone script? Did you try to log the value of getData('file') to check if it was accurate? – Mayers Aug 24 '15 at 15:15
0

Here is the answer.

The value returned from _convertToByte function in Mage_Catalog_Helper_Image is -1 as the CLI php.ini sets the memory_limit to -1 (unlimited).

This could be an oversight by Magento or a design feature not to run this task on unlimited memory, personally I think it’s a bug. I modified the function to the following to set a 2G limit in this case."

if (stripos($memoryValue, 'M') !== false) {
        return (int)$memoryValue * 1024 * 1024;
    }
    elseif (stripos($memoryValue, 'KB') !== false) {
        return (int)$memoryValue * 1024;
    }
    elseif ($memoryValue == "-1") {
        return '2147483648'; //2G
    }

The _convertToByte function in my Magento 1.9.2.3 is in the file lib/Varien/Image/Adapter/Gd2.php

Or alternatively you can edit your cli/php.ini file and set memory_limit = somevalue . Realistically this is better as crons executing php code with unlimited memory can screw up the system in case of loops or bad code.

| improve this answer | |

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.