You are here

function imagecache_external_generate_path in Imagecache External 7.2

Same name and namespace in other branches
  1. 8 imagecache_external.module \imagecache_external_generate_path()
  2. 6.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.

bool $force_refresh: Whether to refresh the cached image.

Return value

string The url to the image.

4 calls to imagecache_external_generate_path()
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.
imagecache_external_field_formatter_view in ./imagecache_external.module
Implements hook_field_formatter_view().
theme_imagecache_external in ./imagecache_external.module
Returns HTML for an image using a specific image style.

File

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

Code

function imagecache_external_generate_path($url, $force_refresh = FALSE) {

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

  // Check if this is a non-standard file stream and adjust accordingly.
  $scheme = file_uri_scheme($url);
  if ($scheme != 'http' && $scheme != 'https') {

    // Obtain the external URL to this file.
    $url = file_create_url($url);
  }

  // Parse the url to get the path components.
  $url_parameters = drupal_parse_url($url);

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

    // Use jpg as default extension.
    $filename .= variable_get('imagecache_default_extension', '.jpg');
  }
  $default_scheme = file_default_scheme();
  $directory = $default_scheme . '://' . variable_get('imagecache_directory', 'externals');

  // Allow other modules to change the directory.
  drupal_alter('imagecache_external_directory', $directory, $filename, $url);
  if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    $needs_refresh = FALSE;
    $filepath = $directory . '/' . $filename;

    // Allow modules to add custom logic to check if it needs to be re-fetched.
    drupal_alter('imagecache_external_needs_refresh', $needs_refresh, $filepath);
    if ($needs_refresh === FALSE && $force_refresh === FALSE) {
      return $filepath;
    }
    elseif ($filepath = imagecache_external_fetch($url, $directory . '/' . $filename)) {
      return $filepath;
    }
  }

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