Пример #1
0
 public function crop($file, $x, $y, $width, $height)
 {
     $image = new \Think\Image();
     $image->open('.' . $file);
     //将图片裁剪为$widthx$height并保存为$tmp_file
     $tmp_file = date('Y_m_d_h_i_s') . '.' . $image->type();
     $local_file = './Uploads/tmp/' . $tmp_file;
     $image->crop($width, $height, $x, $y)->save($local_file);
     if (!is_file($local_file)) {
         $this->error('生成裁剪文件失败');
     }
     $res = local_upload($local_file, 'Picture');
     if ($res['status']) {
         $this->success('裁剪成功', '', $res);
     } else {
         $this->error($res['error']);
     }
 }
Пример #2
0
 public function addCourse()
 {
     if (!IS_POST) {
         $this->error('非法请求', U('Index/index'));
     }
     $courseModel = D('course');
     $res = $courseModel->create($_POST, 4);
     if (!$res) {
         $this->ajaxReturn(get_ajax_res(0, '新增课程失败:' . $courseModel->getError()));
     }
     $rootPath = './Uploads/Course/';
     $upload = new \Think\Upload();
     // 实例化上传类
     $upload->maxSize = 2097152;
     // 设置附件上传大小
     $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
     // 设置附件上传类型
     $upload->rootPath = $rootPath;
     // 设置附件上传根目录
     $upload->autoSub = false;
     // 是否自动生成子目录
     // 上传文件
     $info = $upload->upload();
     // 返回错误提示消息
     if (!$info) {
         $this->ajaxReturn(get_ajax_res(0, '新增课程失败:' . $upload->getError()));
     }
     // 获取文件名和路径
     $imageName = $info['course_image']['savename'];
     $imagePath = $rootPath . $imageName;
     // 将课程信息写入数据库
     $courseModel->create_teacher_id = session('UID');
     $courseModel->course_image = $imageName;
     $courseModel->add();
     // 处理课程缩略图
     $image = new \Think\Image();
     $image->open($imagePath);
     $image->thumb(100, 100)->save($imageName . 'thumb.' . $image->type());
     // 返回成功提示
     $this->ajaxReturn(get_ajax_res(1, '新增课程成功'));
 }
Пример #3
0
 public function modifyAvater()
 {
     if (!IS_POST) {
         $this->error('非法请求', U('Index/index'));
     }
     // 获取用户设置的头像坐标
     $coordsW = I('post.coords_w');
     $coordsH = I('post.coords_h');
     $coordsX = I('post.coords_x');
     $coordsY = I('post.coords_y');
     // 获取图片缩放后宽高
     $imagesW = intval(I('post.images_w'));
     $imagesH = intval(I('post.images_h'));
     // 设置上传参数
     $rootPath = './Uploads/Avater/';
     $upload = new \Think\Upload();
     // 实例化上传类
     $upload->maxSize = 2097152;
     // 设置附件上传大小
     $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
     // 设置附件上传类型
     $upload->rootPath = $rootPath;
     // 设置附件上传根目录
     $upload->autoSub = false;
     // 是否自动创建子目录
     $info = $upload->upload();
     // 上传错误提示错误信息
     if (!$info) {
         $this->ajaxReturn(get_ajax_res(0, '头像上传失败:' . $upload->getError()));
     }
     // 获取头像图片信息
     $avaterName = $info['avater']['savename'];
     $avaterPath = $rootPath . $avaterName;
     // 进行图像裁剪
     $imageObj = new \Think\Image();
     $res = $imageObj->open($avaterPath);
     // 设置临时图像路径和新头像路径
     $imageType = $imageObj->type();
     $tempAvater = $rootPath . 'avater_temp.' . $imageType;
     $newAvaterName = $avaterName . '_thubm.' . $imageType;
     $newAvater = $rootPath . $newAvaterName;
     // 先生成临时头像缩略图
     $imageObj->thumb($imagesW, $imagesH)->save($tempAvater);
     $imageObj->open($tempAvater);
     // 后进行头像裁剪
     $res = $imageObj->crop($coordsW, $coordsH, $coordsX, $coordsY)->save($newAvater);
     if ($res) {
         // 成功后删除原图片和临时图片
         unlink($avaterPath);
         unlink($tempAvater);
         $userDB = M('user');
         $map = array('id' => session('UID'));
         $data = array('avater_path' => $newAvaterName);
         $res = $userDB->where($map)->setField($data);
         if ($res) {
             session('avaterPath', $newAvaterName);
         }
         $this->ajaxReturn(get_ajax_res(1, '头像上传成功'));
     } else {
         $this->ajaxReturn(get_ajax_res(0, '头像上传失败,请重试'));
     }
 }
Пример #4
0
 /**
  * 裁剪图片
  * @author jry <*****@*****.**>
  */
 public function crop($data = null)
 {
     $image = new \Think\Image();
     $image->open($data['src']);
     $type = $image->type();
     if ($image) {
         $file = './Runtime/Temp/crop' . \Org\Util\String::randString(12, 1) . '.' . $type;
         $url = U(MODULE_MARK . "/Upload/upload", null, true, true);
         // 图片缩放计算
         $sw = $sh = 1;
         if ($data['vw']) {
             $sw = $image->width() / $data['vw'];
         }
         if ($data['vh']) {
             $sh = $image->height() / $data['vh'];
         }
         // 裁剪并保存
         $image->crop($data['w'] * $sw, $data['h'] * $sh, $data['x'] * $sh, $data['y'] * $sh)->save($file);
         $result = $this->curlUploadFile($url, $file);
         return json_decode($result, true);
     }
 }
Пример #5
0
 public function helpImage($upload)
 {
     //如果使用了多语言功能的话(假设,我们在当前语言包里面定义了'lang_var'=>'标题必须!'),就可以这样定义模型的自动验证
     //array('title','require','{%lang_var}',1),
     //'FILE_FORMAT ' => '文件格式: {$format},文件大小:{$size}',
     //{:L('FILE_FORMAT ',array('format' => 'jpeg,png,gif,jpg','maximum' => '2MB'))}
     // 采用时间戳命名
     $upload->saveName = 'time';
     // 采用GUID序列命名
     $upload->saveName = 'com_create_guid';
     // 采用自定义函数命名
     $upload->saveName = 'myfun';
     // 开启子目录保存 并以日期(格式为Ymd)为子目录
     $upload->autoSub = true;
     $upload->subName = array('date', 'Ymd');
     $image = new \Think\Image();
     $image->open('./1.jpg');
     $width = $image->width();
     // 返回图片的宽度
     $height = $image->height();
     // 返回图片的高度
     $type = $image->type();
     // 返回图片的类型
     $mime = $image->mime();
     // 返回图片的mime类型
     $size = $image->size();
     // 返回图片的尺寸数组 0 图片宽度 1 图片高度
     //裁剪图片
     $image = new \Think\Image();
     $image->open('./1.jpg');
     //将图片裁剪为400x400并保存为corp.jpg
     $image->crop(400, 400)->save('./crop.jpg');
     //使用thumb方法生成缩略图
     $image = new \Think\Image();
     $image->open('./1.jpg');
     // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
     $image->thumb(150, 150)->save('./thumb.jpg');
     //居中裁剪
     $image = new \Think\Image();
     $image->open('./1.jpg');
     // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
     $image->thumb(150, 150, \Think\Image::IMAGE_THUMB_CENTER)->save('./thumb.jpg');
     //添加图片水印
     $image = new \Think\Image();
     $image->open('./1.jpg');
     //将图片裁剪为440x440并保存为corp.jpg
     $image->crop(440, 440)->save('./crop.jpg');
     // 给裁剪后的图片添加图片水印(水印文件位于./logo.png),位置为右下角,保存为water.gif
     $image->water('./logo.png')->save("water.gif");
     // 给原图添加水印并保存为water_o.gif(需要重新打开原图)
     $image->open('./1.jpg')->water('./logo.png')->save("water_o.gif");
     //给图片添加文字水印
     $image = new \Think\Image();
     // 在图片右下角添加水印文字 T hinkPHP 并保存为new.jpg
     $image->open('./1.jpg')->text('T hinkPHP', './1.ttf', 20, '#000000', \Think\Image::IMAGE_WATER_SOUTHEAST)->save("new.jpg");
 }