此篇文章的代码完成度高且选择多,几乎不需要修改,直接用即可。
gravatar头像源设置
此段代码放进functions.php里的function themeConfig函数内

$gravatars = new Typecho_Widget_Helper_Form_Element_Select('gravatars',[
'www.gravatar.com/avatar' => 'gravatar的www源',
'cn.gravatar.com/avatar' => 'gravatar的cn源',
'secure.gravatar.com/avatar' => 'gravatar的secure源',
'sdn.geekzu.org/avatar' => '极客族源',
'gravatar.proxy.ustclug.org/avatar' => '中科大源[不建议]',
'cdn.v2ex.com/gravatar' => 'v2ex源',
'dn-qiniu-avatar.qbox.me/avatar' => '七牛源[不建议]',
'gravatar.helingqi.com/wavatar' => '禾令奇源[建议]',
'gravatar.loli.net/avatar' => 'loli.net源'],
    'gravatar.helingqi.com/wavatar','<span>gravatar头像源</span>','替换Gravatar头像的默认地址。<br>替换后可提升加载速度,默认使用<b>禾令奇[建议]源</b>。');
$form->addInput($gravatars->multiMode());

核心函数
此段代码推荐放到functions.php最后面,要自己写个class Tool{}放进去。

 /**
 * 输出qq头像(不暴露qq号)或者自定义源gravatar的头像
 * 
 * @return story
 */
public static function avatr($mail, int $size = 100, bool $out = false)
{
    $Op_avatr = Typecho_Widget::widget('Widget_Options')->gravatars;
    $Str = str_replace('@qq.com','',$mail);
    $avatr = '';
    if(stristr($mail,'@qq.com')&&is_numeric($Str)&&strlen($Str)<11&&strlen($Str)>4){
        $url = 'https://s.p.qq.com/pub/get_face?img_type=3&uin='.$Str;
        $api = get_headers($url,true)['Location'];
        $json_api = json_encode($api);
        $ex_api = explode("&k=",$json_api)[1];
        $k_value = explode("&s=",$ex_api)[0];
        $avatr = 'https://q.qlogo.cn/g?b=qq&k='.$k_value.'&s='.$size;
    }else{
        $mail = md5($mail);
        $avatr = 'https://'.$Op_avatr.'/'.$mail.'?s='.$size;
    }
    if ($out === true) {
        return $avatr;
    }else{
        echo $avatr;
    }
}

使用方法

<?php Tool::avatr(mail,size,out); ?>

函数说明:
mail为邮箱地址:$this->user->mail、$this->author->mail、$comments->mail、'12345@qq.com',诸如此类
size为尺寸:正整数型,默认100
out为输出类型:默认false为echo,true则为return
但后两个函数一般不需要填写

标签: none

添加新评论