I'm stumped. How the heck do I display the currently logged in user's profile image? I'm trying to add this to a block that shows for authenticated users and everything I try just doesn't display anything at all.

Comments

ryivhnn’s picture

echo $picture; or echo theme("user_picture", $user);

For a block I stuck this in the php textarea of a new block:

<?php global $user; ?>

<h2><?php echo $user -> name; ?></h2>
<?php echo theme("user_picture", $user); ?>

and tick "authnticated users" to get it to show only to auth users.

Hope I deciphered the question correctly :)

works at bekandloz | plays at technonaturalist

nancydru’s picture

<?php
 global $user; ?>
 echo '<h2>'. $user -> name .'</h2>';
 echo theme("user_picture", $user);
 ?>

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

ryivhnn’s picture

Apologies, terrible editing on my part :)

works at bekandloz | plays at technonaturalist

jormiguel’s picture

global $user;
echo '<h2>'. $user -> name .'</h2>';
echo theme("user_picture", $user);

in line 2 the ?> is wrong.

ps: sorry my poor english.

nancydru’s picture

I noticed another small boo-boo: there are no spaces in $user->name.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

ryivhnn’s picture

Drat, I habitually put spaces in things like that as it's easier for me to read and it hasn't complained yet.

I'd stop coding in the middle of the night if it wasn't the only time I had to do it :)

works at bekandloz | plays at technonaturalist

prattboy’s picture

Excellent! Works great; thank you.

demon326’s picture

is ist possibel to have this:

user avatar
make content
my account
admin..
and all my other custom links

is this possibel if so how can i do that?

Greetz Demon

prattboy’s picture

Yes; try creating a block that is visible only to authenticated users that contains php markup similar to this:

<?php global $user; ?> <!--declare global user info -->
<?php echo theme("user_picture", $user); ?> <!-- display avatar -->
<a href="<?php print base_path() ?>node/add">Add Content</a><!-- add content... a link to a static admin function -->
<a href="<?php print base_path() ?>user/<?php echo $user->uid; ?>">My Profile</a><!--show profile... determines user id of currently logged in user -->

It's very, very important that the filter you use is PHP and not HTML or filtered HTML.

lias’s picture

This works great for current logged in user.
How could this be modified to show the last 5 users who've logged in? Using views for something like this seems to intensive maybe?

thanks!

nancydru’s picture

<?php
  $output = '<center>';
  $result = db_query('SELECT * FROM {users} u WHERE uid > 1 ORDER BY u.access DESC LIMIT 5');
  while ($user = db_fetch_array($result)) {
    $output .= l($user['name'], 'user/'. $user['uid']) .'<br/>';
    if ($user['picture']) { $output .= '<img src="/'. $user['picture'] .'"/><br/>'; }
    else { $output .= t('No picture available') . '<br/>'; }
  }
  echo $output .'</center>';
?>

This should be pretty close to what you want. Note that the query excludes anonymous (user/0) and superuser (user/1).

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

lias’s picture

This almost works Nancy, it does display the last user names but because I have my file download method set to private it fills in the base path as :

//home/user/files/pictures/picture-3.gif

instead of as:

http://domain.com/system/files/pictures/picture-3.gif

so my pictures don't end up displaying. What do I use to modify this string?

if ($user['picture']) { $output .= '<img src="/'. $user['picture'] .'"/><br/>'; }

How can I get it to just output the user's picture file name so that I can just add this?

<img src="http://www.domain.com/system/files/pictures/'. $user['picture'] .'"/><br/>'; }

I'm not sure how to change this $user['picture'] to get just the picture-3.gif file name to be added to the end of the img src url.

Thank you very much for your help.

nancydru’s picture

<img src="http://www.domain.com/system/files/pictures/'. basename($user['picture']) .'"/><br/>'; }

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

lias’s picture

Thank you so much for that and the basename($user['picture']) part would've left me stumped for days.

I ended up using the following code which generates the file path automatically so I don't have to hardcode it.

/* works with 5.1 to display last logged in user and picture
* works with private file downloads directory set above root
*/
  $output = '<center>';
  $result = db_query('SELECT * FROM {users} u WHERE uid > 1 ORDER BY u.access DESC LIMIT 5');
  while ($user = db_fetch_array($result)) {
    $output .= l($user['name'], 'user/'. $user['uid']) .'<br/>';
    if ($user['picture']) { $output .= '<img src="'. file_create_url($file->filepath) . 'pictures/' . basename($user['picture']) .'"/><br/>'; }
    else { $output .= t('No picture available') . '<br/>'; }
  }
  echo $output .'</center>';

Thanks again for your help : )

scarer’s picture

Hi,

I had a look through the user module and couldn't seem to find the bit of code that spits out the most recent people who have joined the site.

I wanted to create a block that shows little pictures of the people who have recently joined the site instead of just a link with their name (as shown in the default block).

Any help would be great.

Cheers,

Sarah

nancydru’s picture

I can't tell you to hack a core module. But perhaps you can find a way to make the "theme_user_list" do what you want. Supposedly this can be modified in your site's theme.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

nayebare’s picture

thanks a lot i have used most of your tutorials and they have been very helpful
though i can't display an image to currently logged in user in my system please assist.
like the way face book does it
i have a database with the user's pictures and i need to display indiviual pictures to the user after
logging in thank you.

nancydru’s picture

I'm getting kind of tired of people thinking that Facebook should be imitated.

Where are these pictures in your database? Standard Drupal is to enter them into your user record at sign-up or later by editing your account. The url of the picture is then saved in the specific user's row in the User table of the database. If it is not, then you'll have to provide a hook_user('load',...) function in a module.

Manohar Papasani’s picture

where should i paste the above script specified

ryivhnn’s picture

The ones that don't go into blocks probably go in a *.tpl.php.

works at bekandloz | plays at technonaturalist