Block that displays user avatars

dimitris_s - October 19, 2007 - 19:54

Hi people,

I'm searching for a custom block that will display the last e.g 20 pictures[avatars] resized. So the block will display 20 user pictures, resized to, let's say, 40x40.

That's the only thing I want. I searched for some custom snippets but none of them did my work. Also the avatar gallery module could not resize the pictures, I don't know why.

I think that this is quite a "simple" php work. I tried coding it myself but my php knowledge is far smaller compared to most of you in here.

Any help greatly appreciated. :)

Until now I came up with

dimitris_s - October 19, 2007 - 22:03

Until now I came up with this :

<div align=justify>
<?php
$limit
= 20;
$default_picture = variable_get('user_picture_default', '');
$result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', 0, $limit);
while (
$account = db_fetch_object($result)) {
  if (
$account->picture){
   
$output .= '<img src="/'.$account->picture.'" alt = "avatar" width="36" height="36" />';
  } else if ((!
$account->picture) AND ($default_picture)){
   
$output .= '<img src="'.$default_picture.'" alt = "avatar" width="36" height="36" />';
  }
}

return
$output;
?>

</div>

The only thing that is missing, is that I want a link to user profiles in every picture and quite a better formatting. Something like with 4 rows and 5 columns..

Does anyone know anything about this?

http://www.dobro.gr - Online community for Greek students in Bulgaria
http://simpledesigns.hopto.org - simple designs

You're on the right track

yelvington - October 19, 2007 - 23:28

We do something similar. In our case, only users with valid avatars make the cut. Note the use of divs to tie the image and the username link together. CSS is used to handle the layout; the avatar class is declared as inline, so it flows. We're linking the images to the user page, but not linking the usernames.

Another version of this code selected from users who were active this week. I just noticed our programmer forgot that clause. :-)

<?php
        $sql
= "SELECT u.uid, u.name, u.picture FROM {users} u WHERE picture!='' ORDER BY RAND() LIMIT 6";
       
$result = db_query($sql);

        while(
$user_data = db_fetch_object($result)) {
           
// truncate long usernames
           
$name = $user_data->name;
            if (
strlen($name) > 9) {
               
$name = substr($name, 0, 8) . "...";
                }
            print
"<div class=\"avatar\">\n";
            print
"    <a href=\"/user/$user_data->uid\"><img src=\"$user_data->picture\" \></a><br />\n";
            print
"$name\n";
            print
"</div>\n";
        }
   
?>

Thanks for the reply. I'm

dimitris_s - October 20, 2007 - 02:36

Thanks for the reply. I'm still trying to figure out, though, how to link to user profiles using my php code. It simply doesn't work if i add your <a> tag.

Any help please?

edit : I tried now with this line :

$output .= '<a href="' . $root . url("user/" . $uid) . '"><img src="'.$default_picture.'" alt = "avatar" width="36" height="36" /></a>';

and it does give me a valid but not correct link. It gives me a http://www.site.gr/user/ link, without the uid.

Help... :(

http://www.dobro.gr - Online community for Greek students in Bulgaria
http://simpledesigns.hopto.org - simple designs

Anyone?

dimitris_s - October 20, 2007 - 13:09

Anyone?

edit : OK I've found it.

http://www.dobro.gr - Online community for Greek students in Bulgaria
http://simpledesigns.hopto.org - simple designs

What exactly...

latte - October 21, 2007 - 04:01

What exactly did you do to get it to work?

It's driving me crazy!

Latte/

this worked great

interfaced - November 30, 2007 - 04:36

One correction,

print " uid\">picture\" \>
\n";

should be

print " uid\">picture\" />
\n";

This is the code that worked

arbel - December 21, 2007 - 17:44

This is the code that worked for me:

<?php
        $sql
= "SELECT u.uid, u.name, u.picture FROM {users} u WHERE picture!='' ORDER BY RAND() LIMIT 6";
       
$result = db_query($sql);

        while(
$user_data = db_fetch_object($result)) {
           
// truncate long usernames
           
$name = $user_data->name;
            if (
strlen($name) > 9) {
               
$name = substr($name, 0, 8) . "...";
                }
       
$output = "<div class='avatar'>";
$output .="<a href='" . url("user/" . $uid) . $user_data->uid ."'>";
$output .="<img src='". $user_data->picture . "'/></a><br>";
$output .= $name;
$output .= "</div>";
echo
$output;
        }
  
?>

i know this post is long ago.

sadist - May 21, 2009 - 01:39

i know this post is long ago. but i just want to share it here and get advice from the php gurus here. i do this to get my user's avatars to lay in a block, limit by 20 latest members with picture uploaded only.

i used realname module.

if there's a reason for this code to be better, i would like to hear your advice. cheers!

<?php
$limit
= 20;
$result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', 0, $limit);
while (
$account = db_fetch_object($result))
  {
    if (
$account->picture)
      {
       
$realname = theme('username', $account, array('plain' => TRUE));
       
$output .= '<a href="' . url("user/" . $uid) . $account->uid .'"><img src="/sites/default/files/imagecache/your_preset_name/'.$account->picture.'" title="'. $realname .'" alt="'. $realname .'"></a>';
      }
    else if (!
$account->picture) { $output .= ''; }
  }
return
$output;
?>

 
 

Drupal is a registered trademark of Dries Buytaert.