Hey there,

Anyone know how to pull the avatar of a user as you are looking at their profile, but in a block? I'm not a PHP wiz at all, and I've only figured out a way to print the logged in users picture. Here's the code I have at the moment. Please disregard the sloppiness; the first part is the unfunctional part. Would a db_query be what I need?

<?php  global $user; ?>
<?php if (arg(0) == 'user' && $user->uid != arg(1)) : ?>
<div class="userpic2"><?php print theme('user_picture', $user->uid); ?></div>
<?php elseif ($user->uid) : ?>
<div class="userpic"><?php print theme('user_picture', $user); ?></div>
<?php else : ?>
<div class="userpic2"><img src="http://tenning.com/themes/box_grey/noaccount.png" /></div>
<?php endif; ?>

Thanks in advance!

Comments

dakku’s picture

Hi,
I think you are trying to do something silimar to this:
http://www.themes4urmobilephone.com/user/dakku
http://www.themes4urmobilephone.com/user/admin

Although I am not displaying the image in a block, but that is farily easy to do anyway. Here is how I would go about doing what you need:

Route 1: (harder, but more flexible)
1) write your own module that can generate a block, see the handbook on code to generate a block from inside of a module.
2) Use the hook_user to add avatar image code to the user
3) add your costom code to the block above

Route 2: (easy)
1) Alternatively, create a block using drupal's admin/build/block/add

2) Copy paste the code into your block:

<?php  global $user; ?>
<?php if (arg(0) == 'user' && $user->uid != arg(1)) : ?>
<div class="userpic2"><?php print theme('user_picture', $user->uid); ?></div>
<?php elseif ($user->uid) : ?>
<div class="userpic"><?php print theme('user_picture', $user); ?></div>
<?php else : ?>
<div class="userpic2"><img src="http://tenning.com/themes/box_grey/noaccount.png" /></div>
<?php endif; ?>

3) enable the block on "user/*" pages.

Post here if you need more help

juicestain’s picture

Sorry, I'll be a bit more clear. I wrote my plea late last night.

Basically I want to have your avatar when you're logged in the upper right corner of the page. However, I want this to change to display the avatar of a different user when you are viewing their profile.

The code works except for

<?php print theme('user_picture', $user->uid); ?></div>

which does not return the avatar of another user when on their profile.

dakku’s picture

ahhh..
you see $user is the object for the user that is logged in (ie YOU). If you need to display the avatar per user profile you need to do something like:

<?php
// load the user object for the user in the profile
if(arg(0) == "user"){
  $uid = $arg(1);
  print theme('user_picture', $uid);
}
?>

The above assumes that you are not using pathauto to alias the user profiles. if you are you will need to do a lookup on the alias table to get to the uid firstly!!

Let me know if you need more help.

juicestain’s picture

great, so you've pinpointed my problem now.

i am using pathauto to alias the user profile. sorry for all the trouble, but could you point me to how to grab the uid from the generated alias? thank you!

juicestain’s picture

i think the solution lies a line of code that is able to extract/load a user from the uid supplied in arg(1). i wish my php skills were better...

dakku’s picture

hiya again,
if you are using path auto, here is the complete code you will need

<?php

// load the user object for the user in the profile
if(arg(0) == "user"){
  $source = drupal_lookup_path("source", arg(0)."/".arg(1));
  $uid = str_replace("user/", "", $source);
  print theme('user_picture', $uid);
}
else{
  print theme('user_picture', $user->uid);
}
?>

good luck

juicestain’s picture

i couldn't get your code to work, but i did manage to get it working with the following code for anyone who may be interested:

<?php  global $user; ?>
<?php if (arg(0) == 'user' && $user->uid != arg(1)) : ?>
<?php
    $userid = arg(1);
    $result = db_query("SELECT u.uid, u.name
    FROM {users} u
    WHERE u.uid = %d", $userid);
    $u = db_fetch_object($result);
?>
<div class="userpic2"><?php print theme('user_picture', $u); ?></div>
<?php elseif ($user->uid) : ?>
<div class="userpic"><?php print theme('user_picture', $user); ?></div>
<?php else : ?>
<div class="userpic2"><img src="http://tenning.com/themes/box_grey/noaccount.png" /></div>
<?php endif; ?>

thanks for the suggestions..

gonz’s picture

This was exactly what I was looking for.

- Much Appreciated

thepaul’s picture

I am trying to do this exact same thing, but your code isn't working for me. It always displays the default avatar (as if the user hasn't uploaded their own) .. no matter what.

Is there something that I need to tweak with the above code so that it works with my version of Drupal (5.6) ?

Chippe’s picture

Seems like it only works if you're logged in as an admin.

Just put in u.picture in to the code and it will work for all users.

Manamarak’s picture

Where is the u.picture supposed to go?

Chippe’s picture

After u.name like this: $result = db_query("SELECT u.uid, u.name, u.picture

Manamarak’s picture

Sorry Chippe, that was obvious

Thanks for your help.

Summit’s picture

Bookmarking, greetings, Martijn

pribeh’s picture

How would I alter this to display the $account's picture of the user profile being displayed. Can't figure it out....

dalad’s picture

For the ones who google this thread, or for you if you didn't find the solution yet:
The best way is to use user_load. This way you don't have to deal with the query. Assuming the path is something like ?q=user/%

$owner = user_load(arg(1));
print theme('user_picture', $owner);
tnt2000’s picture

Is it possible to implement imageCache profiles to this code?

<?php  global $user; ?>
<?php if (arg(0) == 'user' && $user->uid != arg(1)) : ?>
<?php
    $userid = arg(1);
    $result = db_query("SELECT u.uid, u.name
    FROM {users} u
    WHERE u.uid = %d", $userid);
    $u = db_fetch_object($result);
?>
<div class="userpic2"><?php print theme('user_picture', $u); ?></div>
<?php elseif ($user->uid) : ?>
<div class="userpic"><?php print theme('user_picture', $user); ?></div>
<?php else : ?>
<div class="userpic2"><img src="http://tenning.com/themes/box_grey/noaccount.png" /></div>
<?php endif; ?>

thekayra’s picture

I think this one works the best. Thanks dalad.

ericinwisconsin’s picture

It BASICALLY works for me, dalad, but I get this error:

warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /var/www/websites/drupal6/sites/all/modules/imagecache_profiles/imagecache_profiles.module on line 41.

Any ideas, anyone?

glitz’s picture

im using

$owner = user_load(arg(1));
print theme('user_picture', $owner);

in a block using php input filter. Does not seem to work. Has anyone got this to work? Im trying to display the picture of the user who's profile is being viewed in a block.

thanks

mikeejt’s picture

I think the previous code is deprecated. Declare the global variable $user which will get drupal's global user (currently logged in), then call print theme(...:

<?php
global $user;
print theme('user_picture', $user->uid);
...

This will print the user's pic/avatar