News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Creating a URL that shows user avatar

Started by nadrojcote, December 14, 2009, 09:20:31 PM

Previous topic - Next topic

nadrojcote

I dont exactly know how to explain what I am looking for, but here it goes. Currently my chat module is using

<img src="avatars/' + avatar + '">

to display my users avatars. The problem with this is that it only displays avatars for users that chose an avatar from the selection I have on my forum, it does not show the avatars of the user that uploaded their own or the users that are using a url.

Is there a simply way to make the img src display the avatar for all users?

Thanks in advance to anyone who replies.

nadrojcote

i forgot to mention it is pulling the avatar variable from smf_members.avatar

nadrojcote

maybe if there is a way that I can modify smf to save everyones avatar as a full url in a database???

Arantor

First up, you could have just edited your first post to include that information without having to bump the post twice.

There isn't an easy way to do it because there are three kinds of avatar, stored in two different places across two different tables, potentially.

You'd be easier modifying the chat module to be honest.
Holder of controversial views, all of which my own.


SoLoGHoST

#4
Yep, that is correct.  If the user has selected avatar from the gallery and/or the URL, it stores the path/url within the members table in the avatar column.  Otherwise, if the user has uploaded an avatar, it's filepath will be:  http://yourforumsite/index.php?action=dlattach;attach=42;type=avatar

Where 42 = the attach id associated with it via the attachments table.  But you have to be sure it's an avatar attachment, since this table holds all regular post attachments as well.  So you can check the id_member column of the attachments table.  If this is empty than it is actually a post attachments.  That is if it equals 0.  But if it equals the $user_info['id'] than it is an avatar attachment.  Note, if there are no matches via the id_member column in the attachments table, than the user hasn't uploaded an avatar.  So actually this is rather simple.

1. Check the attachments table for id_member column = $user_info['id'].  If a match is found, return the url like so:  http://yourforumsite/index.php?action=dlattach;attach=[id_attach];type=avatar
2. If this isn't found, check within the members table in the avatar column.  If this field is empty also, than no avatar exists for that member.  If a url exists for that users avatar, than http:// will be the first 7 characters in here.  Otherwise Foldername/filename.extension will be in here and will be coming from your avatars directory within the SMF Root, and you can link to it like so:  http://yourforumsite/avatars/Foldername/filename.extension
For Example:  http://somewebsite.com/avatars/Actors/Brad_Pitt.jpg

Well, hope this answers your question.  There is no 1 link to control them all, though you could create an action/url that can handle all of these in 1.  And than just call that action and TADA.

Cheers :)

Arantor

Quote from: SoLoGHoST on December 15, 2009, 02:59:58 AM
Otherwise, if the user has uploaded an avatar, it's filepath will be:  http://yourforumsite/index.php?action=dlattach;attach=42;type=avatar

Only if they haven't specified a custom avatar directory... which is still triggered off the attachments table, whose filename is there too, but the physical path is elsewhere based on $modSettings values.

Yeah, 4 potential URL styles. Better off modifying the chat to figure it out to be honest.
Holder of controversial views, all of which my own.


SoLoGHoST

#6
This url handles all avatar attachments within the attachments table.  So not really sure what you are getting at.  This will always work if an avatar is found for that user in the attachments table:

http://yourforumsite/index.php?action=dlattach;attach=42;type=avatar

Where 42 = id_attach associated with the match of $user_info['id'] = id_member column (from the attachments table).

If no match is found, than check the members table, and get the avatar column where $user_info['id'] = id_member column (from the members table).  If this field is empty also, than the user has no avatar.  If this field has http:// as the first 7 letters, than it is a URL, otherwise, it will contain the avatar relative to the avatar directory via your SMF Root.  In this case, it should be linked to the avatar directory.  The value for the avatar column can look like this:  Actors/Bruce_Campbell.jpg.  So all you need to do is place your forum url in front of it (minus the index.php) and add avatars/ to it.  So will look like this:  http://yourforum.com/avatars/Actors/Bruce_Campbell.jpg

And like I said, you can create an action to do this with and just call upon that action and it can do all of the necessary stuff for this and return the URL depending on these things mentioned above.

Cheers

Arantor

Except that if you have a custom directory it doesn't work.

Classic case, I just proved on my dev forum, which is 2.0 RC1.2. I use a custom avatar directory, which issues:
http://example.com/avs_user/avatar_19_1260556983.gif

The matching ID in the attachments table - the only thing user 19 has uploaded, is attachment 668, which is their avatar, which does indeed have avatar_19_1260556983.gif as the filename.

Except when I go to that URL, http://example.com/index.php?action=dlattach;attach=668;type=avatar I get nothing. Can't be found.

(Obviously I tested that with the real domain, not example.com, but the principle stands)
Holder of controversial views, all of which my own.


SoLoGHoST

#8
Ahhhhh, ok, I see what you are saying here.  So, just check the attachment_type.  If it is empty, than it will be coming from the URL, if not, than it will be coming from an actual folderpath.  Though, if you don't have this set, than don't worry about it and use the approach I supplied.

Arantor

Correct thing to do is check $modSettings['custom_avatar_url'], like the code in loadMemberContext does.
Holder of controversial views, all of which my own.


SoLoGHoST


nadrojcote

Ok I will work on figuring this out, i knew it wasnt going to be easy and i knew that smf stores avatars all over the place. Thanks for all the info guys.

SoLoGHoST

Honestly, It's very easy if you ask me.  A lot easier than modifying your Chat Module, which looks like it's using some sort of javascript to set the url into the src where avatar = the filepath for the avatar url.  Might even be better to modify both.

In any case, good luck, and glad we could be of some help.

Cheers :)

Advertisement: