phpcms 分析用户头像在服务器端的保存与使用

来源:互联网 发布:ubuntu ports 源 编辑:程序博客网 时间:2024/05/22 14:07

phpcms 分析用户头像在服务器端的保存与使用

距离第一篇blog快4年了,没想到一下子就过去了
很少记录事,因为感觉一直很忙
这两天要请人安装水果电脑系统,闲下来了,写两篇文章来记记事
顺便看看markdown写文章是什么感觉

对phpcms也看了一段时间了,觉得它的实现还是不错的
对它也有感情了,应该也没时间再学第二个内容管理系统了
好,开始

[域名]/index.php?m=member&c=index&a=account_manage_avatar&t=1
这网页在firefox排版有问题,在chrome没问题
这个是登录后的phpcms用户的会员中心
在账号管理-》修改头像
选择头像后,有个保存按钮

按了之后,前端我就不分析了,后端是到了
[域名]/phpsso_server/index.php?m=phpsso&c=index&a=uploadavatar
下面带有slako字串的是我的分析

     /**     *  上传头像处理     *  传入头像压缩包,解压到指定文件夹后删除非图片文件     */    public function uploadavatar() {        //根据用户id创建文件夹        if(isset($this->data['uid']) && isset($this->data['avatardata'])) {            $this->uid = intval($this->data['uid']);            $this->avatardata = $this->data['avatardata'];        } else {            exit('0');        }        $dir1 = ceil($this->uid / 10000);//slako 一万个用户使用该目录         $dir2 = ceil($this->uid % 10000 / 1000);//slako 一万个用户中的一千个用户使用该目录        //slako 我觉得这种        //创建图片存储文件夹        $avatarfile = pc_base::load_config('system', 'upload_path').'avatar/';        $dir = $avatarfile.$dir1.'/'.$dir2.'/'.$this->uid.'/';        //slako 每一个用户有一个自己的目录        if(!file_exists($dir)) {            mkdir($dir, 0777, true);        }        //存储flashpost图片        $filename = $dir.'180x180.jpg';        $fp = fopen($filename, 'w');        fwrite($fp, $this->avatardata);        fclose($fp);        $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');        $files = glob($dir."*");        foreach($files as $_files) {            if(is_dir($_files)) dir_delete($_files);            if(!in_array(basename($_files), $avatararr)) @unlink($_files);        }        if($handle = opendir($dir)) {            while(false !== ($file = readdir($handle))) {                if($file !== '.' && $file !== '..') {                    if(!in_array($file, $avatararr)) {                        @unlink($dir.$file);                    } else {                        $info = @getimagesize($dir.$file);                        if(!$info || $info[2] !=2) {                            @unlink($dir.$file);                        }                    }                }            }            closedir($handle);            }        //slako 把头像存为几个大小        pc_base::load_sys_class('image','','0');        $image = new image(1,0);        $image->thumb($filename, $dir.'30x30.jpg', 30, 30);        $image->thumb($filename, $dir.'45x45.jpg', 45, 45);        $image->thumb($filename, $dir.'90x90.jpg', 90, 90);        //slako 标志着该用户已经有头像了        $this->db->update(array('avatar'=>1), array('uid'=>$this->uid));        exit('1');    }

如图是服务器保存的情况

当要调取使用头像时,使用方法如

public function account_manage_avatar() {        $memberinfo = $this->memberinfo;        //初始化phpsso        $phpsso_api_url = $this->_init_phpsso();        $ps_auth_key = pc_base::load_config('system', 'phpsso_auth_key');        $auth_data = $this->client->auth_data(array('uid'=>$this->memberinfo['phpssouid'],'sys_auth_time'=>microtime(true)), '', $ps_auth_key);        $upurl = base64_encode($phpsso_api_url.'/index.php?m=phpsso&c=index&a=uploadavatar&auth_data='.$auth_data);        //slako 获取头像数组的函数调用        $avatar = $this->client->ps_getavatar($this->memberinfo['phpssouid']);        include template('member', 'account_manage_avatar');    }

来看看ps_getavatar
在phpcms/modules/member/classes/client.class.php中

/**     * 根据phpsso uid获取头像url     * @param int $uid 用户id     * @return array 四个尺寸用户头像数组     */    public function ps_getavatar($uid) {        $dir1 = ceil($uid / 10000);        $dir2 = ceil($uid % 10000 / 1000);        $url = $this->ps_api_url.'/uploadfile/avatar/'.$dir1.'/'.$dir2.'/'.$uid.'/';        $avatar = array('180'=>$url.'180x180.jpg', '90'=>$url.'90x90.jpg', '45'=>$url.'45x45.jpg', '30'=>$url.'30x30.jpg');        return $avatar;    }

这记写了一个小时,实在写得慢

下一篇分析一下该系统是怎么保存发表了的文章中的图片

阅读全文
1 0