You are here

function brightcove_remote_image in Brightcove Video Connect 7.6

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.2 brightcove.module \brightcove_remote_image()
  5. 7.3 brightcove.module \brightcove_remote_image()
  6. 7.4 brightcove.module \brightcove_remote_image()
  7. 7.5 brightcove.module \brightcove_remote_image()

Function that saves a remote image as a local file.

Parameters

string $url: Remote image URL.

Return value

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

1 call to brightcove_remote_image()
theme_brightcove_field_image in ./brightcove_field.formatters.inc
Theme function to render brightcove images.

File

./brightcove.module, line 2333
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']);
  $thumbnail_directory = 'brightcove_thumbnail';
  $fullpath = file_default_scheme() . '://' . $thumbnail_directory;
  $basename = md5_file($url) . ".{$path['basename']}";
  $final_file = "{$fullpath}/{$basename}";
  if (!file_exists($fullpath)) {
    if (!mkdir($fullpath, 0755, TRUE)) {
      drupal_set_message(t('Failed to create directory %directory', [
        '%directory' => drupal_realpath($fullpath),
      ]), 'error');
    }
  }
  if (file_exists($final_file)) {
    return file_build_uri("{$thumbnail_directory}/{$basename}");
  }

  // Perform basic extension check.
  if (!in_array(drupal_strtolower($path['extension']), [
    'jpg',
    'jpeg',
    'png',
    'gif',
  ])) {
    return FALSE;
  }
  if (!file_prepare_directory($fullpath, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    return FALSE;
  }
  return system_retrieve_file($url, $final_file, FALSE, FILE_EXISTS_REPLACE);
}