Magento programmatically add product image

Magento programmatically add product image

July 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento programmatically add product image

Programmatically Adding Product Images in Magento

Adding product images directly in Magento without using the API can be achieved by manipulating the database tables manually or through custom scripts. I’ll provide you with a step-by-step guide on how to accomplish this efficiently.

Understanding the Magento Image Structure

In Magento, each product image is stored as a separate entry in the catalog_product_entity_varchar table, where the attribute_id corresponds to the specific attribute (e.g., small_image, thumbnail, image). The actual image files are saved in the media/catalog/product directory of your Magento installation.

Adding Images via Custom Script

To add product images programmatically without using the API, you can create a custom script that directly interacts with these tables. Here’s an example of how to achieve this:

<?php
require_once 'app/Mage.php';
Mage::app();

$productId = 123; // Replace with your product ID
$imageUrl = 'http://example.com/image.jpg'; // The URL of the image you want to add

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

// Get the attribute IDs for small_image, thumbnail, and image
$smalImageAttrId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'small_image')->getId();
$thumbailAttrId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'thumbnail')->getId();
$imageAttrId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'image')->getId();

// Get the current images
$smalImages = $product->getData($smalImageAttrId);
$thumbnailImages = $product->getData($thumbailAttrId);
$images = $product->getData($imageAttrId);

// Save the new image URLs to the respective arrays
$smalImages[] = $imageUrl;
$thumbnailImages[] = $imageUrl;
$images[] = $imageUrl;

// Update the product with the new images
$product->setData($smalImageAttrId, serialize($smalImages));
$product->setData($thumbailAttrId, serialize($thumbnailImages));
$product->setData($imageAttrId, serialize($images));

// Save the changes to the database
try {
    $product->save();
    echo "Image added successfully!\n";
} catch (Exception $e) {
    echo "Error adding image: " . $e->getMessage() . "\n";
}

Conclusion

Adding product images programmatically without using the API is possible by directly manipulating the database tables. The provided example demonstrates how to retrieve the necessary attribute IDs and update the product’s image data accordingly.

Remember that this approach should be used judiciously, as it bypasses Magento’s built-in validation and security mechanisms. Always ensure you have proper backup procedures in place before running such scripts.

For more advanced customization options and best practices in Magento development, I recommend exploring our Magento Development Services at Laravel Company. Our experienced team can help you optimize your Magento store’s performance and functionality while adhering to industry standards.

Happy coding!

Catalog Database API Configuration

Our network