Skip to main content
1 of 2
Steve Robbins
  • 2.6k
  • 2
  • 29
  • 44

Programmatically Update Image Labels

I'm trying to update my product images' labels. I want them to be the same as the name of the product.

What I've tried

$mediaModel = Mage::getModel("catalog/product_attribute_backend_media");
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    $mediaModel->updateImage(
        $product->getId(),
        $image->getFile(),
        array("label" => $title)
    );
}

But then I get this error

Fatal error: Call to a member function getAttributeCode() on a non-object in app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php on line 401

I've also tried

$mediaModel = Mage::getModel("catalog/product_attribute_media_api");
$images = $mediaModel->items($product->getId());
foreach ($images as $image) {    
    $mediaModel->update(
        $product->getId(),
        $image['file'],
        array("label" => $title)
    );
}

Which runs fine, but the values aren't updated in the admin.

How do I go about this?

Steve Robbins
  • 2.6k
  • 2
  • 29
  • 44