1

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

Alexey
  • 7,127
  • 9
  • 57
  • 94

1 Answers1

3

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.

Laxman13
  • 5,226
  • 3
  • 23
  • 27
  • Thank you for response. Now I'm starting to understand how the the things work. But if i want to display the list of user avatars? And I don't want to use theme function. I want to send image to the template file. What should i do? P.S. I'm working with drupal 7 – Alexey Jul 16 '11 at 07:52
  • Send from .module to .php.tpl file. Template file gets info from module file. That's why i said that. I don't want to use Views. I want to do everything by my own module without external ones. – Alexey Jul 17 '11 at 21:28
  • If you want to send information from a module to a `.tpl` file you will have to use `hook_theme()`: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_theme/6 – Laxman13 Jul 17 '11 at 21:59
  • I managed to display user image in Drupal 7 using: user_load(1)));?> But how can i change style of the image? (e.g. change the size of the avatar)? And how can i display other parameters (uid, name and other items of user table)? – Alexey Jul 18 '11 at 11:49
  • Use the `$user` variable to show other user information, ie `print $user->uid; print $user->name;` etc. If you are looking to change the size of the image and are _set_ on using `theme_user_picture()` (which really just uses `theme_image()` unless have image module) you can override `template_preprocess_user_picture()`: http://api.drupal.org/api/drupal/modules--user--user.module/function/template_preprocess_user_picture/7 in your template.php – Laxman13 Jul 18 '11 at 14:18
  • Ok thank you for response. I did it in this way: $account = user_load(1); print ''.$account->name.'';} – Alexey Jul 18 '11 at 14:50
  • Now i have another problem. I want to get acces to the drupal function from external php file: $account = user_load(1) But i got the error: Call to undefined function user_load(). How can i use this function in the external file? – Alexey Jul 18 '11 at 14:52
  • If you are going to do it that way, you should just do `$account = user_load(1); $variables = array('path' => 'sites/default/files/pictures/'.$account->picture->filename', 'width' => 'auto', 'height' => '50px', 'alt' => $account->name); print theme('image', $variables);` – Laxman13 Jul 18 '11 at 14:57
  • Take a look at http://stackoverflow.com/questions/6517813/calling-drupal-functions-in-external-php-file for the answer to your other question. Please do not ask more than one question per "question". – Laxman13 Jul 18 '11 at 14:59