2

I'd like to know how to display a user avatar in Drupal 7. I want to display the avatar in full size, and as thumbnail.

2 Answers 2

3

The code used from user_user_view() is the following one:

$account->content['user_picture'] = array(
  '#markup' => theme('user_picture', array('account' => $account)), 
  '#weight' => -10,
);

For example, if you want to output the HTML code to show the picture associated with the currently logged-in user in a form, you could use code similar to the following one:

$form['user_picture'] = array(
  '#theme' => 'user_picture',
  '#account' => $GLOBALS['user'], 
);

This is similar to the following code used in node_admin_nodes(), which uses a different theme function.

$form['nodes'] = array(
  '#theme' => 'table', 
  '#header' => $header, 
  '#rows' => $options, 
  '#empty' => t('No content available.'),
);

The content of "#header," "#rows," and "#empty" are used to populate the content of the $variables array that is passed to the theme function ("#header" becomes "header," etc).

theme('user_picture') works even when the Image module (the one that defines theme_image_style()) is not enabled; in that case, it uses theme_image() instead. When the Image module is enabled, it uses the default style as contained in the Drupal variable "user_picture_style." In both the cases, the user image is a link to the user profile.

As the theme function is using a template (user-picture.tpl.php), the relevant code is in template_preprocess_user_picture(); if you look at the code for theme_user_picture(), you would see there is no code.

2
  • Thank you for the answer. Is it possible to change the size of the avatar: thumbnail, medium, large? I use this code: global $user; $account = user_load($user->uid); $account->content['user_picture'] = array( '#markup' => theme('user_picture', array('account' => $account)), '#weight' => -10, ); print ($account->content['user_picture']['#markup']); And I also don't understand why '#weight' => -10 is used here. I know what it means in form api.
    – Alexey
    Commented Aug 2, 2011 at 8:16
  • "#weight" is used for every form field to order them; in this case −10 would put the user image on top of the form. For the other question, I updated my answer.
    – avpaderno
    Commented Aug 2, 2011 at 14:19
2

A user's profile picture is available on the loaded user object. You can use image_style theme function to render an image in the size of your choosing.

1
  • where can i read how to use this function?
    – Alexey
    Commented Aug 1, 2011 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.