You are here

function adaptive_image_preprocess_image in Adaptive Image 7

Implements template_preprocess_image().

Adds a class to adaptive images for max-width.

File

./adaptive_image.module, line 171
Adaptive Image - Adaptive images for Drupal

Code

function adaptive_image_preprocess_image(&$variables) {
  global $base_url;
  if (isset($variables['style_name'])) {

    // Get image style settings
    $style = image_style_load($variables['style_name']);

    // Check if style contains the adaptive image effect
    if ($style && adaptive_image_contains_effect($style)) {
      $settings = adaptive_image_effect_settings($style);
      $resolutions = explode(',', $settings['resolutions']);
      $resolution = adaptive_image_resolution($resolutions);

      // Only construct direct path if not private
      if (!strpos($variables['path'], '/system/') && is_numeric($resolution)) {
        $path_parts = pathinfo($variables['path']);
        $derivative_uri = $path_parts['dirname'] . '/' . $resolution . '/' . $path_parts['basename'];
      }
      if (isset($derivative_uri) && file_exists(str_replace($base_url, '.', $derivative_uri))) {

        // Deliver existing path to bypass menu callback
        $variables['path'] = $derivative_uri;
      }
      else {

        // Reconstruct the image path to %/%style_name/adaptive-image/% to
        // trigger image generation or file access check
        $variables['path'] = str_replace('styles/' . $variables['style_name'], 'styles/' . $variables['style_name'] . '/adaptive-image', $variables['path']);
      }

      // Add class for styling
      $variables['attributes']['class'][] = 'adaptive-image';

      // Remove fixed image dimensions
      unset($variables['height']);
      unset($variables['width']);
    }
  }
}