Importing Images From External URL into Magento

There are occasions where you might not have access to downloading the images from your current setup into the Magento or it is simply easier to reference the current file path on an external URL. The following snipet will add the ability for you to grab images from an external URL when importing products through a data feed. Rather then setting up the image column within the data feed with /image-name.jpg you can now use the full URL path http://www.domain.com/image-name.jpg as an example.

Download the following file – app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php and search for the following section of code:

foreach ($imageData as $file => $fields) {
try {
$product->addImageToMediaGallery(Mage::getBaseDir(‘media’) . DS . ‘import’ . $html_filename, $fields);
}
catch (Exception $e) {}
}

Comment out the previous section of code or replace tit with the following:

foreach ($imageData as $file => $fields) {
$path_parts = pathinfo($file);
$html_filename = DS . $path_parts[‘basename’];
$fullpath = Mage::getBaseDir(‘media’) . DS . ‘import’ . $html_filename;
try {
$ch = curl_init ($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
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) {}
try {
$product->addImageToMediaGallery(Mage::getBaseDir(‘media’) . DS . ‘import’ . $html_filename, $fields);
}
catch (Exception $e) {}
}

Upload the updated Product.php file to into your local setup to prevent overwriting during upgrades – app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php