blog-banner

Saving Remote Images as Drupal Files Locally

  • Drupal 7
  • Drupal Planet
  • FILE API

Save Image Files in Drupal Local

'A picture is worth a thousand words, a famous adage. Images play an important role in elaborating the contents. But at times there may transpire a situation where these images have to be saved from a remote site and then upload into the corresponding content. If the images reach an outbound limit, it becomes infeasible to have an endeavor. At such an instance the best approach is to programmatically save the images in your local system.

 

[[{"type":"media","view_mode":"media_original","fid":"94","attributes":{"alt":"","class":"media-image","typeof":"foaf:Image"}}]]

 

At a stretch, the images cannot be saved easily into the local system but can be done with a step of two. There are many other means by which this task can be achieved, but I prefer DOMDocument to other methods. The DOMDocument has the ability to scrape the contents of the page and get the Image tags. Once the image tags are obtained, with the help of the attributes the image source can also be gathered. file_save_data() saves the images from their corresponding source into the local system.

 

The below piece of code is taken from a successful implementation of this task. The below function can be called in the corresponding node's image field. In my case, I retried the main images by making a check against the size of the image. If all the images have to be saved in your case, this check can be removed.

 

The saved images can be viewed in /sites/default/files/images.

  1. function kf_feed_image_to_press_release($content) {
  2.   $collection = array();
  3.   $doc = new DOMDocument();
  4.   @$doc->loadHTML($content);
  5.   $xml = simplexml_import_dom($doc);
  6.   $images = $xml->xpath('//img');
  7.   if (empty($images)) {
  8.     return array();
  9.   }
  10.   $local = array();
  11.   foreach ($images as $image) {
  12.     $image = (array) $image;
  13.     $src = $image['@attributes']['src'];  
  14.     if (isset($local[$source])) {
  15.       continue;
  16.     }    
  17.     $local[$source] = file_get_contents($source);
  18.     $size = array();
  19.     $size = getSizeFile($source);
  20.     if($size > (1024 * 4)) {
  21.       $picture_directory = variable_get('file_default_scheme', 'public') . '://' . 'images';
  22.       file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
  23.       $destination = file_stream_wrapper_uri_normalize($picture_directory . '/' . uniqid() . '.png');
  24.       $file_path = file_save_data($local[$source], $destination, FILE_EXISTS_RENAME);        
  25.       $uid = $file_path->uid;
  26.       $image_path = $file_path->uri;
  27.       $file_image = (object) array(
  28.         'uid' => $uid,
  29.         'uri' => $image_path,
  30.         'filemime' => file_get_mimetype($image_path),
  31.         'status' => 1,
  32.       );
  33.       $file_image = file_copy($file_image, $image_path);
  34.       $collection[] = (array)$file_image;
  35.       return $collection;
  36.     }
  37.   }
  38.   return array();
  39. }

 

Get awesome tech content in your inbox