• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Get avatar while having only user id

CyberAP

Well-known member
#1
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}
 

Chris D

XenForo developer
Staff member
#2
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.
 

CyberAP

Well-known member
#3
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?
 

CyberAP

Well-known member
#5
As far as I know it's not passed to thread_list_item template. By broken avatar you meant this:

Or a broken link?
 

Chris D

XenForo developer
Staff member
#6
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.
 

CyberAP

Well-known member
#8
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.
 

CyberAP

Well-known member
#9
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.