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

本文详细分析了phpcms系统中用户头像的上传、存储和使用过程。首先,当用户选择头像并保存时,头像会被保存在特定目录下,并生成多个尺寸。然后,在调取头像时,通过phpsso接口获取不同尺寸的头像URL。整个过程涉及文件操作、图像处理和前后端交互。
摘要由CSDN通过智能技术生成

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');

}

0818b9ca8b590ca3270a3433284dd417.png

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

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;

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值