Sorry about my messed up post earlier. Let me start all over:

My site's View Online. I'm trying to make it so that users' avatars show up next to their node/comment, like LiveJournal. I've installed profile.module which adds support for avatars. With heavy muddling, I managed to edit the Trip theme main file to include this script below.

It seems to be working, but only partially. It works only for users #2 and #3, but not for anyone else. Could anyone look at this code please and maybe troubleshoot?

Thanks!

   [!-- Inserts Avatar picture here.  --]
        
        $filename = profile_avatar_path($node->uid,".jpg");
        print "[img class=\"avatar\" src=\"";
          if (!file_exists($filename)) {
                $filename =  profile_avatar_path(1, ".jpg");
          }
        print $filename;
        print "\" align=\"top\"]";
        

           print strtr(t("%a | %b"), array("%a" =] format_name($node), "%b" =] format_date($node->created, "medium"))); $
        [/td]
        [td][/td]
                [td align="right" valign="top" class="node-taxonomy"]

          [?php

          if (module_exist("taxonomy")) {
            $terms = taxonomy_link("taxonomy terms", $node);
          }
          print $this->links($terms);
          

Comments

Anonymous’s picture

Won't anyone help? :(

fiade’s picture

the problem could be that other users are using non ".jpg" images. You are writting ".jpg" as second argument for profile_avatar_path() when you should use something like:
profile_avatar_path( "$user->nid", "$user->profile_avatar" ) so drupal asks the database wich extension the image has.

I don´t understand how you are getting more avatars than only the one for user #1 when you are using profile_avatar_path(1,".jpg") :?

KaT
Fiade: Juegos de rol

Anonymous’s picture

You're right, it was the beta user (#4) using an animated GIF, not a JPG.

The way I have it set up is so that when an user does NOT have an avatar, the logic returns the default avatar which is in user #1's profile. Hopefully I've written the logic correctly.

I'd like to give the user the option between JPG and GIF, and whatever else, so I've tried different combinations of your suggestion and what the code already has, and none has worked.

$node->uid as the first argument seems to be working, since it correctly returns the user's number for each node.

I can't seem to write the second argument correctly so that it returns the extension..?

Thanks in advance, you've already helped a lot!

Dries’s picture

I just looked at the profile module's code and I find the way that avatars are being handled somewhat peculiar. Looks like there is no easy way to get a user's avatar which, IMO, is a bug in the profile module's design. (Why would we want to hash the user ID, and why is profile_avatar_path() so weird?)

marco’s picture

I'm the writer of that code. Avatars have these problems:
- when you upload an image you must be sure not to overwrite any other file, especially other people's avatars. You must also pay attention to the filenames for security reasons; so I decided to overwrite the image name with something based on the user id (actually I don't know why I choose to use md5, probably just the uid would be enough)
- but you also need to store the image type (jpg/gif/png); actually most if not all browsers don't care whether an image extension is right and show the image anyway, but you still need to know whether a user has an avatar or not
- finally, the avatar must be stored somewhere; using a function to return the path makes sure that internal changes will be safe (like hashing files to dirs and subdirs)

The greatest problem I see is that generally avatars are stored in the data field, which is:
a) serialized
b) not returned where needed, like for example in comment module; the cleanest way to fix this is to load the user in the theme, but this leads to many many more queries.

You can deal with a) adding a specific profile_avatar field, but with b) the only scalable fix I found was to patch comment.module to return profile_avatar too.

But I need rid too, to write nicknames in different colors. And other fields may be needed.

Unfortunately we can't just return user.*, because there may be (and actually are) name clashes between joined tables. So perhaps the cleanest way to deal with this is to rename every field of user table from FIELD to user_FIELD, or perhaps a shorter uFIELD. This is clearly a major change, and to be perfect should be coupled with similar changes to every other table.

--
Marco

fiade’s picture

the idea is that you need some way to extract the data "profile_avatar" from the user. You are saying to me that you have now the uid via $node->uid, so, you can make something like:

$user_i_want = user_load(array("uid" => $node->uid));

now you have loaded into $user_i_want all the data you need so you can use:

profile_avatar_path($user_i_want->uid, $user_i_want->profile_avatar);

As you can see, profile_avatar_path() needs a uid as first argument with wich it creates a md5 hash and then an extension as second parameter.

tell us if this helps you.

KaT
Fiade: Juegos de rol

Anonymous’s picture

Alrite! Your advice worked! Thanks so much! Sorry for the late reply, I was out on vacation before school started again.

I did the same fix for the comments, using:

$user_i_want = user_load("uid" => $comment->uid);
$filename = profile_avatar_path($comment->uid, $user_i_want->profile_avatar);

Worked beautifully! Thank you!

Steven’s picture

It does indeed seem that the person who did the avatar aspect of profile module didn't think a lot about how to use them practically.

Here's the code I use on my mini-site:

if ($comment->uid) {
  $author = user_load(array("uid" => $comment->uid));
  $ext = $author->profile_avatar;
  $avatar = profile_avatar_path($comment->uid, $ext);
}
print ($avatar ? "<img src='$avatar' alt='' title='' class='avatar'>" : "");

For nodes it works the same, except you replace $comment with $node.

It works quite nicely on my site. Default behaviour is to not show anything for anonymous users or people without avatars, but you can change that in the last line if you wish.

The main problem is that this requires an SQL query per comment/node, so it's not a good option for heavy traffic sites. Also, user_load loads the entire user record, even though only one specific module-defined field is required.

You could submit a feature request for this...

Note, you'll probably want to use a img.avatar { float: right; } CSS rule or similar.