You are here

function bpn_flickr_download_photos in Bulk File Nodes 7

Batch operation: Downloads a single image from flickr.

Downloads the largest image size available to the public files directory. Also fetches extra information about the image, such as size, date taken, etc.

Parameters

string $flickr_id: The flickr id of the image.

2 string references to 'bpn_flickr_download_photos'
bpn_flickr_form_step_2_submit in modules/bpn_flickr/bpn_flickr.module
Form submission handler for bpn_flickr_form_step_2().
bpn_flickr_private_form_step_2_submit in modules/bpn_flickr_private/bpn_flickr_private.module
Form submission handler for bpn_flickr_private_form_step_2().

File

modules/bpn_flickr/bpn_flickr.module, line 380
Hooks and functions for the bpn_flickr module.

Code

function bpn_flickr_download_photos($flickr_id, &$context) {

  // Set batch message.
  $context['message'] = t('Importing image with flickr id: "@id"', array(
    '@id' => $flickr_id,
  ));

  // Get extra informaiton about the image from flickr.
  $flickr_sizes = bpn_flickr_get_photo($flickr_id);
  $extra_info = bpn_flickr_get_info($flickr_id);
  $largest_size = count($flickr_sizes) - 1;
  $extra_info['photo']['exif_width'] = $flickr_sizes[$largest_size]['width'];
  $extra_info['photo']['exif_height'] = $flickr_sizes[$largest_size]['height'];

  // Get URL to download  image from.
  $parsed_url = parse_url($flickr_sizes[$largest_size]['source']);
  $filename = substr($parsed_url['path'], strrpos($parsed_url['path'], '/') + 1);

  // Request and save the image.
  $downloaded_file = drupal_http_request($flickr_sizes[$largest_size]['source']);
  $destination_uri = file_stream_wrapper_uri_normalize('public://' . $filename);
  $file_uri = file_unmanaged_save_data($downloaded_file->data, $destination_uri);

  // Convert the saved file to a file object.
  $file_object = bpn_flickr_file_uri_to_object($file_uri);
  file_save($file_object);
  $context['results'][] = array(
    'extra_info' => $extra_info['photo'],
    'file_object' => $file_object,
  );
}