stovroz
stovroz

Reputation: 7065

How do I get the url for a Drupal image, taking into account its style?

In a Drupal 7 .tpl.php file, given an image $some_node->field_image, how do I fetch the appropriate URL for use in an img src attribute, taking into account the image's style as set in its node's content type's display settings? I have seen the following suggestion, but without any indication where $style is coming from:

image_style_url($style, $some_node->field_image['und'][0]['uri']);

(the incumbent views are full of these foo['und'][0]bars by the way, is this really how it's done in Drupal, it seems ridiculous?)

Upvotes: 0

Views: 2538

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27013

The $style is the style name (machine name) of the style you want to apply to the image. You can find all the image styles at ?q=admin/config/media/image-styles.

See image_style_url for more details.

Example:

image_style_url('large', $some_node->field_image['und'][0]['uri']);

Update

Based on your last comment. I'd suggest you to use field_view_field.

Example:

 $node = node_load(12);
 print render(field_view_field('node', $node, 'YOUR_IMAGE_FIELD_NAME'));

Upvotes: 3

Related Questions