1.20.x theme.inc template_preprocess_image(&$variables)

Preprocess variables for theme_image().

@since 1.8.0 Function added. Populates $variables['attributes'] before theme_image() is called.

Related topics

File

includes/theme.inc, line 1970
The theme system, which controls the output of Backdrop.

Code

function template_preprocess_image(&$variables) {
  // Accept either URI or path key. But both are handled by file_create_url().
  $path_or_uri = $variables['uri'] ? $variables['uri'] : $variables['path'];
  $variables['attributes']['src'] = file_create_url($path_or_uri);

  // Convert all other variables into the attributes array.
  foreach (array('width', 'height', 'alt', 'title') as $key) {
    if (isset($variables[$key])) {
      $variables['attributes'][$key] = $variables[$key];
    }
  }
}