You are here

function brightcove_remote_image in Brightcove Video Connect 7.2

Same name and namespace in other branches
  1. 6.2 brightcove.module \brightcove_remote_image()
  2. 6 brightcove.module \brightcove_remote_image()
  3. 7.7 brightcove.module \brightcove_remote_image()
  4. 7.3 brightcove.module \brightcove_remote_image()
  5. 7.4 brightcove.module \brightcove_remote_image()
  6. 7.5 brightcove.module \brightcove_remote_image()
  7. 7.6 brightcove.module \brightcove_remote_image()

Function that saves a remote image as a local file.

Parameters

$url string: Remote image URL.

Return value

Returns FALSE if image doesn't exist, cannot be saved or is not image (based on extension). Returns local file path if image already exists or was saved correctly.

3 calls to brightcove_remote_image()
theme_brightcove_field_dialog_image in brightcove_field/brightcove_field.formatters.inc
Theme callback for dialog with image player.
theme_brightcove_field_entity_image_style in brightcove_field/brightcove_field.formatters.inc
Theme callback for imagecache image
theme_brightcove_field_entity_link_dialog in brightcove_field/brightcove_field.formatters.inc
Theme callback for image linking to entity.

File

./brightcove.module, line 233
Brightcove module is an integration layer between any modules using Brightcove API. It makes all necessary checks for the API and makes settings available to the user.

Code

function brightcove_remote_image($url) {
  $parse = parse_url($url);
  $path = pathinfo($parse['path']);
  $fullpath = drupal_realpath(file_default_scheme() . ':/') . '/brightcove_thumbnail';
  $final_file = $fullpath . '/' . $path['basename'];
  if (file_exists($final_file)) {
    return $final_file;
  }

  // Perform basic extension check.
  if (!in_array(strtolower($path['extension']), array(
    'jpg',
    'jpeg',
    'png',
    'gif',
  ))) {
    return FALSE;
  }
  if (!file_prepare_directory($fullpath, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    return FALSE;
  }
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

  // Causes a warning if PHP safe mode is on.
  @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  $rawdata = curl_exec($ch);
  if ($fp = @fopen($final_file, 'x')) {
    fwrite($fp, $rawdata);
    fclose($fp);
  }
  return $final_file;
}