1

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??

asked Jul 7, 2016 at 12:36
1
  • download the image to the /media/import/ folder Commented Jul 7, 2016 at 13:40

1 Answer 1

0

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;
 }
answered Jul 7, 2016 at 13:36
1
  • get complete image info using this function pathinfo($thumbnail); Commented Jul 7, 2016 at 13:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.