You are here

function textimage_file_download in Textimage 8.4

Same name and namespace in other branches
  1. 8.3 textimage.module \textimage_file_download()
  2. 7.3 textimage.module \textimage_file_download()

Implements hook_file_download().

Control the access to files underneath the textimage directories.

File

./textimage.module, line 60
Textimage - Provides text to image manipulations.

Code

function textimage_file_download($uri) {
  $path = \Drupal::service('stream_wrapper_manager')
    ->getTarget($uri);

  // Private file access for image style derivatives.
  if (strpos($path, 'textimage') === 0) {

    // Check that the file exists and is an image.
    $image = \Drupal::service('image.factory')
      ->get($uri);
    if ($image
      ->isValid()) {
      return [
        // Send headers describing the image's size, and MIME-type...
        'Content-Type' => $image
          ->getMimeType(),
        'Content-Length' => $image
          ->getFileSize(),
      ];
    }
    return -1;
  }
  return NULL;
}