Programmatically add multiple images to a single product in magento

Programmatically add multiple images to a single product in magento

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically add multiple images to a single product in magento

Programmatically Add Multiple Images to a Single Product in Magento

When working with Magento, there are times when you need to add multiple images to a single product programmatically. This could be part of an import process or automated content generation. In this post, we’ll guide you through adding multiple images to a single product using the addImageToMediaGallery method.

Understanding the Magento Image System

Magento uses a media gallery system for managing product images and other media files. Each image is associated with an entity (like a product) and can have attributes like position, label, etc. The core class responsible for this is Mage_Catalog_Model_Product.

Adding Images to a Product Programmatically

To add multiple images to a single product programmatically, you’ll need the product object and the image file paths. Here’s a step-by-step guide:

Step 1: Prepare Your Image Files

Ensure your images are in the correct format (JPEG or PNG) and have proper naming conventions. It’s best to use a unique filename for each image.

$imagePaths = ['/path/to/image1.jpg', '/path/to/image2.jpg'];

Step 2: Get the Product Object

You can retrieve the product object using the product ID. Let’s assume $productId is your desired product ID.

$product = Mage::getModel('catalog/product')->load($productId);

We’ll use a loop to save each image to the media gallery and associate it with our product.

foreach ($imagePaths as $path) {
    // Save the image to the media gallery
    $imgUrl = _save_image($path, Mage::getSingleton('catalog/product_action'));

    // Add the image to the specific product
    if ($count == 0) {
        $product->addImageToMediaGallery($imgUrl, 'image', true, false);
    } else {
        $product->addImageToMediaGallery($imgUrl, null, true, false);
    }

    $count++;
}

Note the use of the addImageToMediaGallery method. The first argument is the image file URL, and the second argument is the media attribute code to which the image belongs (in this case, ‘image’ for primary product images).

Best Practices

  • Always ensure your images have proper permissions and are stored in the appropriate directory.
  • Use a consistent naming convention for your images to avoid conflicts or errors during import.
  • Consider using Magento’s default image import/export functionality for large-scale operations.

Conclusion

Adding multiple images to a single product programmatically in Magento is a straightforward process once you understand the basics of the media gallery system. By following this guide, you can automate image management tasks and streamline your workflow.

Remember, proper image optimization and organization are vital for delivering a great user experience and improving site performance. If you need more advanced image handling or want to learn about other Magento functionalities, consider exploring our comprehensive Magento development services at Laravel Company.

Catalog Import/Export

Our network