Drupal - How do I get the correct URL of an image field?

You want to use the drupal_realpath() function to convert the URI into the actual filesystem path.

echo drupal_realpath($field_company_logo['und'][0]['uri']);

You could also use file_create_url() to get the actual URL to the image, as opposed to a local filepath.

And the correct way to render an image is to run it through theme_image():

echo theme('image', array(
  'path' => file_create_url($field_company_logo['und'][0]['uri']),
  'alt' => check_plain($company[0]['#markup']),
));

Also, accessing field data the way you're doing it isn't the preferred method. Have a look at field_get_items() and the detailed example in the Rendering Drupal Fields (the right way) blog entry.


If you literally just want to transform a URI into an actual URL, then you want the file_create_url() function, which is used like this:

$url = file_create_url($uri);

But you might find it easier to use a theme function, like Yuriy Babenko suggests:

$image_vars = array(
  'path' => $field_company_logo['und'][0]['uri'],
  'alt' => $company[0]['#markup'],
);
echo theme('image', $image_vars);

You can easily adapt this to use theme_image_style() instead, if you want a sized derivative of your image.


You 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

Tags:

Media

Entities