/** *上传userInfo到数据库 */ public function userInfo() { $upload = new \Think\Upload(); $upload->maxSize = 1000000; $upload->exts = array('jpg', 'png', 'jpeg'); $upload->rootPath = './Public/Bzone/images/head-pic/'; $upload->savePath = ''; $upload->replace = 'true'; $upload->saveName = 'time'; $info = $upload->upload(); if (!$info) { $this->error($upload->getError()); } else { foreach ($info as $file) { $image = new Image(); $image->open('./Public/Bzone/images/head-pic/' . $file['savepath'] . $file['savename']); $width = $image->width(); $height = $image->height(); if ($width <= 130 || $height <= 130) { $image->crop(130, 130)->save('./Public/Bzone/images/head-pic/' . $file['savepath'] . $file['savename']); } else { $image->thumb(130, 130)->save('./Public/Bzone/images/head-pic/' . $file['savepath'] . $file['savename']); } $data['info_pic'] = $file['savepath'] . $file['savename']; $data['info_selfintroduce'] = I('selfInfo'); if (M('userinfo')->where(array('user_id' => session('userId')))->save($data)) { redirect(U('Bzone/showFabu', '', '')); } else { $this->error('上传失败'); } } } }
/** * 上传头像图片 * @author huajie <*****@*****.**> */ public function uploadPhoto() { //TODO: 用户登录检测 /* 返回标准数据 */ $return = array('status' => 1, 'info' => '上传成功', 'data' => ''); /* 调用文件上传组件上传文件 */ $Picture = D('Picture'); $pic_driver = C('PHOTO_UPLOAD_DRIVER'); $info = $Picture->upload($_FILES, C('PHOTO_UPLOAD'), C('PHOTO_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG")); //TODO:上传到远程服务器 /* 记录图片信息 */ if ($info) { $return['status'] = 1; $return = array_merge($info['download'], $return); $image = new Image(); $path = strpos($return['path'], '/') == 0 ? substr($return['path'], 1) : $return['path']; $image->open($path); if ($image->width() > 500 || $image->width > 300) { $image->thumb(500, 300); $image->save($path, null, 100, true); } } else { $return['status'] = 0; $return['info'] = $Picture->getError(); } /* 返回JSON数据 */ echo json_encode($return); //$this->ajaxReturn($return); }
public function img() { //实例化图像处理类,默认为GD库 $image = new Image(); //加载一张预处理的图片 $image->open('./Public/images/1.jpg'); //上面两句,可以用一句话包含 //$image = new Image(Image::IMAGE_GD, './Public/images/1.jpg'); //获取图片信息 $arr['width'] = $image->width(); $arr['height'] = $image->height(); $arr['type'] = $image->type(); $arr['mime'] = $image->mime(); $arr['size'] = $image->size(); //裁剪图片,高400,宽400 $image->crop(400, 400)->save('./Public/images/1.jpg'); $image->open('./Public/images/2.jpg'); $image->thumb(300, 300, Image::IMAGE_THUMB_CENTER)->save('./Public/images/2.jpg'); //在图片右下角添加水印并生成 $image->open('./Public/images/3.jpg'); $image->water('./Public/images/logo.png')->save('./Public/images/3.jpg'); echo '<pre>' . print_r($arr, 1) . '</pre>'; }
public function imginfo() { $path = './Public/images/demo.jpg'; $image = new Image(Image::IMAGE_GD, $path); dump(['width' => $image->width(), 'height' => $image->height(), 'mime' => $image->mime(), 'type' => $image->type()]); }