Hopp til hovedinnhold

Drupal 7 image styles

Jørn Fauske
- 12 Jan 2016
Screenshot image style confirguration

If you need to get the url or print the image of a Drupal 7 image field with a specific image style you can do it like this:

$wrapper = entity_metadata_wrapper('node', $node);
$image_style = 'large';
$file = file_load($wrapper->field_image->file->fid->value());

// get the url
$url = image_style_url($image_style, $file->uri);

// or the html
$variables = array(
  'style_name' => $image_style,
  'path' => $file->uri,
  'height' => NULL,
  'width' => NULL,
  'alt' => $wrapper->field_image->alt->value(),
  'title' => $wrapper->field_image->title->value(),
$html = theme_image_style($variables); 

If you need to get the configured image-style for a node's view mode you could do something like this:

$view_mode = 'teaser';
$instance_info = field_info_instance('node', 'field_image', 'article');
// you should do some checks on the $instance_info array before using it like this
$image_style = $instance_info['display'][$view_mode]['settings']['style']; 

til toppen