You are here

function imagecache_external_fetch in Imagecache External 8

Same name and namespace in other branches
  1. 6.2 imagecache_external.module \imagecache_external_fetch()
  2. 7.2 imagecache_external.module \imagecache_external_fetch()
  3. 7 imagecache_external.module \imagecache_external_fetch()

Api function to fetch a url.

Parameters

string $url: The url to fetch.

string $cachepath: The directory where to save the images within the files directory.

Return value

bool|string Either the URI of the file, e.g. public://directory/file.jpg. or FALSE.

1 call to imagecache_external_fetch()
imagecache_external_generate_path in ./imagecache_external.module
Util to generate a path to an image.

File

./imagecache_external.module, line 178
Allows the usage of Image Styles on external images.

Code

function imagecache_external_fetch($url, $cachepath) {

  // Validate the image URL against the whitelist.
  if (imagecache_external_validate_host($url) === FALSE) {
    return FALSE;
  }

  // Drupal config object.
  $config = imagecache_external_config();
  try {

    // Drupal httpClient.
    $http = \Drupal::httpClient();
    $result = $http
      ->request('get', $url);
    $code = floor($result
      ->getStatusCode() / 100) * 100;
    $types = imagecache_external_allowed_mimetypes();

    // If content-type not set, use the default 'application/octet-stream'.
    $response_mimetype = $result
      ->getHeaderLine('content-type') ? strtolower($result
      ->getHeaderLine('content-type')) : 'application/octet-stream';

    // Add extension to the cached file to allow file_entity to use it for
    // mimetype identification.
    $cachepath_ext = pathinfo($cachepath, PATHINFO_EXTENSION);
    $default_extension = $config
      ->get('imagecache_default_extension');
    if (!$cachepath_ext && $default_extension != '') {
      $cachepath .= $default_extension;
    }

    // Explode content-type to handle mimetypes with more than one
    // property (eg. image/jpeg;charset=UTF-8).
    $content_type_array = explode(';', $response_mimetype);
    $content_type_allowed = FALSE;
    foreach ($content_type_array as $content_type) {
      if (in_array(strtolower($content_type), $types)) {
        $content_type_allowed = TRUE;
      }
    }
    if (!empty($result
      ->getBody()) && $code != 400 && $code != 500 && $content_type_allowed) {
      if ($config
        ->get('imagecache_external_management') == 'unmanaged') {
        return \Drupal::service('file_system')
          ->saveData($result
          ->getBody(), $cachepath, FileSystemInterface::EXISTS_REPLACE);
      }
      else {
        $file = file_save_data($result
          ->getBody(), $cachepath, FileSystemInterface::EXISTS_REPLACE);
        return $file
          ->getFileUri();
      }
    }
    else {
      throw new Exception('Image could not be retrieved');
    }
  } catch (Exception $e) {
    $fallback_image_fid = $config
      ->get('imagecache_fallback_image');
    if (!empty($fallback_image_fid) && ($fallback_image = File::load(reset($fallback_image_fid)))) {
      return $fallback_image
        ->getFileUri();
    }
    \Drupal::logger('imagecache_external')
      ->notice(t('The image %url could not be retrieved', [
      '%url' => $url,
    ]));
    return FALSE;
  }
}