Drupal 7 Get Image Style Path

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().

$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.

// 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->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.