1

I am using this solution to print the image for a node in the html.tpl.php file. This works, except it has the full markup. I need just the url for the image. Any ideas of how to print this out?

For instance, I am using this:

 if ($node = menu_get_object() ) {
  if ($node && isset($node->nid)) {
    $node = node_load($node->nid);
    node_build_content($node);
    $variables['field_image'] = render($node->content['field_image']);
     } 
   }
3

If you just want the path to the original file you can use file_create_url() like this:

if ($node = menu_get_object()) {
  if ($items = field_get_items('node', $node, 'field_image')) {
    $variables['field_image_path'] = file_create_url($items[0]['uri']);
  }
}

Note that I've done away with some redundant if statements, the extra node_load (the get object call already loads the node) and the building of the node content.

If you want to use an image style insead of the original image you can use image_style_url() like this:

if ($node = menu_get_object()) {
  if ($items = field_get_items('node', $node, 'field_image')) {
    $variables['field_image_path'] = image_style_url('style_name', $items[0]['uri']);
  }
}

If you wanted to get the image style from the content type display settings you can do this using field_info_instance() like this (this example assumes a content type called "article" and a display mode of "default"):

if ($node = menu_get_object()) {
  if ($items = field_get_items('node', $node, 'field_image')) {
    // Set a default style name as fallback.
    $image_field_style = 'original';
    // Use the image style from the field settings if we have it.
    if ($image_field_instance = field_info_instance('node', 'field_image', 'article')) {
      if (isset($image_field_instance['display']['default']['settings']['image_style'])) {
        $image_field_style = $image_field_instance['display']['default']['settings']['image_style'];
      }
    }

    $variables['field_image_path'] = image_style_url($image_field_style, $items[0]['uri']);
  }
}
  • Actually, it's not working properly. When I use the first one, it only prints out the base url. When I try the second and third ones, it prints a url that say access denied. local.mysite.com:8888/sites/local.mysite.com/files/styles/340/… Looks like it's not the file, but rather the style. – HanSoloShotFirst Nov 26 '14 at 17:45
  • The second and third one do print the style. My answer specifically says that. The first one should print the non-style one. Can you give an example of the base url that the first one prints? – rooby Nov 26 '14 at 23:50
  • Yea. Sure. It just prints local.mysite.com:8888 It's odd. I wouldn't think this should be so hard by using the pre process function for html. Just to be clear, I am looking to print the full absolute path to an image field in a node where the meta tags go, much like the way the og:image gets printed out by the meta module. Much appreciated. – HanSoloShotFirst Nov 27 '14 at 4:11
  • Sorry I had an error on my code examples.It should have been $items[0]['uri'] instead of $items[0]->uri. - I have updated my answer. – rooby Nov 27 '14 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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