18

I am trying to theme a node with tpl and when i tried to print image , I cant find image path like in d6.. which function i have to call to properly output image .. I mean something like theme('')?

Array
(
    [und] => Array
        (
            [0] => Array
                (
                    [fid] => 13
                    [alt] => 
                    [title] => 
                    [width] => 416
                    [height] => 335
                    [uid] => 1
                    [filename] => Capture2.PNG
                    [uri] => public://Capture2.PNG
                    [filemime] => image/png
                    [filesize] => 215377
                    [status] => 1
                    [timestamp] => 1346837738
                    [rdf_mapping] => Array
                        (
                        )

                )

        )

)

5 Answers 5

37
<?php print render($content['field_image']); ?>

If you want to change how the image is displayed (dimensions, link, etc), set them in Manage Display tab in the node type settings.

You can also do an imagecache preset like this:

<?php
print theme('image_style', array('path' => $node->field_image[LANGUAGE_NONE][0]['uri'], 'style_name' => [STYLE NAME]));
?>

But that's not the recommended way!

if you want to build URL from URI,

<img src="<?php print file_create_url($node->field_image[LANGUAGE_NONE][0]['uri']); ?>" />
3
  • thanks, your answer covers all things . This should be in drupal.org documentation :)
    – Serjas
    Commented Sep 8, 2012 at 7:05
  • It's always a good idea to use LANGUAGE_NONE instead of 'und'
    – qasimzee
    Commented Dec 18, 2014 at 7:46
  • I know i'm beating a dead horse, but if you take the second approach, how can you assure you get a result that doesn't 404? Commented Dec 19, 2014 at 16:53
3

For those using the file_entity module (perhaps with the media module), you may be wondering how to programmatically render files/images:

$image = (object) $node->field_image[ LANGUAGE_NONE ][0];
$image_entity = file_view($image, "summary");
echo drupal_render($image_entity);

Where "field_image" is your field name, and "summary" is your view mode.

2
  • does work for me, but needed to set the style on $image_entity['file']['#style_name'] again ..
    – rémy
    Commented Mar 18, 2014 at 15:28
  • You shouldn't hardcode the language key.
    – AlxVallejo
    Commented Aug 14, 2014 at 17:08
1

If you want to simply render the image with the image style set for that image in manage display: Just type <?php print_render($content['field_image']) ?>

If you want to display image with any other image style you have SUPPOSE : 'sales_album' then type:

list($albumImage) = field_get_items('node', $album, 'uc_product_image');

$albumImageUrl = file_create_url($albumImage['uri']);

$style_array = array('path' => $albumImage['uri'], 'style_name' => 'sales_album');

$render_album_image = theme('image_style', $style_array);

print $render_album_image;
1
$img_url = $node->field_name['und'][$i]['uri'];
print image_style_url("style_name", $img_url);

$i is in case you have multiple images to display. You can use a for loop like:

for($i=0;$i < $imageCount; $i++) { /*the above code*/ }

and $imageCount is basically declared above for loop as

$images = array();<br>
$imageCount= count($node->field_image['und']);

Hope this helps!

1

When using the field module, I found this was better:

in page--yourcontenttype.tpl.php:

<?php
  $this_field_image = field_view_field('node',$node,'field_image');
  print render($this_field_image);?>
?>

using field_view_field() provides additional advantages whereby one can set an array of display settings:

<?php
  $hide_labels = array('label'=>'hidden');
  $this_field_image = field_view_field('node',$node,'field_image', $hide_labels);
  print render($this_field_image);?>
?>

https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x

Not the answer you're looking for? Browse other questions tagged or ask your own question.