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

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

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

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


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
    }
}

[/ttr2v]
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行修改为如下代码



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

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
评论 共9条

请登录后发表评论