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

A way to get avatars?

Discussion in 'XenForo Development Discussions' started by LunarCraft, Sep 5, 2011.

  1. LunarCraft

    LunarCraft Member

    I am working on a little control panel project for a minecraft server I run, it is hooked into Xenforo for authentication and I figured I'd try to use the avatar of the person logged in.
    However I noticed that when using the method getAvatarFilePath($userId, $size) in XenForo_Model_Avatar. I get the local system path.

    That of course won't work when I want to output the image in regular html, so if you don't mind me asking, what is the best approach to getting and displaying a users xenforo avatar in a page outside xenforo?
    I attempted adding a method to XenForo_Model_Avatar which does what I want, but at the same time I would like to make it work universally without having to modify the class.

    Custom method that I want to get rid of in favor of a built-in solution:
    PHP:
    public function getAvatarUrl($userId$size)
        {
            return 
    sprintf('http://domain/xenforo_installation/data/avatars/%s/%d/%d.jpg',
                
    $size,
                
    floor($userId 1000),
                
    $userId
            
    );
        }
    I hope some bright person might be able to answer me :)
     
  2. James

    James Well-Known Member

    What's wrong with using getAvatarFilePath()? It'll return a URL relative to the XF installation which you can use in your img tags?

    EDIT: Never mind, I forgot the method calls the first parameter as XenForo_Helper_File::getExternalDataPath().
     
  3. ragtek

    ragtek Guest

    XenForo_Template_Helper_Core::helperAvatarUrl

    PHP:
    /**
         * Helper to fetch the URL of a user's avatar.
         *
         * @param array $user User info
         * @param string $size Size code
         * @param boolean Serve the default gender avatar, even if the user has a custom avatar
         * @param boolean Serve the full canonical URL
         *
         * @return string Path to avatar
         */
        
    public static function helperAvatarUrl($user$size$forceType null$canonical false)
        {
            if (!
    is_array($user))
            {
                
    $user = array();
            }

            if (
    $forceType)
            {
                switch (
    $forceType)
                {
                    case 
    'default':
                    case 
    'custom':
                        break;

                    default:
                        
    $forceType null;
                        break;
                }
            }

            
    $url self::getAvatarUrl($user$size$forceType);

            if (
    $canonical)
            {
                
    $url XenForo_Link::convertUriToAbsoluteUri($urltrue);
            }

            return 
    htmlspecialchars($url);
        }
     
    LunarCraft and Floris like this.
  4. Floris

    Floris Guest

    Ragtek to the rescue, no surprise there ;)
     
  5. LunarCraft

    LunarCraft Member

    Okay, excuse my poor understanding of XenForo, but the method works perfectly except for one thing, I can't quite figure out why it only shows the default gender avatar. I am guessing it has something to do with the array that is passed to the method, is there anywhere I can see what keys I must add? I added gender, which made it work with the default avatars and I have tried to guess the key for user id without success so far.

    Thanks :)
     
  6. ragtek

    ragtek Guest

    You need to pass "all" relevant data ( i think userid, gravatar, avatar_date and gender if you want the genderavatar as standardavatar are enough) to the method as first parameter as array.

    /** @var $user XenForo_Model_User */
    $user = XenForo_Model::create('XenForo_Model_User');

    $me = $user->getUserById(1);

    $avatarUrl = XenForo_Template_Helper_Core::callHelper('avatar', array($me, 'm', null, true));
     
    Mutt, Fuhrmann and LunarCraft like this.
  7. LunarCraft

    LunarCraft Member

    Wow, I can't believe I missed that. I actually had most of the code in the page already, I just weren't using it right.
    Thanks again!
     
  8. xfrocks

    xfrocks Well-Known Member

    You should use XenForo_Template_Helper_Core::callHelper instead, something like this

    PHP:
    $avatarUrl XenForo_Template_Helper_Core::callHelper('avatar', array($me'm'nulltrue));
     
    ragtek likes this.
  9. Mutt

    Mutt Well-Known Member

    I have a script that uses
    Code:
    $visitor = XenForo_Visitor::getInstance();
    and I tried getting the av by using
    Code:
    $avatarUrl = XenForo_Template_Helper_Core::callHelper('avatar', array($visitor, 'm', null, true));
    but it's returning the generic avatar instead of the proper custom one. anyone know what I'm doing wrong?
     
  10. xfrocks

    xfrocks Well-Known Member

    Short answer: because $visitor is not an array.
    Longer answer: this block of code

    PHP:
    public static function helperAvatarUrl($user$size$forceType null$canonical false)
    {
    if (!
    is_array($user))
    {
    $user = array();
    }
    ...
    }
    Solution:

    PHP:
    $avatarUrl XenForo_Template_Helper_Core::callHelper('avatar', array($visitor->toArray(), 'm'nulltrue));
     
  11. Mutt

    Mutt Well-Known Member


    thank you. I had already handled. when I changed things around due to other reasons I needed to add
    Code:
    $visitor = XenForo_Visitor::getInstance()->toArray();
    then this worked fine
    Code:
    $avatarUrl = XenForo_Template_Helper_Core::callHelper('avatar', array($visitor, $avsize, null, true));
     

Share This Page