import the image to magento programmatically

1.use function  addImageToMediaGallery

addImageToMediaGallery (line 956)

Add image to media gallery

  • access: public
void addImageToMediaGallery (string $file, [string|array $mediaAttribute = null], [boolean $move = false], [boolean $exclude = true])
  • string $file: file path of image in file system
  • string|array $mediaAttribute: code of attribute with type ‘media_image’, leave blank if image should be only in gallery
  • boolean $move: if true, it will move source file
  • boolean $exclude: mark image as disabled in product page view
$prefix=Mage::getBaseDir() ;
$path_lunette=$prefix.'/lunette_360/';

$path_test=$path_lunette.'demo/1.jpg';

$_product=Mage::getModel('catalog/product')->loadByAttribute('sku','15082-c');


$_product->addImageToMediaGallery ($path_test, 'image',false, false);
$_product->addImageToMediaGallery ($path_test, 'small_image',false, false);
$_product->addImageToMediaGallery ($path_test, 'thumbnail' ,false, false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$_product->save();


note : important writer this for set scope view par default
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Leave a Comment