Get avatar while having only user id

CyberAP

Well-known member
I'm wondering is there any way to get user's avatar by passing only his user id? So for example it will look like this:

{xen:helper avatar, $thread.lastPostInfo.user_id, s}
 
The minimum amount of data you need to get an avatar is the user_id and avatar_date. The avatar_date is the field which confirms they have uploaded an avatar, but, of course, they might also have switched to using gravatar, in which case the gravatar field is important too.

That said, if you want to "fake it":
Code:
{xen:helper avatar, {xen:array 'user_id={$thread.lastPostInfo.user_id}', 'avatar_date=1'}, s}

Only problem is if there's actually no avatar, then it will just show a broken image.
 
The minimum amount of data you need to get an avatar is the user_id and avatar_date. The avatar_date is the field which confirms they have uploaded an avatar, but, of course, they might also have switched to using gravatar, in which case the gravatar field is important too.

That said, if you want to "fake it":
Code:
{xen:helper avatar, {xen:array 'user_id={$thread.lastPostInfo.user_id}', 'avatar_date=1'}, s}

Only problem is if there's actually no avatar, then it will just show a broken image.
Wow, that really helps, thanks! Maybe there is a way to check if avatar is set?
 
As far as I know it's not passed to thread_list_item template. By broken avatar you meant this:
avatar_s.png

Or a broken link?
 
I actually meant a broken link, but I was wrong.

You're right, it will show the default avatar. This might not be correct if they are using gravatar.
 
It turns out actually you will not get a question mark avatar, but a broken link. Solved this with putting a default avatar as a background behind an actual avatar.
 
I've encountered another problem, you can't do that image replacement trick with semi-transparent avatars. So I had to dig for a javascript code that solves the problem and here it is:

Code:
$(window).load(function() {
  $('.avatar img').each(function() {
    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
      // do whatever you want here, for example you can replace broken image with a default question mark avatar like this ↓
      this.src = 'styles/default/xenforo/avatars/avatar_s.png';
    }
  });
});
Please note this code works slow.
 
Top Bottom