You are here

function imagecache_external_file_download in Imagecache External 8

Same name and namespace in other branches
  1. 7.2 imagecache_external.module \imagecache_external_file_download()

Implements hook_file_download().

When using the private file system, we have to let Drupal know it's OK to download images from our Imagecache External directory.

File

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

Code

function imagecache_external_file_download($uri) {

  // Check if the path contains 'imagecache/external'.
  // If not, we fallback to the Image module.
  if (strpos($uri, '/' . imagecache_external_config()
    ->get('imagecache_directory') . '/') > 0) {

    /** @var \Drupal\Core\Image\Image $image */
    $image = Drupal::service('image.factory')
      ->get($uri);

    // For safety, we only allow our own mimetypes.
    if (in_array($image
      ->getMimeType(), imagecache_external_allowed_mimetypes())) {
      return [
        'Content-Type' => $image
          ->getMimeType(),
        'Content-Length' => $image
          ->getFileSize(),
      ];
    }
  }
  else {

    // Do a fallback to the Image module.
    return image_file_download($uri);
  }
}