Drupal 7 Get Image Style Path
Mike Milano — September 8, 2010 - 11:24pm
Today I was working with the new image styles (imagecache) in Drupal 7.
I created a content type with an image field and needed to embed the image style path inside custom HTML for a JQuery slideshow. That was the reason I needed to get the path vs using the theme image function.
The trick was using image_style_url().
<?php $path = image_style_url('home_rotator', $node->field_slideshow_image[LANGUAGE_NONE][0]['uri']); ?>
A problem you might come across is the default images assigned to the imagefield not loading. You can call field_attach_prepare_view() to remedy this though. Here's a typical scenario.
<?php // build query to get latest 10 article node ids $query = db_select('node', 'n'); $query->condition('n.type', 'article'); $query->condition('n.status', 1); $query->orderBy('n.created'); $query->range(0, 10); $nids = $query->execute()->fetchCol(0); // load the nodes $nodes = node_load_multiple($nids); // at this point, if you went to display an imagefield associated with this // article, it would not render. // call this to prepare your nodes and it will set the default image if an image doesn't exist field_attach_prepare_view('node', $nodes, 'full'); // and now you can use the image paths as mentioned in the first code snippet. ?>

It is a good practice to use
Juampy (not verified) — January 10, 2012 - 3:34amIt is a good practice to use LANGUAGE_NONE instead of 'und'.
Good catch, thanks.
Mike Milano — January 30, 2012 - 7:32pmGood catch, thanks.
Wow! Really this is
Rajesh Ranjan (not verified) — March 15, 2011 - 8:24amWow! Really this is excellent, I want the exact thanks a lot!
Thanks for posting this was
s (not verified) — February 15, 2011 - 9:42pmThanks for posting this was what I needed but couldn't find it in the documentation and my newness to Drupal didn't help. Thanks!
This was really, really
yskel (not verified) — January 27, 2011 - 6:48pmThis was really, really helpful - thanks for posting.
Post new comment