How do I output a drupal image field?

Alex C picture Alex C · Jun 21, 2012 · Viewed 48.7k times · Source

It is quite possible that I'm just looking for help finding the name of a function that already exists within drupal (7) but sometimes the documentation is a bit difficult to navigate. Hopefully someone can help me.

I have a node that has a custom field.

I am working within a template field--mycustomcontenttype.tpl.php and so am trying to find the name of the PHP function that outputs and image field with image styles.

mycustomcontenttype is a NODE with the following additional field:

[field_image] => Array
(
    [und] => Array
    (
        [0] => Array
        (
            [fid] => 51
            [alt] => ImageAltText
            [title] => 
            [width] => 150
            [height] => 150
            [uid] => 29
            [filename] => myimagename.jpg
            [uri] => public://myimagename.jpg
            [filemime] => image/jpeg
            [filesize] => 8812
            [status] => 1
            [timestamp] => 1339445372
            [uuid] => a088ea8f-ddf9-47d1-b013-19c8f8cada07
            [metatags] => Array
        (
    )
)

So I could display the image using an (ugly) hand rolled functions that takes the value found in $item['#options']['entity']->field_image and does the substitution of public:// for the actual server path, and then it's also possible that I'm going to want to load the image with the correct drupal image style (thumbnail, custom-style, etc...)

Sadly, I just have no idea what the name of the function that works something like: unknown_function_name_drupal_image_print($item['#options']['entity']->field_image, 'thumnail'); is.

Is there anyone who can help me find this?

Answer

SpaceBeers picture SpaceBeers · Jun 21, 2012

You are looking for image_style_url(style_name, image_url);

For example:

<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['filename']).'" />'?>

EDIT

As pointed out you can also set the image style in the Manage Display page for the content type and then output using render.

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