Render image with media style applied

Public

Gets the imagecache image style for the selected image.

Get raw version
php
  1. /**
  2.  * Apply a media style to a node's image URI, and return that image's HTML tag
  3.  */
  4. function get_image_media_style($style, $field, $node){
  5. $l = isset($node->language) ? $node->language : LANGUAGE_NONE;
  6. if (!isset($node->{$field}[$l]['0']['uri'])) return '';
  7.  
  8. // Get the image URI from the node.
  9. $uri = $node->{$field}[$l]['0']['uri'];
  10.  
  11. // Get the image Title and Alt texts
  12. $alt = !empty($node->{$field}[$l]['0']['alt']) ? $node->{$field}[$l]['0']['alt'] : '';
  13. $title = !empty($node->{$field}[$l]['0']['title']) ? $node->{$field}[$l]['0']['title'] : '';
  14.  
  15. // Prepare and call the imagecache style
  16. $data = array(
  17. 'style_name' => $style,
  18. 'path' => $uri,
  19. 'width' => '',
  20. 'height' => '',
  21. 'alt' => $alt,
  22. 'title' => $title,
  23. );
  24.  
  25. // Generate and return the image source
  26. $image = theme_image_style($data);
  27. return $image;
  28. }

Comments

agoradesign's picture

Better solution would be to change the last line in:

$image = theme('image_style', $data);

Now custom theme overrides could be used