1

I am using Drupal 7 and Feeds to import an image field into a content type article and it does work when the image URL is a direct link to the image.

However images that are linked to image handlers, like:

http://business.iafrica.com/apps/imageUtil/view/article/1008937/1/630x385/

are not importing.

I have tried:

  • to use the Feeds Tamper module and Feeds Tamper PHP and alter the image name, but this resulted in an invalid URL loading,
  • to use Field Image Grabber, but I have an XML source, not HTML.

Here is my source feed URL:

http://resource.thumbtribe.mobi/ds1.portal/portal/1/4/resource/view/maximized/9500187?format=atom_extern

and my Image: URI XPath mapping is:

string(link[@rel="enclosure"]/@href)

2 Answers 2

1

I managed to solve this by using the feeds tamper module with the following php snippet to save the image and then reference the path on my server.

$checkext = substr($field, -5);

if (strpos($checkext,'.') == false)
{
  $filename = str_replace(".","_",microtime(true)).".jpg";
  $output = "public://field/image/".$filename;
  file_put_contents($output, file_get_contents($field));
  return  $output;
}
else
{
  return $field;
}
0

The problem is that current version of Feeds gets the filename from the URL (last part separated by / without anything after ?) where the logic is basically (in FeedsParser.inc):

// Strip any query string or fragment from file name.
list($filename) = explode('?', $this->getValue());
list($filename) = explode('#', $filename);
$filename = rawurldecode(drupal_basename($filename));

so it doesn't detect the extension automatically.

So the solution is to patch Feeds by fetching the file name either from headers (Content Disposition, check this patch) or based on its mime type:

$filename = $this->getLocalValue();
$filemime = file_get_mimetype($filename);

if ($filemime != $this->mime_type) {
  include_once DRUPAL_ROOT . "/includes/file.mimetypes.inc";
  $mapping = file_mimetype_mapping();
  if ($ext_id = array_search($this->mime_type, $mapping["mimetypes"])) {
    $extensions = array_keys($mapping["extensions"], $ext_id);
    foreach ($extensions as $extension) {
      if (in_array($extension, explode(' ', $this->allowedExtensions), TRUE)) {
        $filename .= ".$extension";
        break;
      }
    }
  }
}

Please check the following Drupal issues which have appropriate patches fixing it:

which should be included soon into Feeds.


Alternative solution could be to rename the file before it gets saved.

4
  • Hi @Kenorb, Thank you for your time to post a solution, i have implemented both patches however the images are still not saving. separately of course, i have extreme filenames... please see the import URL ewn.co.za/cdn/… Commented Nov 17, 2015 at 18:17
  • @JohannCombrink Which error do you get? How the image fields shows? Did you applied the recent patch (from #12)? Anything in the logs?
    – kenorb
    Commented Nov 17, 2015 at 21:00
  • I managed to get everything working without duplication of images by implementing another patch, and changing the code. Commented Nov 18, 2015 at 15:19
  • i will update my original answer with the new code. Commented Nov 18, 2015 at 15:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.