21

It is quite possible that I'm just looking for help finding the name of a function that already exists within drupal (7) but sometimes the documentation is a bit difficult to navigate. Hopefully someone can help me.

I have a node that has a custom field.

I am working within a template field--mycustomcontenttype.tpl.php and so am trying to find the name of the PHP function that outputs and image field with image styles.

mycustomcontenttype is a NODE with the following additional field:

[field_image] => Array
(
    [und] => Array
    (
        [0] => Array
        (
            [fid] => 51
            [alt] => ImageAltText
            [title] => 
            [width] => 150
            [height] => 150
            [uid] => 29
            [filename] => myimagename.jpg
            [uri] => public://myimagename.jpg
            [filemime] => image/jpeg
            [filesize] => 8812
            [status] => 1
            [timestamp] => 1339445372
            [uuid] => a088ea8f-ddf9-47d1-b013-19c8f8cada07
            [metatags] => Array
        (
    )
)

So I could display the image using an (ugly) hand rolled functions that takes the value found in $item['#options']['entity']->field_image and does the substitution of public:// for the actual server path, and then it's also possible that I'm going to want to load the image with the correct drupal image style (thumbnail, custom-style, etc...)

Sadly, I just have no idea what the name of the function that works something like: unknown_function_name_drupal_image_print($item['#options']['entity']->field_image, 'thumnail'); is.

Is there anyone who can help me find this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alex C
  • 16,624
  • 18
  • 66
  • 98

10 Answers10

23

You are looking for image_style_url(style_name, image_url);

For example:

<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['filename']).'" />'?>

EDIT

As pointed out you can also set the image style in the Manage Display page for the content type and then output using render.

<?php print render($content['field_image']); ?>
Wtower
  • 18,848
  • 11
  • 103
  • 80
SpaceBeers
  • 13,617
  • 6
  • 47
  • 61
  • I agree with this, though I would say it is better render the image using Drupals theme layer http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_image/7 – Web Assistant Jun 21 '12 at 16:36
  • I totally agree, but the question seemed to be looking for a function that takes the image style in which this does. I've edited to include the render option though. – SpaceBeers Jun 21 '12 at 17:28
14

I would do this using a combination of field_get_items and field_view_value.

For example:

    <?php

    // In some preprocessor...
    $images = field_get_items('node', $node, 'field_image');
    if(!empty($images)) {
      $image = field_view_value('node', $node, 'field_image', $images[0], array(
        'type' => 'image',
        'settings' => array(
          'image_style' => 'custom_image_style' // could be 'thumbnail'
        )
      )
     );
    }
    $variables['image'] = $image;

    // Now, in your .tpl.php
    <?php print render($image); ?>

I have a write up about field access and this very thing here.

Good luck.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
mavame
  • 399
  • 4
  • 12
  • 2
    This is great! However (for Drupal 7 at least), the call to field_view_value should be: field_view_value('node', $node, 'field_image', $images[0], array( 'type' => 'image', 'settings' => array( 'image_style' => 'custom_image_style' // could be 'thumbnail' ) ); ... you need to include the field name. – Chadwick Wood May 22 '13 at 16:19
  • Is there any way to css class to the field wrapper too?! – SaidbakR Sep 06 '15 at 05:44
  • this worked for me and seems to respect the languages module oob – Thomas Fellinger Apr 29 '16 at 14:35
  • heres the code i got in the node.tpl.php: field_image) ) { $node_image = field_get_items('node', $node, 'field_image'); ?> – Thomas Fellinger Apr 29 '16 at 14:37
12

SpaceBeers' answer is correct - you can print the image in that way. But in Drupal side, it's bad. You have language undefined, and directly using only the first field if it's a multi-value field. Also, you are hardcoding some of the Drupal's nice stuff such as changing image style using the UI.

<?php print render($content['field_image']); ?>

This will print the image with proper dimensions (in img tag), alt tags, and always respects what you have set in Manage Display tab of the mycustomcontenttype node type.

AKS
  • 4,618
  • 2
  • 29
  • 48
  • I've amended my answer to include this. I assumed they knew this already and wanted to be able to output the image in a different style to the default one. – SpaceBeers Jun 21 '12 at 18:14
8

I use this:

print theme('image_style', array('path' => [image uri from field], 'style_name' => [image style]));

You can also include an "attributes" array variable that contains elements for (eg) class, alt & title. I think this is a better option for allowing your choice of image style, while still using Drupal's (or your theme's) default image rendering.

Heather Gaye
  • 1,118
  • 1
  • 11
  • 19
3

You should use

<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['uri']).'" />'?>

instead of

<?='<img src="'.image_style_url('fullwidth', $node->field_page_image['und'][0]['filename']).'" />'?>
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Denis
  • 1,167
  • 1
  • 10
  • 30
1

I think Ayesh K's answer is the preferred Drupal way of printing a field to the page. Much easier to read, and keeps any operations / presets you do to your Image in the UI.

You can break it up, but it seems like that would be for narrow use cases.

ricardoom
  • 71
  • 1
  • 9
1
<?php
global $base_url;
$nid=6;//node id
$node=node_load($nid);
echo '<div style="margin-right:350px;">';
echo "<marquee>";
foreach($node->field_image_gallery['und'] as $v){
$image=$v['uri'];
$image_path=explode("public://",$image);
$url_path=$base_url.'/sites/default/files/'.$image_path[1];
echo '<img src="'.$url_path.'" width="100" height="100">&nbsp;';
 }

echo "</marquee>";
echo "</div>";
?>
gangadhara
  • 11
  • 1
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. (from [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)) – kowsky Apr 19 '17 at 07:19
0

this can be done like that :

   $style='full_width';
   $path=$node->my_img_field['und']['0']['uri'];
   $style_url = image_style_url($style, $path);
   print "<img src=".file_create_url($style_url)." >";
Matoeil
  • 6,851
  • 11
  • 54
  • 77
0
 $field_image = field_view_field('node', $promoNode, 'field_image');
 $variables['promo_image'] = render($field_image);

Renders:

<div class="field field--name-field-image field--type-image field--label-above">
  <div class="field__label">Image:&nbsp;</div>
  <div class="field__items">
      <div class="field__item even">
        <img typeof="foaf:Image" 
             src="http://domain/sites/domain/files/promo1.png"
             width="360" 
             height="301"
             alt="Promotional Image"
             title="Promotional Image Title">
      </div>
  </div>
</div>

Note: The label-above class could be removed with the content-type Manage-Display settings of the fields, by setting the Label to , but that isn't working for me right now so I'm simply using CSS to hide the .field__label class.

Eduardo Chongkan
  • 752
  • 7
  • 12
-2

This worked for me for adding an image to my search results page:

<?php if ($result['node']-> field_image): ?>
  <?php
  $search_image_uri = $result['node']-> field_image['und'][0]['uri'];
  // assuming that the uri starts with "public://"
  $search_image_locate = explode('//', $search_image_uri);
  $search_image_filepath = '/sites/actual/path/to/public/' . $search_image_locate[1];
  ?>
  <div class="search-image">
    <img src="<?php print $search_image_filepath; ?>" />
  </div>
<?php endif; ?>