public function cropImg()
 {
     //图片裁剪数据
     $params = I('post.');
     //裁剪参数
     if (!isset($params) && empty($params)) {
         $this->error('参数错误!');
     }
     //头像目录地址
     $path = 'Uploads/Avatar/';
     //裁减后的地址
     $crop_path = $path . session('user_auth')['username'] . '_crop.jpg';
     //最终保存的地址
     $final_path = $path . session('user_auth')['username'] . '.jpg';
     //临时图片地址-- 刚才上传的头像地址
     $pic_path = $path . get_avatar_name() . '.jpg';
     //实例化裁剪类
     $Think_img = new ThinkImage(THINKIMAGE_GD);
     //裁剪原图得到选中区域
     $Think_img->open($pic_path)->crop($params['w'], $params['h'], $params['x'], $params['y'])->save($crop_path);
     //生成缩略图
     $Think_img->open($crop_path)->thumb(150, 150, 1)->save($final_path);
     //删除上传的图片和临时目录
     unlink($crop_path);
     unlink($pic_path);
     //更新个人信息中的avatar的值为1  说明已经上传过头像
     $data = array();
     $data['uid'] = session('user_auth')['uid'];
     $data['avatar'] = 'Uploads/Avatar/' . session('user_auth')['username'] . '.jpg';
     M('User')->save($data);
     $this->success('上传头像成功');
 }
 public function save()
 {
     $params = I('post.');
     $tmp_url = $params['src'];
     //得到真实图片的的路径
     $old_real_url = str_replace('_tmp.', '.', $tmp_url);
     $old_thumb_url = str_replace('Picture', 'Thumb', $old_real_url);
     $file_name = pathinfo($old_real_url)['filename'];
     $new_name = time() . rand(100000, 999999);
     //给原图和缩略图都重新命名--避免缓存
     $rand = rand();
     $new_pic_path = str_replace($file_name, $new_name, $old_real_url);
     $new_thumb_path = str_replace('Picture', 'Thumb', $new_pic_path);
     //更新图片表(Pic)---更新缩略图表(Thumb)
     $map = array('path' => $old_real_url);
     M('Pic')->where($map)->setField('path', $new_pic_path);
     $map = array('path' => $old_thumb_url);
     M('Thumb')->where($map)->setField('path', $new_thumb_path);
     //实例化裁剪类
     $Think_img = new ThinkImage(THINKIMAGE_GD);
     // 如果这些参数存在说明有裁减图片
     if (isset($params['w']) && isset($params['h']) && isset($params['x']) && isset($params['y'])) {
         //裁剪原图得到选中区域
         $Think_img->open($tmp_url)->crop($params['w'], $params['h'], $params['x'], $params['y'])->save($tmp_url);
     }
     //同时重新生成缩略图
     $Think_img->open($tmp_url)->thumb(150, 150, 3)->save($new_thumb_path);
     // 复制一份缓存图片保存为原图,然后删除缓存图片
     copy($tmp_url, $new_pic_path);
     unlink($tmp_url);
     $this->success('保存成功');
 }
Exemplo n.º 3
0
 public function cropImg()
 {
     //图片裁剪数据
     $params = I('post.');
     //裁剪参数
     if (!isset($params) && empty($params)) {
         $this->error('参数错误!');
     }
     //头像目录地址
     $path = './Avatar/';
     //要保存的图片
     $real_path = $path . $_SESSION['user']['id'] . 'avatar.jpg';
     //临时图片地址
     $pic_path = $path . 'temp.jpg';
     //实例化裁剪类
     $Think_img = new ThinkImage(THINKIMAGE_GD);
     //裁剪原图得到选中区域
     $Think_img->open($pic_path)->crop($params['w'], $params['h'], $params['x'], $params['y'])->save($real_path);
     //生成缩略图
     $Think_img->open($real_path)->thumb(250, 250, 1)->save($path . $_SESSION['user']['id'] . 'avatar_250.jpg');
     $Think_img->open($real_path)->thumb(150, 150, 1)->save($path . $_SESSION['user']['id'] . 'avatar_150.jpg');
     $Think_img->open($real_path)->thumb(40, 40, 1)->save($path . $_SESSION['user']['id'] . 'avatar_40.jpg');
     //把头像传入数据表
     //判断有无头像
     $_POST['uid'] = $_SESSION['user']['id'];
     $_POST['head_img_middle'] = $_SESSION['user']['id'] . 'avatar_150.jpg';
     $_POST['head_img_small'] = $_SESSION['user']['id'] . 'avatar_40.jpg';
     $_POST['head_img_big'] = $_SESSION['user']['id'] . 'avatar_250.jpg';
     $headimg = $this->isUploadHead($_SESSION['user']['id']);
     if (!$headimg) {
         $headimg = D('head_img');
         $headimg->create();
         $headimg->add();
         $this->success('上传头像成功');
     } else {
         $_POST['id'] = $headimg['id'];
         $headimg = D('head_img');
         $headimg->create();
         $headimg->save();
         $this->success('修改头像成功');
     }
 }