Search code examples
drupaluid

How can I display user avatar by knowing user ID(uid) in Drupal?


I want to display user picture if the user id is known. Is possible to do it without using additional modules?


Solution

  • If you know the user id you want to get the picture of you can use user_load(). user_load() will load a user object full of a user's information. To find the structure of the user object, you can use print_r().

    Example for Drupal 6:

    <?php
      $account = user_load(10);
      print theme('image', $account->picture, 'User Avatar', $account->name . "'s Avatar");
    ?>
    

    There is also more you can do with theme_image(), like adding attributes to the img tag, etc.

    If you are looking for the picture of the currently logged in user, you don't even need to use user_load() or the uid, simply do the following:

    <?php
      global $user;
      print theme('image', $user->picture, 'User Avatar', $user->name . "'s Avatar");
    ?>
    

    These examples were for Drupal 6 (since no version was specified) but there are only slight changes for Drupal 7.