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. 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. 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. 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. 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. 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!

    • 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.

    • This may be due to server permissions. The images are being uploaded to /media/import. This folder needs to have write permissions (777). It is how I got rid of my ‘Image does not exist’ error.

  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

    • 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. Potentially you can use another solution that simply calls external image URLs should you not be concerned with having local copies.

    Reply

  11. 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. SEM Truth,

    Simply put, I’m amazed that there aren’t more people doing this. Having said that, this would be very impressive, if I could get it to work. I’ve copied your code verbatim, but anytime I try to run an import, it errors out. The problem is no errors get dumped into the logs and the spot for the error is empty. Looking at the system log (Thank god this is our test server.) the server is complaining about this:

    [code]
    2011-10-05T21:22:16+00:00 DEBUG (7): HEADERS ALREADY SENT: [0] /home/testxserver/public_html/app/code/core/Mage/Core/Controller/Response/Http.php:44
    [1] /home/testxserver/public_html/lib/Zend/Controller/Response/Abstract.php:727
    [2] /home/testxserver/public_html/app/code/core/Mage/Core/Controller/Response/Http.php:75
    [3] /home/testxserver/public_html/app/code/core/Mage/Adminhtml/controllers/System/Convert/ProfileController.php:211
    [4] /home/testxserver/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php:418
    [5] /home/testxserver/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:254
    [6] /home/testxserver/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php:177
    [7] /home/testxserver/public_html/app/code/core/Mage/Core/Model/App.php:304
    [8] /home/testxserver/public_html/app/Mage.php:596
    [9] /home/testxserver/public_html/index.php:99
    [/code]

    Looking at the code, the only thing I can think of is the fact that you are using curl to grab the header here:

    [code]
    curl_setopt($ch, CURLOPT_HEADER, 0);
    [/code]

    Anyways, if I use the old product.php and it will import, but no images. Any advise you can give me to get this squared away? I’m on 1.4.0.1 if that helps.

  13. 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

  14. works fine in magento 1.6.1.0, thank you

    • Hi sjolzy, could you please advise how to implement this functionality in Magento 1.6.1?

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

      Only similiar:
      ******************
      $addedFilesCorrespondence = $mediaGalleryBackendModel->addImagesWithDifferentMediaAttributes(
      $product,
      $arrayToMassAdd, Mage::getBaseDir(‘media’) . DS . ‘import’,
      false,
      false
      );
      ******************

    • hello,

      i have magento 1.6.1.0 but i cant find the code where do you find it ?

      thanks

  15. 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.

  16. Roy Bainbridge says:

    This is driving me mad, I’m running Magento 1.5.0.1 and I just can’t seem to get this to work… Last time I tried it I broke Magento with a 500 server error. I can’t seem to find that exact bit of code either. I do have the following though:

    foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
    $addedFile = ”;
    if (isset($importData[$mediaAttributeCode . ‘_label’])) {
    $fileLabel = trim($importData[$mediaAttributeCode . ‘_label’]);
    if (isset($importData[$mediaAttributeCode])) {
    $keyInAddedFile = array_search($importData[$mediaAttributeCode],
    $addedFilesCorrespondence[‘alreadyAddedFiles’]);
    if ($keyInAddedFile !== false) {
    $addedFile = $addedFilesCorrespondence[‘alreadyAddedFilesNames’][$keyInAddedFile];
    }
    }

    if (!$addedFile) {
    $addedFile = $product->getData($mediaAttributeCode);
    }
    if ($fileLabel && $addedFile) {
    $mediaGalleryBackendModel->updateImage($product, $addedFile, array(‘label’ => $fileLabel));
    }
    }
    }

    Any ideas? Thanks

  17. Please could someone help. Am using Magento 1.6.1 but it does not seem to work. When I use Data Flow Import I get an error message that Image Does not exist

  18. Causing some errors in Mag 1.7, trying to fix.

  19. I’m using magento 1.7.0.2 and code for it is instead of this one:
    foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
    if (isset($importData[$mediaAttributeCode])) {
    $file = trim($importData[$mediaAttributeCode]);
    if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
    $arrayToMassAdd[] = array(‘file’ => trim($file), ‘mediaAttribute’ => $mediaAttributeCode);
    }
    }
    }

    put this one:
    foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
    if (isset($importData[$mediaAttributeCode])) {
    $file = trim($importData[$mediaAttributeCode]);
    if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
    // Start Of Code To Import Images From Urls
    if (preg_match(‘%https?://[a-z0-9\-./]+\.(?:jpe?g|png|gif)%i’, $file)) {
    $path_parts = pathinfo($file);
    $html_filename = DS . $path_parts[‘basename’];
    $fullpath = Mage::getBaseDir(‘media’) . DS . ‘import’ . $html_filename;
    if(!file_exists($fullpath)) {
    $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);
    }
    $arrayToMassAdd[] = array(‘file’ => trim($html_filename), ‘mediaAttribute’ => $mediaAttributeCode);
    }
    else
    {
    $arrayToMassAdd[] = array(‘file’ => trim($file), ‘mediaAttribute’ => $mediaAttributeCode);
    }
    }
    }
    }

  20. shashank gupta says:

    Can you please provide correct code for 1.7.0.2 , its urgent .

Speak Your Mind

*

CAPTCHA image
*