I added the code to update the product images programmatically.

Please refer the below code

$firstProduct  = Mage::getModel ('catalog/product')                                 

$firstProduct->setMediaGallery (array('images'=>array (), 'values'=>array ()))

$mediaGallery = $firstProduct->getMediaGallery();

My doubt here is, how to set added image as base image.

Thank you for your valuable inputs

3 Answers 3


To save product base image use this,

if(//baseimage only one time)
    $mediaAttribute = array (
    $mediaAttribute = null;

$product->addImageToMediaGallery($filepath_to_image, $mediaAttribute, true, false);


//3rd param is move, if it is true then move source file
//4th param is exclude,if true mark image as disabled in product page view 

if you create a new product then, setting it with second attribute array as 'image','thumbnail','small_image' automatically set as base image, or if you edit your product, then just save new image as base with first condition i given, it sets default base image.

  • Thanks for your reply. I tried but no luck, i checked function with core file 3 parameter stands for moving source file. Please let me know if i am doing wrong.
    – Venkat
    Commented Nov 21, 2014 at 11:03
  • What you have tried and what you get with my code.It should be used to set for only one image.Try my edited code. Commented Nov 21, 2014 at 11:17
  • I tried the same code above, images are added. But in admin base, small and thumbnail images radio buttons are not checked for that image. Regarding third parameter i set as false because i don't want to move the original image file.
    – Venkat
    Commented Nov 21, 2014 at 11:38
  • @Venkat so you are editing the product right? Commented Nov 21, 2014 at 11:39
  • yes, in existing product adding new image and make this is base image.
    – Venkat
    Commented Nov 21, 2014 at 11:41

I've got this working for Magento 1.9

require 'app/Mage.php'; 

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setPageSize(5000);
foreach ($products as $product) {
  $gallery_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();
  $path = $gallery_images->toArray()["items"][0]["file"];
  if (!$product->hasImage()) continue;
  if (!$product->hasSmallImage()) $product->setSmallImage($path);
  if (!$product->hasThumbnail()) $product->setThumbnail($path);

Please use below code for assign base image for a product if not assign.

include_once "app/Mage.php";
$app = Mage::app();

$products = Mage::getResourceModel('catalog/product_collection');
$c = 0;
foreach($products as $p){
    $pid = $p->getId();
    $product = Mage::getModel('catalog/product')->load($pid);

    //get all images
    $mediaGallery = $product->getMediaGallery();

    //if($product->getImage() != "no_selection" ){   OR

        if (isset($mediaGallery['images'])){
            //loop through the images
            foreach ($mediaGallery['images'] as $image){

                //set the first image as the base image
                Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('image'=>$image['file'],'small_image'=>$image['file'],'thumbnail'=>$image['file']), 0);

echo($c . " product(s) updated.");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.