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

Comments

  1. Viveka says:

    Hi,

    I am also trying to do the same but in my product.php there is no coding as you are asking to replace.
    I am sending you my coding below, Please guide me the way.

  2. SEM Truth says:

    For Magento 1.5.0.1, you can import external images using the following updated:

    In ‘app\code\core\Mage\Catalog\Model\Convert\Adapter\Product.php’

    Replace:
    foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
    if (isset($importData[$mediaAttributeCode])) {
    $file = $importData[$mediaAttributeCode];
    if (trim($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
    $arrayToMassAdd[] = array(‘file’ => trim($file), ‘mediaAttribute’ => $mediaAttributeCode);
    }
    }
    }

    With:
    foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
    if (isset($importData[$mediaAttributeCode])) {
    $file = $importData[$mediaAttributeCode];
    if (trim($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
    // Start Of Code To Import Images From Urls
    $path_parts = pathinfo($file);
    $html_filename = DS . $path_parts['basename'];
    $fullpath = Mage::getBaseDir(‘media’) . DS . ‘import’ . $html_filename;
    $ch = curl_init(str_replace(” “,”%20″,$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);
    $arrayToMassAdd[] = array(‘file’ => trim($html_filename), ‘mediaAttribute’ => $mediaAttributeCode);
    // End Of Code To Import Images From URLs
    }
    }
    }

    Upload the changes to your local director to prevent overwriting during updates:
    app\code\local\Mage\Catalog\Model\Convert\Adapter\Product.php

  3. Skyler Malley says:

    This is just the solution I am looking for, however when I make this change I am recieving this error any ideas?
    P
    arse error: syntax error, unexpected T_STRING in /home/carlford/public_html/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 698

  4. SEM Truth says:

    Skyler – make sure that when you copy the code over, the single quotes ( ‘ ) do not include any special formatting that can be copied over from the post area.

  5. jack says:

    i changethe code but there has error :

    Parse error: syntax error, unexpected T_STRING in /home/*****/public_html/go/app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php on line 730

    can you help me to fix it?thanks

  6. TECHeGO says:

    I am using Magento 1.5.1.0 and I am getting the following error
    Parse error: syntax error, unexpected T_STRING in /home/elitetac/public_html/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 746

    I have checked that no special formatting has been carried over and the line that it is referring to is

    $ch = curl_init(str_replace(” “,”%20?,$file));

    Any insight or fix would be greatly appreciated as we are attempting to import 30K images into a site via distributor datafeed URL’s.

  7. liam says:

    Hi I’m trying to use your code above for magento 1.6 i dont get any errors but then again i dont get any image. Is there anything i have to do to the image atributes? the url i upload via my feed seems to be working. please advise

  8. Yasen says:

    Hey there! I can’t get this to work on Magento v1.6.0.0 (I am running it locally on XAMPP) and keep getting the “Image does not exist” error in the import pop-up window. Any suggestions? Thanks!

    • SEM Truth says:

      Hello Yasen

      There may be an updated snippet of code specific to 1.6. Also local server setups can encounter issues that are unique. I would suggest that you run a test on a dev server.

  9. hey there!

    Thanks for your efforts!

    I got the code copied across ok, I checked in Dreamweaver for any rogue quote marks, all ok, but sadly, the image didnt get copied across! I just get our standard “awaiting image” pic instead…

    Any idea why this may be? I am running 1.3.1.1 on a Cherokee cloud server.

    Simon

    • SEM Truth says:

      I have not tested this setup in 1.3. I would suggest that you start by verifying that the following code matches.

      app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php

      Verify that the following code matches what is included in the product.php file in Magento 1.3

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

      Secondly I would need an example of your import file external URL. I assume that you are using the full URL path ( including http://), so there should be no issue, but I have to ask.

  10. SEM Truth says:

    Potentially you can use another solution that simply calls external image URLs should you not be concerned with having local copies.

    Reply

  11. Richard says:

    I’m running 1.5.1.0. It seems to work and attempt to download the image to the Media directory and it attaches the file to the product. However, after the import is complete, the image is empty and when I open it it looks to be corrupted.

    Any thoughts?

  12. MagePsycho says:

    Hi Mage Folks

    Instead of using curl we can easily get the external image to our folder by just using
    file_get_contents() & file_put_contents().
    for more:
    http://www.blog.magepsycho.com/how-to-import-product-images-from-external-url-in-magento/

    Happy Importing!!

    Thanks

  13. sjolzy says:

    works fine in magento 1.6.1.0, thank you

  14. And to all who face similar problems like

    ‘Parse error: syntax error, unexpected T_STRING in /home/carlford/public_html/app/code’

    Its just because of these characters ” ‘ while you copy paste this code so just replace these double quotes with proper double quotes ” and single quote ‘.

    And after that just create a dataflow profile separately for images and try , it will work like a cake.

    In my case i am able to import the images to my magento 1.5.1.0. store.

Speak Your Mind

*

CAPTCHA image
*