首页 > 技术文章 > thinkphp3.2.3----图片上传并生成缩率图

qiuphp 2017-09-18 16:05 原文

 1 public function uploadify(){
 2     if(!IS_POST){
 3         $this->error('非法!');
 4     }
 5     $upload = $this->_upload();
 6 }
 7 
 8 protected function _upload(){
 9     $obj = new \Think\Upload();
10     $obj->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
11     $obj->rootPath = './data/upload/';    //upload类、image类的./入口目录
12     $obj->savePath = 'face/';
13     $obj->saveRule = 'uniqid';
14     $obj->uploadReplace = true;
15     $obj->autoSub = true;
16     $obj->subType = 'date';
17     $obj->dateFormat = 'Y_m';
18     $info = $obj->upload();
19     $userinfo = $this->users_model->where(array('id' => $this->userid))->find();
20     if(!$info) {// 上传错误提示错误信息
21         $this->error($obj->getError());
22     }else{// 上传成功
23         $face = './data/upload/'.$info['face']['savepath'].$info['face']['savename'];
24         // 生成缩略图128*128;
25         $image = new \Think\Image(); 
26         $image->open($face);
27         // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
28         $image->thumb(128, 128)->save($face);
29       M('users')->where(array('id' => $this->userid))->save(array('avatar' => $info['face']['savepath'].$info['face']['savename']));
30       if($userinfo['avatar']){
31            @unlink('./data/upload/'.$userinfo['avatar']);//删除旧图
32       }
33       redirect(U('user/profile/upload_face'));
34     }
35 }

 

推荐阅读