You are here

function imagecache_create_url in ImageCache 6.2

Same name and namespace in other branches
  1. 5.2 imagecache.module \imagecache_create_url()

Return a URL that points to the location of a derivative of the original image transformed with the given preset.

Special care is taken to make this work with the possible combinations of Clean URLs and public/private downloads. For example, when Clean URLs are not available an URL with query should be returned, like http://example.com/?q=files/imagecache/foo.jpg, so that ImageCache is able intercept the request for this file.

This code began similarly to Drupal core's function file_create_url(), but handles the case of Clean URLs and public downloads differently however. It also implements hook_file_url_alter() which was added to Drupal 7 and backported to PressFlow 6.x.

Parameters

$presetname: String specifying an ImageCache preset name.

$filepath: String specifying the path to the image file.

$bypass_browser_cache: A Boolean indicating that the URL for the image should be distinct so that the visitors browser will not be able to use a previously cached version. Defaults to FALSE.

$absolute: A Boolean indicating that the URL should be absolute. Defaults to TRUE.

3 calls to imagecache_create_url()
imagecache_ui_preset_form in ./imagecache_ui.pages.inc
theme_imagecache in ./imagecache.module
Create and image tag for an imagecache derivative
theme_imagecache_formatter_url in ./imagecache.module

File

./imagecache.module, line 343
Dynamic image resizer and image cacher.

Code

function imagecache_create_url($presetname, $filepath, $bypass_browser_cache = FALSE, $absolute = TRUE) {
  $args = array(
    'query' => empty($bypass_browser_cache) ? NULL : time(),
    // Little hack to avoid having language_url_rewrite() prefix the path with the
    // language code, but preserve the domain rewriting.
    'language' => (object) array(
      'language' => '',
      'domain' => $GLOBALS['language']->domain,
    ),
  );
  $file_directory = file_directory_path();

  // Determine the path of the derivative inside the files directory.
  $derivative_path = 'imagecache/' . $presetname . '/' . _imagecache_strip_file_directory($filepath);

  // Then construct a full path and see if anyone wants to alter it.
  $altered_path = $old_path = $file_directory . '/' . $derivative_path;
  drupal_alter('file_url', $altered_path);

  // If any module has altered the path, then return the alteration...
  if ($altered_path != $old_path) {

    // ...but use url() so our $bypass_browser_cache parameter is honored.
    return url($altered_path, $args);
  }

  // It was unchanged so use the download method's prefix.
  $prefix = array(
    FILE_DOWNLOADS_PUBLIC => $file_directory,
    FILE_DOWNLOADS_PRIVATE => 'system/files',
  );
  $path = $prefix[variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)] . '/' . $derivative_path;
  return url($path, $args + array(
    'absolute' => $absolute,
  ));
}