You are here

function imagecache_external_generate_path in Imagecache External 8

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

Util to generate a path to an image.

Parameters

string $url: The url to the image.

Return value

string The url to the image.

6 calls to imagecache_external_generate_path()
ImagecacheExternalImage::viewElements in src/Plugin/Field/FieldFormatter/ImagecacheExternalImage.php
Builds a renderable array for a field value.
ImagecacheExternalResponsiveImage::viewElements in src/Plugin/Field/FieldFormatter/ImagecacheExternalResponsiveImage.php
Builds a renderable array for a field value.
ImagecacheExternalTestCase::testCachingExternalImageUsingManagedFileSystem in ./imagecache_external.test
Test caching an external image using the managed file system.
ImagecacheExternalTestCase::_testCachingExternalImageWithImageStyle in ./imagecache_external.test
Test caching an external image with an image style.
template_preprocess_imagecache_external in ./imagecache_external.module
Returns HTML for an image using a specific image style.

... See full list

File

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

Code

function imagecache_external_generate_path($url) {

  // Get configuration.
  $config = imagecache_external_config();

  // Create the external images directory and ensure it's writable.
  $hash = md5($url);
  $filename = $hash;

  // Get the FileSystem service.
  $file_system = \Drupal::service('file_system');

  // Check if this is a non-standard file stream and adjust accordingly.
  $scheme = StreamWrapperManager::getScheme($url);
  $default_scheme = \Drupal::config('system.file')
    ->get('default_scheme');
  if ($scheme == $default_scheme) {

    // Don't try fetch already existing files on local file system.
    return $url;
  }
  elseif ($scheme != 'http' && $scheme != 'https') {

    // Obtain the "real" URL to this file.
    $url = $file_system
      ->realpath($url);
  }

  // Parse the url to get the path components.
  $url_parameters = UrlHelper::parse($url);

  // Add the extension for real images.
  if ($extension = strtolower(pathinfo($url_parameters['path'], PATHINFO_EXTENSION))) {
    if (in_array($extension, [
      'jpg',
      'png',
      'gif',
      'jpeg',
    ])) {
      $filename .= '.' . $extension;
    }
  }
  else {

    // Use jpg as default extension.
    $filename .= $config
      ->get('imagecache_default_extension');
  }
  $directory = $default_scheme . '://' . $config
    ->get('imagecache_directory');
  if ($file_system
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
    $needs_refresh = FALSE;
    $filepath = $directory . '/' . $filename;

    // Allow modules to add custom logic to check if it needs to be re-fetched.
    \Drupal::moduleHandler()
      ->alter('imagecache_external_needs_refresh', $needs_refresh, $filepath);
    if ($needs_refresh === FALSE) {
      return $filepath;
    }
    elseif ($filepath = imagecache_external_fetch($url, $directory . '/' . $filename)) {
      return $filepath;
    }
  }

  // We couldn't get the file.
  return FALSE;
}