This is similar to this question, but I think I understand what I'm trying to do I just don't understand why calling $product->addImageToMediaGallery is throwing an error.

I am trying to generate images for products on the fly inside of a catalog_product_save_before observer using imagejpeg. The images are drawn from a custom attribute added to categories (each category's products will each share the same background image) with the product's title written on top and saved.

All of this works currently, I generate the image and right now I am storing it inside of media/tmp. I want to tell Magento to go ahead and generate all of the assorted sizes that product images normally get generated to, and then add those images to the product, storing them at the correct place inside of the media/catalog/product directory.

As far as I know, calling $product->addImageToMediaGallery($filePath, $imageType, false) will just set the path to the product image at it's current location inside of media/tmp, which I don't want. Is there a particular way to instruct Magento to go ahead and run the thumbnail resize process and move the image to its correct media location?


I tried going this route, and added the following to my observer once the generated image is ready:

// Adapted from: @Zachary Schuessler, https://stackoverflow.com/a/10540132/697370
// Add three image sizes to media gallery
$mediaArray = array(
    'thumbnail'   => $filename,
    'small_image' => $filename,
    'image'       => $filename,

// Remove unset images, add image to gallery if exists
$importDir = Mage::getBaseDir('media') . DS . 'tmp/';

foreach ( $mediaArray as $imageType => $fileName ) {
    $filePath = $importDir . $fileName;
    if ( file_exists($filePath) ) {
        $product->addImageToMediaGallery($filePath, $imageType, false);

When this runs, I can see images being generated inside of /media/tmp/catalog/product/(a)/(b), but when it hits /app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php on line 329, I get an error: Warning: Invalid argument supplied for foreach() I look up that line, and it looks like this, inside of Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage:

foreach ($mediaGalleryData['images'] as &$image) { 

I quickly put in a dump and die statement on the media gallery data:

array (size=2)
  'images' => string '[]' (length=2)
  'values' => string '{"image":"no_selection","small_image":"no_selection","thumbnail":"no_selection"}' (length=80)

Sure enough, I don't think you can foreach over a string like that. I'm pretty sure my path is right beause the images are being generated correctly in the media/tmp/catalog/products directory, but I cant seem to be able to get this addImageToMediaGallery call to work the way I think that it should.


I'm adding this as an answer because it works, though I think my problem is that there is some extension that is modifying these attributes at some point. I haven't really figured out where.

Here is my workaround:

// Hack because for some reason the images key in the media gallery is a 
// JSON encoded string instead of an array that 
// Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage expects
$mediaGallery = $product->getMediaGallery();
$mediaGallery['images'] = json_decode($mediaGallery['images'], true);

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.