You are here

function theme_lazy_image in Lazy-load 7

Returns HTML for an image.

Parameters

$variables: An associative array containing:

  • path: Either the path of the image file (relative to base_path()) or a full URL.
  • width: The width of the image (if known).
  • height: The height of the image (if known).
  • alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0 always require an alt attribute. The HTML 5 draft allows the alt attribute to be omitted in some cases. Therefore, this variable defaults to an empty string, but can be set to NULL for the attribute to be omitted. Usually, neither omission nor an empty string satisfies accessibility requirements, so it is strongly encouraged for code calling theme('image') to pass a meaningful value for this variable.

  • title: The title text is displayed when the image is hovered in some popular browsers.
  • attributes: Associative array of attributes to be placed in the img tag.
1 theme call to theme_lazy_image()
theme_lazy_image_formatter in ./lazy.module
Returns HTML for an image field formatter.

File

./lazy.module, line 480
Module file for Lazy-load.

Code

function theme_lazy_image($variables) {
  $opt_skipClass = variable_get('lazy_filter_skipClass');
  $opt_selector = ltrim(variable_get('lazy_filter_selector'), '.');
  $opt_src = variable_get('lazy_filter_src') !== 'src' ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
  $opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
  if ($variables['style_name']) {

    // Determine the dimensions of the styled image.
    $dimensions = array(
      'width' => $variables['width'],
      'height' => $variables['height'],
    );
    image_style_transform_dimensions($variables['style_name'], $dimensions);
    $variables['width'] = $dimensions['width'];
    $variables['height'] = $dimensions['height'];

    // Determine the URL for the styled image.
    $variables['path'] = image_style_url($variables['style_name'], $variables['path']);
  }

  // Skip Blazy rendering if...
  //
  // library is not available OR
  // viewing a page listed in disabled pages
  $class = isset($variables['attributes']['class']) ? $variables['attributes']['class'] : array();
  if (!variable_get('lazy_library_installed') || !lazy_is_path_allowed() || in_array($opt_skipClass, $class, TRUE)) {
    return theme('image', $variables);
  }
  $attributes = $variables['attributes'];
  if (variable_get('lazy_prefer_native')) {

    // Required attribute for enabling native lazy-loading.
    $attributes['loading'] = 'lazy';
    $attributes['src'] = file_create_url($variables['path']);
  }
  else {
    $attributes['src'] = $opt_placeholderSrc;
    $attributes[$opt_src] = file_create_url($variables['path']);
    $attributes['class'][] = $opt_selector;
  }
  foreach (array(
    'width',
    'height',
    'alt',
    'title',
  ) as $key) {
    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }
  return '<img' . drupal_attributes($attributes) . ' />';
}