Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.7k views
in Q2A Core by

How can I get profile avatar image url?

I'm trying to create og meta tags for facebook, to display user's avatars as the og image, something like this:

if (qa_request() === 'user') {
    $this->output('<meta property="og:image" content=" Get User Profile Image "/>');
}

I also need to know if this will do for the case "User Profile"  if (qa_request() === 'user') 

by
So yo only want to add that meta when displaying the user profile, right? That would mean you link to a user profile and you see their avatar. Is this correct?
by
Yes @pupi1985 , to display user's avatars as the og image if a person shares the link of any profile on Facebook, like - http://www.question2answer.org/qa/user/Lostfiles
Then the user's avatar is displayed as the featured image
by
So did this actually work?
by
I'm sorry for the delay, I haven't tested it yet, but I will get back to you as soon as I test it.

1 Answer

+2 votes
by
selected by
 
Best answer

The thing is that the URL depends on whether you've enabled gravatar or not, whether it is possible to upload avatars or not, whether the user has upload an avatar or agreed to use gravatar or not. Not to mention there are sizes that should be taken into account as the final image might need a specific one.

I haven't tested but this code added to your qa-theme.php file should output the meta tag you're mentioning, covering all the scenarios:

public function head_metas() { 
    parent::head_metas(); 
    if (qa_request_part(0) !== 'user') 
        return; 
    $handle = qa_request_part(1); 
    if (empty($handle)) 
        return; 
    $useraccount = qa_db_single_select(qa_db_user_account_selectspec($handle, false));

    $size = 60;  // Size of the image to be requested 
    $defaultBlobId = qa_opt('avatar_default_blobid');
    if (qa_opt('avatar_allow_gravatar') && ($useraccount['flags'] & QA_USER_FLAGS_SHOW_GRAVATAR)) 
        $html = sprintf( 
            '%s://www.gravatar.com/avatar/%s?s=%s', 
            qa_is_https_probably() ? 'https' : 'http', 
            md5(strtolower(trim($useraccount['email']))), 
            $size 
        ); 
    elseif (qa_opt('avatar_allow_upload') && ($useraccount['flags'] & QA_USER_FLAGS_SHOW_AVATAR) && isset($useraccount['avatarblobid'])) 
        $html = qa_path('image', array('qa_blobid' => $useraccount['avatarblobid'], 'qa_size' => $size), qa_opt('site_url'), QA_URL_FORMAT_PARAMS); 
    elseif ((qa_opt('avatar_allow_gravatar') || qa_opt('avatar_allow_upload')) && qa_opt('avatar_default_show') && !empty($defaultBlobId)) 
        $html = qa_path('image', array('qa_blobid' => qa_opt('avatar_default_blobid'), 'qa_size' => $size), qa_opt('site_url'), QA_URL_FORMAT_PARAMS); 
    else 
        $html = null;

    if (isset($html)) 
        $this->output('<meta property="og:image" content="' . $html . '"/>'); 
}

by
Sorry for the delay @Pupi1985 , I've just tested the code and it's giving me an error that says: Can't use function return value in write context in:


elseif (qa_opt('avatar_allow_upload') && ($useraccount['flags'] & QA_USER_FLAGS_SHOW_AVATAR) && isset($useraccount['avatarblobid']))
        $html = qa_path('image', array('qa_blobid' => $useraccount['avatarblobid'], 'qa_size' => $size), qa_opt('site_url'), QA_URL_FORMAT_PARAMS);
    elseif ((qa_opt('avatar_allow_gravatar') || qa_opt('avatar_allow_upload')) && qa_opt('avatar_default_show') && !empty(qa_opt('avatar_default_blobid')))
        $html = qa_path('image', array('qa_blobid' => qa_opt('avatar_default_blobid'), 'qa_size' => $size), qa_opt('site_url'), QA_URL_FORMAT_PARAMS);
    else
        $html = null;
by
Probably, it happens because your PHP version is lower than 5.5 so it is expecting the "empty()" function to receive a variable. I've extracted the "qa_opt" call to a variable. Try again.
by
it's working now :D Thank you very much ^^
by
Thanks Pupi!
...