Drupal 7 Get image field path

2019-03-08 16:47发布

I would like to get the image path of a field. I'm in a node and I need the Url of image in order to put it as a background-image in inline css. I can't find the solution. Can you help me?

5条回答
我想做一个坏孩纸
2楼-- · 2019-03-08 16:58

I sometime use this:

$node = node_load($nid);
$img_url = $node->field_userimage['und'][0]['uri'];

<img src="<?php print image_style_url($style, $img_url) ?>" />
查看更多
女痞
3楼-- · 2019-03-08 17:05

To get just the path of an image from it's URI:

file_create_url($node->field_image['und'][0]['uri']);

More information on file_create_url() can be found here: http://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7

To get the path of an image created using an image style from it's URI use:

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

More information on image_style_url() can be found here: http://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7

查看更多
姐就是有狂的资本
4楼-- · 2019-03-08 17:09

to get image style url:

$field = field_get_items('node', $node, 'field_image');
$image_url = image_style_url('landingpage_slider', $field[0]['uri']);
查看更多
5楼-- · 2019-03-08 17:17

You also can use the Image URL Formatter module. It allows to change the field format in order to get only the URL:

Drupal field settings

Bonus: it works with Views as well:

enter image description here

查看更多
倾城 Initia
6楼-- · 2019-03-08 17:19

I am afraid, none of the solutions above are correct. They don't follow Drupal standards.

// field_video_image is the name of the image field

// using field_get_items() you can get the field values (respecting multilingual setup)
$field_video_image = field_get_items('node', $node, 'field_video_image');

// after you have the values, you can get the image URL (you can use foreach here)
$image_url = file_create_url($field_video_image[0]['uri']);

More details here: http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

查看更多
登录 后发表回答