Display image in theme template

Public

Sometimes you need to call an image directly from your theme template if it isn't practical to do with css. This snippet works well for me.

Get raw version
php
  1. <?php global $base_path;
  2. print "<img src='" . $base_path . path_to_theme() . "/img/filename.png' />"; ?>

Comments

doubouil's picture

You can also use the theme image function like so :
Drupal 7

  1. print theme("image", array(
  2. "path" => path_to_theme() . "/img/filename.png",
  3. "alt" => "alt text",
  4. "title" => "title text",
  5. "attributes" => array(
  6. "class" => array("img")
  7. ) );

Drupal 6
  1. print theme("image",
  2. path_to_theme() . "/img/filename.png",
  3. "alt text",
  4. "title text",
  5. array("class" => "img")
  6. );

Theme functions allows themes and module to alter the output, in this case a CDN module could use a different base path.