You are here

function lazyloader_image in Image Lazyloader 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.

Return value

string HTML for a lazyloaded image

1 string reference to 'lazyloader_image'
lazyloader_theme_registry_alter in ./lazyloader.module
Implements hook_theme_registry_alter().

File

./lazyloader.module, line 94
Lazyloader Module

Code

function lazyloader_image($variables) {
  static $rdwimages_enabled, $image_placeholder_src;
  $attributes = $variables['attributes'];
  $noscript_attributes = $variables['attributes'];
  $src = file_create_url($variables['path']);
  if (_lazy_loader_enabled()) {
    $attributes['data-src'] = $src;
    if (!isset($image_placeholder_src)) {

      // Path to dummy placeholder image, to be replaced by actual image.
      $image_placeholder = trim(variable_get('lazyloader_placeholder', LAZYLOADER_PLACEHOLDER));
      $image_placeholder_src = $image_placeholder ? base_path() . $image_placeholder : file_create_url(drupal_get_path('module', 'lazyloader') . '/image_placeholder.gif');
    }
    $attributes['src'] = $image_placeholder_src;
    $noscript_attributes['src'] = $src;

    // Integrate with Responsive Webdesign module.
    if (!isset($rdwimages_enabled)) {
      global $_rwdimages_set;
      $rdwimages_enabled = module_exists('rdwimages') && $_rwdimages_set['enabled'];
    }
    if ($rdwimages_enabled) {
      $attributes['class'] = array(
        'rwdimage',
      );
    }
  }
  else {
    $attributes['src'] = $src;
  }
  foreach (array(
    'width',
    'height',
    'alt',
    'title',
  ) as $key) {
    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
    if (isset($variables[$key])) {
      $noscript_attributes[$key] = $variables[$key];
    }
  }
  $noscript = '';
  if (!empty($attributes['data-src'])) {
    $noscript = '<noscript><img' . drupal_attributes($noscript_attributes) . ' /></noscript>';
  }
  return '<img' . drupal_attributes($attributes) . ' />' . $noscript;
}