Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output a drupal image field?

Tags:

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?

like image 381
Alex C Avatar asked Jun 21 '12 15:06

Alex C


People also ask

How do I get an image from a field in Drupal 8?

You can use the folowing methods to get the uri or the url: $entity->get('field_image')->entity->getFileUri(); $entity->get('field_image')->entity->url();

How do I display images in Drupal 9?

Choose Administer > Structure > Content types > Manage display. Choose the display (e.g. "default" or "teaser") via the tab buttons. Click the gear wheel button to choose an image style; you are also able to link the image to the node. Save the settings.

How do you modify the display settings of your image field in Drupal?

Go to Structure -> “Content types”(admin/structure/types) and click on “manage display” within the Article row. 2. Select “Rendered file” from the Format drop-down list within the Image field and click on Save. That's all we need to change on the Image field, next we'll configure the Image file entity display.

Where are Drupal images stored?

On the File System page, the top field is named Public file system path. The content of this field contains the path where the images are uploaded into your Drupal 7 structure. The default location is sites/default/files.


1 Answers

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']); ?>
like image 82
SpaceBeers Avatar answered Oct 30 '22 18:10

SpaceBeers