RiPro免插件修改用户默认头像为字母头像

RiPro免插件修改用户默认头像为字母头像

RiPro默认注册是没有头像的,要么用户自己上传,要么用QQ登录,然后用户选择QQ头像,这样感觉有点不方便,这里就分享下用户默认头像为字母头像,自动根据用户名首字母获取指定头像。效果如下:教程:本次修改主要在 ripro –> inc ->  theme-functi...

RiPro默认注册是没有头像的,要么用户自己上传,要么用QQ登录,然后用户选择QQ头像,这样感觉有点不方便,这里就分享下用户默认头像为字母头像,自动根据用户名首字母获取指定头像。

效果如下:

教程:

本次修改主要在 ripro –> inc ->  theme-functions.php  文件中修改

1. 搜索“function _the_theme_avatar()”,将该 function 函数替换为如下代码

class NameFirstChar {
    /*构造器*/
    public function __construct($name, $convertNum=true, $default="#"){
        $this->_name = $name;
        $this->_convertNum = $convertNum;
        $this->_default = $default;
        $this->firstChar = $this->getFirstChar();
    }
    /*待查找首字符的名字*/
    private $_name;
    /*是否转换数字为字母*/
    private $_convertNum;
    /*缺省返回值*/
    private $_default;
	 /*查找结果值*/
    public $firstChar;
    /*应字母的GB2312中文起始计算码*/
    private $_pinyinLetters = array(
        176161 => 'A',
        176197 => 'B',
        178193 => 'C',
        180238 => 'D',
        182234 => 'E',
        183162 => 'F',
        184193 => 'G',
        185254 => 'H',
        187247 => 'J',
        191166 => 'K',
        192172 => 'L',
        194232 => 'M',
        196195 => 'N',
        197182 => 'O',
        197190 => 'P',
        198218 => 'Q',
        200187 => 'R',
        200246 => 'S',
        203250 => 'T',
        205218 => 'W',
        206244 => 'X',
        209185 => 'Y',
        212209 => 'Z',
    );
    /*0-9 对应字母,取数字的英文首字母*/
    private $_numLetters = array(
        0 => 'Z',
        1 => 'O',
        2 => 'T',
        3 => 'T',
        4 => 'F',
        5 => 'F',
        6 => 'S',
        7 => 'S',
        8 => 'E',
        9 => 'N'
    );
    /* 二分搜索法查找GB2312计算码对应字母*/
    private function dichotomyLetterSearch($code){
        $keys = array_keys($this->_pinyinLetters);
        $lower = 0;
        $upper = sizeof($this->_pinyinLetters)-1;
        $middle = (int) round(($lower + $upper) / 2);
        if ( $code < $keys[0] ) return -1;
        for (;;) {
            if ( $lower > $upper ){
                return $keys[$lower-1];
            }
            $tmp = (int) round(($lower + $upper) / 2);
            if ( !isset($keys[$tmp]) ){
                return $keys[$middle];
            }else{
                $middle = $tmp;
            }
            if ( $keys[$middle] < $code ){
                $lower = (int)$middle + 1;
            }else if ( $keys[$middle] == $code ) {
                return $keys[$middle];
            }else{
                $upper = (int)$middle - 1;
            }
        }
        return -1;
    }
    /*获取字符串首字母或数字字符*/
    private function getFirstChar(){
        if(preg_match('/^[a-zA-Z]/', $this->_name)){
            //TODO $this->prefixType = "Letter"
            return $this->_name[0];
        }elseif(preg_match('/^[0-9]/', $this->_name)){
            //TODO $this->prefixType = "Number"
            return $this->_convertNum ? $this->_numLetters[$this->_name[0]] : $this->_name[0];
        }elseif (preg_match('/^[一-龥]/', $this->_name)) {
            //TODO $this->prefixType = "Chn"
            if(!$str = iconv( 'utf-8', 'gb2312', $this->_name )){
                return $this->_default;
            }
            $code = ord( substr($str, 0, 1) ) * 1000 + ord( substr($str, 1, 1) );
            if(($i=$this->dichotomyLetterSearch($code)) != -1){
                return $this->_pinyinLetters[$i];
            }
            return $this->_default;
        }
        return $this->_default;
    }
    /*转换首字符为大写*/
    public function toUpperCase(){
        return ucfirst($this->firstChar); // Sharp
    }
}

2. 搜索“// 判断头像类型”,在其上方添加如下代码(共2处)

$instance = new NameFirstChar($user->data->display_name, true, "Sharp");
$firstLetter = $instance->toUpperCase();

3. 搜索“_the_theme_avatar()”,将其替换为如下代码(共4处)

get_stylesheet_directory_uri() . '/assets/images/avatar/'.$firstLetter.'.png'

4. ripro –> inc -> admin -> page -> index.php  文件,第107行修改为如下代码

<div class="layui-status-img"><a href="javascript:;"><?php echo get_avatar($userss->user_email); ?></a></div>

5.删除 ripro –> assets –> images 下的 avatar 文件夹内的图片,替换为下面压缩包内的图片

版权申明:
版权声明

①:本站文章均为原创,除非另有说明,否则本站内容依据CC BY-NC-SA 4.0许可证进行授权,转载请附上出处链接,谢谢。
②:本站提供的所有资源均为网上搜集,不保证能100%完整,如有涉及或侵害到您的版权请立即通知我们。
③:本站所有下载文件,仅用作学习研究使用,请下载后24小时内删除,支持正版,勿用作商业用途。
④:本站保证所提供资源的完整性,但不含授权许可、帮助文档、XML文件、PSD、后续升级等。
⑤:使用该资源需要用户有一定代码基础知识!由本站提供的资源对您的网站或计算机造成严重后果的本站概不负责。
⑥:本站资源售价只是赞助,收取费用仅维持本站的日常运营所需。
⑦:如果喜欢本站资源,欢迎捐助本站开通会员享受优惠折扣,谢谢支持!
⑧:如果网盘地址失效,请在相应资源页面下留言,我们会尽快修复下载地址。

0

评论0

请先

会员低至49元,开通享海量VIP资源免费下载 自助开通
显示验证码
没有账号?注册  忘记密码?