1. 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

Discussion in 'XenForo Development Discussions' started by CyberAP, Sep 3, 2015.

  1. CyberAP

    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}
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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 likes this.
  3. CyberAP

    CyberAP Well-Known Member

    Wow, that really helps, thanks! Maybe there is a way to check if avatar is set?
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    The only way to know is if you have the real avatar_date value.
     
    CyberAP likes this.
  5. CyberAP

    CyberAP Well-Known Member

    As far as I know it's not passed to thread_list_item template. By broken avatar you meant this:
    [​IMG]
    Or a broken link?
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    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 likes this.
  7. CyberAP

    CyberAP Well-Known Member

    This seems fine for me, thanks for your help!
     
  8. CyberAP

    CyberAP Well-Known Member

    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.
     
  9. CyberAP

    CyberAP Well-Known Member

    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.
     

Share This Page