• 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?

#1
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 :)
 

James

Well-known member
#2
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().
 
R

ragtek

Guest
#3
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($url, true);
        }

        return htmlspecialchars($url);
    }
 
#5
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 :)
 
R

ragtek

Guest
#6
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));
 

xfrocks

Well-known member
#8
You should use XenForo_Template_Helper_Core::callHelper instead, something like this

PHP:
$avatarUrl = XenForo_Template_Helper_Core::callHelper('avatar', array($me, 'm', null, true));
 

Mutt

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

xfrocks

Well-known member
#10
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?
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', null, true));
 

Mutt

Well-known member
#11
Solution:

PHP:
$avatarUrl = XenForo_Template_Helper_Core::callHelper('avatar', array($visitor->toArray(), 'm', null, true));

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));