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

Getting an avatar from a URL

Discussion in 'XenForo Questions and Support' started by Brentnauer, Apr 21, 2012.

  1. Brentnauer

    Brentnauer Well-Known Member

    Would someone be willing to help me figure out how to grab an avatar using something like:

    <img src="http://mysite.com/avatar.php?userid=1" width="50" height="50">
    I realize some code would need to be written to make avatar.php work, but I'm not sure where to start.

    What I'm trying to do is find a way to pull my user's avatars into AddonChat... I can return values containing the user ID and username in the chat message templates, but I need some kind of external script to make the above snippet useful.

    Any help would be greatly appreciated... New to xF coding (and a novice in PHP) so I'm not sure where to start with this.
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Here is the function for it:


    	 * Returns the URL to a user's custom avatar
    	 * @param array $user
    	 * @param string $size (s,m,l)
    	 * @return string
    	protected static function _getCustomAvatarUrl(array $user, $size)
    		$group = floor($user['user_id'] / 1000);
    		return XenForo_Application::$externalDataUrl . "/avatars/$size/$group/$user[user_id].jpg?$user[avatar_date]";
    Brentnauer likes this.
  3. Brentnauer

    Brentnauer Well-Known Member

    Greatly appreciated, this definitely helps.

    The code makes sense, but I'm not sure how to put it to any use... I tried putting it in it's own php file:

    But it returns:
    Parse error: syntax error, unexpected T_PROTECTED in /home/XXXX/public_html/community/avatar.php on line 11

    I apologize for my lack of knowledge here... Do I need to call something at the beginning of the file? I've been up all night taking care of sick dogs so maybe I'm not following or putting 2 and 2 together.

    Thank you for your help, Jake.
  4. Brentnauer

    Brentnauer Well-Known Member

    I have no idea what I'm doing:

    include_once "../community/library/XenForo/Template/Helper/Core.php";
    include_once "../community/library/XenForo/Application.php";
    $chatterid = (int)$_GET['chatterid'];
    $group = floor($user['user_id'] / 1000);
    $avatar = XenForo_Application::$externalDataUrl . "/avatars/s/$group/$chatterid.jpg?$chatterid[avatar_date]";
    print $avatar;
    Result: http://curiouscosmos.com/community/avatar.php?chatterid=1
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  6. Brentnauer

    Brentnauer Well-Known Member

    Oh man... You are an absolute hero right now, you have no idea.

    I spent most of my day trying to figure this out, and ended up steam cleaning the carpets of my entire apartment out of frustration.

    I don't even know how to thank you right now, you're awesome.

  7. Brentnauer

    Brentnauer Well-Known Member

    Studying your code now, this is how I feel:

Share This Page