I want to import a image to product.
I have:
$url = 'www.example.com/imagen01.jpg';
$product->addImageToMediaGallery($url, array('image','thumbnail','small'),false,false);
But it doesn't work. How can I do that? Do I need to download the image to /media??
- 
 download the image to the /media/import/ folderPradeep Sanku– Pradeep Sanku2016年07月07日 13:40:04 +00:00Commented Jul 7, 2016 at 13:40
 
1 Answer 1
Hello Please use below code
$imgUrl = 'www.example.com/imagen01.jpg';
$imgName = $this->getImages(imgUrl);
setMediaGallery(array('images' => array(), 'values' => array()))
 ->addImageToMediaGallery('media/import/'.$imgName, array('image', 'thumbnail', 'small_image'), false, false);
public function getImages($imgUrl) {
 $thumbnail = $imgUrl;
 $newfilename = 'image name here with file extension';
 $fullpath = Mage::getBaseDir('media') . DS . 'import' . DS . $newfilename;
 try {
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $thumbnail);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
 $rawdata = curl_exec($ch);
 curl_close($ch);
 if (file_exists($fullpath)) {
 unlink($fullpath);
 }
 $fp = fopen($fullpath, 'x');
 fwrite($fp, $rawdata);
 fclose($fp);
 } catch (Exception $e) {
 }
 return $newfilename;
 }
 - 
 get complete image info using this function pathinfo($thumbnail);Jack– Jack2016年07月07日 13:38:01 +00:00Commented Jul 7, 2016 at 13:38
 
default