coder1.com

  • home
  • drupal articles
  • contact
Home

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

  1. <?php
  2. $path = image_style_url('home_rotator', $node->field_slideshow_image[LANGUAGE_NONE][0]['uri']);
  3. ?>

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.

  1. <?php
  2. // build query to get latest 10 article node ids
  3. $query = db_select('node', 'n');
  4. $query->condition('n.type', 'article');
  5. $query->condition('n.status', 1);
  6. $query->orderBy('n.created');
  7. $query->range(0, 10);
  8. $nids = $query->execute()->fetchCol(0);
  9.  
  10. // load the nodes
  11. $nodes = node_load_multiple($nids);
  12.  
  13. // at this point, if you went to display an imagefield associated with this
  14. // article, it would not render.
  15.  
  16. // call this to prepare your nodes and it will set the default image if an image doesn't exist
  17. field_attach_prepare_view('node', $nodes, 'full');
  18.  
  19. // and now you can use the image paths as mentioned in the first code snippet.
  20. ?>

  • Drupal 7
  • Image
  • Imagecache

It is a good practice to use

Juampy (not verified) — January 10, 2012 - 3:34am

It is a good practice to use LANGUAGE_NONE instead of 'und'.

  • reply

Good catch, thanks.

Mike Milano — January 30, 2012 - 7:32pm

Good catch, thanks.

  • reply

Wow! Really this is

Rajesh Ranjan (not verified) — March 15, 2011 - 8:24am

Wow! Really this is excellent, I want the exact thanks a lot!

  • reply

Thanks for posting this was

s (not verified) — February 15, 2011 - 9:42pm

Thanks for posting this was what I needed but couldn't find it in the documentation and my newness to Drupal didn't help. Thanks!

  • reply

This was really, really

yskel (not verified) — January 27, 2011 - 6:48pm

This was really, really helpful - thanks for posting.

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <img> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6> <h7>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Poll

Have you used any NoSQL Databases?:

User login

  • Request new password

Navigation

  • Recent posts

  • home
  • drupal articles
  • contact