theme_image

5 theme.inc theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE)
6 theme.inc theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE)
7 theme.inc theme_image($variables)
8 theme.inc theme_image($variables)

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.

Related topics

File

includes/theme.inc, line 1661
The theme system, which controls the output of Drupal.

Code

<?php
function theme_image($variables) {
  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }

  return '<img' . drupal_attributes($attributes) . ' />';
}
?>

Comments

Image with style

Example use

<?php

  $variables
= array(
     
'path' => 'path/to/img.jpg',
     
'alt' => 'Test alt',
     
'title' => 'Test title',
     
'width' => '50%',
     
'height' => '50%',
     
'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
      );
 
$img = theme('image', $variables);
?>

This example seems to be

This example seems to be misaligned with the topic. The topic is about the theme_image($variables) function, but the example does not show this.
The example shows the theme() function which is a different function.
Staying on topic, the example should show as the last line $img = theme_image($variables);

Actually, theme('image', $variables) is the right notation

This always trips up n00bies.

The example is correct as it

The example is correct as it allows this theme function to be overridden in a template or module. Calling theme_image directly does not and so is not recommended.

It's the incorrect function.

There are several good reasons to use theme() instead of theme_image(), but that's a different discussion. Here's a correct example of theme_image for drupal 7:

<?php

$arbitrary_array
= array(
 
'path' => '/sites/all/arbitrary_theme/images/image.jpg',
 
'alt' => 'Alternate Text',
 
'title' => 'Image Title',
 
'width' => '100px',
 
'height' => '50px',
 
'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
);
print
theme_image($arbitrary_array);
?>

Note that I used the 'print' command. theme_image, on its own, only produces code without outputting it.

D7: Why no defaults?

D7: There's lots of cross linking to other issues but I still don't really understand why all the options now have to be specified when you call theme_image() and I can't just pass in array('path' => $mypath')

Why does theme_image not default the parameters like most other theme functions?

quick update

So re-reading the notes on issue: http://drupal.org/node/1004556 and I see that it's recommended that we use theme('image', array('path' => $mypath)); instead of theme_image();

Can do as in my case, allows me to not pass in the other parameters and now I don't get the PHP notice messages.

This is an excellent question.

Drupal 7's API is more of a PI-- that is, not very advanced. For any image in the system, all the relevant information is stored in existing structures, but the API does not access any of it, and requires the coder to supply the most basic information. D7 seems to be falling victim to abstraction fetishism at the cost of everyday usability.

print render

in case any themers end up here as I did, in D7, it's as simple as <?php print render($content['field_mypic']); ?> to stick a fully rendered version of your image field in your tpl files.

Login or register to post comments