Here's the code I used recently for creating a product. It's based on having an array of image urls ($imgArray
) rather than a single image url. It adds the first image in the array as the thumb/base/small and the rest as additional images.
It was based on MagePsycho's blog post here;
http://www.blog.magepsycho.com/how-to-import-product-images-from-external-url-in-magento/
// We set up a $count variable - the first image gets used as small, thumbnail and base
$count = 0;
$mediaAttribute = array (
'thumbnail',
'small_image',
'image'
);
foreach ($imgArray as $image) :
$imgUrl = $this->_save_image( $image );
if ($count == 0) :
$newproduct->addImageToMediaGallery( $imgUrl , $mediaAttribute, false, false );
else :
$newproduct->addImageToMediaGallery( $imgUrl , null, false, false );
endif;
$count++;
endforeach;
And the _save_image
method;
protected function _save_image($img) {
$imageFilename = basename($img);
$image_type = substr(strrchr($imageFilename,"."),1); //find the image extension
$filename = md5($img . strtotime('now')).'.'.$image_type; //give a new name, you can modify as per your requirement
$filepath = Mage::getBaseDir('media') . DS . 'import'. DS . $filename; //path for temp storage folder: ./media/import/
$newImgUrl = file_put_contents($filepath, file_get_contents(trim($img))); //store the image from external url to the temp storage folder
return $filepath;
}
If you are using similar and it isn't working you might want to look at filepaths, permissions and also reindexing as potential issues that could fix your problem.
Here is given programmatically adding media images to product, you can adjust this code to your own importing script:
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'media_gallery');
$mediaGallery = $mediaGalleryAttribute->getBackend();
$attrCode = $mediaGalleryAttribute->getAttributeCode();
$product = Mage::getModel('catalog/product')->load($productId);
$mediaGalleryData = $product->getData($attrCode);
/**
* If you want to remove old images use this code
*/
if (isset($mediaGalleryData['images'])) {
foreach ($mediaGalleryData['images'] as &$image) {
$image['removed'] = 1;
}
$product->setData($attrCode, $mediaGalleryData);
}
/**
* Here is real image file path
*/
$thumbnailFile = 'real/path/to/thumbnail/file.jpg';
$imageFile = 'real/path/to/image/file.jpg';
try {
//Set thumbnail
$imageFileUri = $mediaGallery->addImage($product, $thumbnailFile, null, false, false);
$mediaGallery->setMediaAttribute($product, 'thumbnail', $imageFileUri);
//Set image (or base image, small image whatever you want
$imageFileUri = $mediaGallery->addImage($product, $imageFile, null, false, false);
$mediaGallery->setMediaAttribute($product, 'image', $imageFileUri);
$product->save();
} catch (Exception $e) {
Mage::logException($e);
}
Best Answer
place the below code in a file which is in root directory and execute the code. Please take the
backup
of your database before running the code.