You are here

function _manualcrop_add_cache_control in Manual Crop 7

Adds a cache control parameter to the image URI so the image will be reloaded if the crop selection was changed since it was last feched by the browser.

Parameters

$style_name: Image style name.

$path: The absolute URL where the styled image can be downloaded.

Return value

The altered image URL or NULL if the URL wasn't changed.

2 calls to _manualcrop_add_cache_control()
manualcrop_preprocess_image in ./manualcrop.module
Preprocessing for theme_image(); Force reloading of an image after re-cropping.
manualcrop_preprocess_image_url_formatter in ./manualcrop.module
Preprocessing for theme_image_url_formatter(); Force reloading of an image after re-cropping.

File

./manualcrop.helpers.inc, line 859
Helper functions for the Manual Crop module.

Code

function _manualcrop_add_cache_control($style_name, $url) {

  // Is cache control enabled?
  if (variable_get('manualcrop_cache_control', TRUE)) {
    $styles = manualcrop_styles_with_crop(TRUE);

    // Does this image style have a Manual Crop effect?
    if (isset($styles[$style_name])) {
      $cache_key = 'manualcrop:' . md5($url);

      // Attempt to load the HTTP cache-controller from cache.
      if ($cached_url = cache_get($cache_key)) {
        return $cached_url->data;
      }

      // Get the image path from the URL.
      $match = '/styles/' . $style_name . '/';
      $path = parse_url($url, PHP_URL_PATH);
      $path = drupal_substr($path, strrpos($path, $match) + drupal_strlen($match));
      $path = explode('/', $path);

      // Build the local image URI.
      $scheme = array_shift($path);
      $target = implode('/', $path);
      $image_uri = $scheme . '://' . urldecode($target);

      // Get the image effect.
      $effect = $styles[$style_name]['effect'];
      if (_manualcrop_is_own_effect($effect, FALSE)) {
        unset($style_name);
        switch ($effect['name']) {
          case 'manualcrop_reuse':

            // Use the reuse style to load the crop selection.
            if (!empty($effect['data']['reuse_crop_style'])) {
              $style_name = $effect['data']['reuse_crop_style'];
            }
            break;
          case 'manualcrop_auto_reuse':

            // Get the first applied crop selection.
            if ($crop = manualcrop_load_crop_selection($image_uri)) {
              $crop = reset($crop);
            }
            break;
        }
      }

      // Load the crop selection.
      if (isset($style_name)) {
        $crop = manualcrop_load_crop_selection($image_uri, $style_name);
      }

      // Add the cache controller and cache the new URL.
      if (!empty($crop)) {
        $url .= (strpos($url, '?') ? '&' : '?') . 'c=' . md5($crop->x . '|' . $crop->y . '|' . $crop->width . '|' . $crop->height);
        cache_set($cache_key, $url);
        return $url;
      }
    }
  }
  return NULL;
}