/** * 上传图片 */ public function uploadPic() { $config = array('maxSize' => 0, 'exts' => array('jpg', 'png', 'gif', 'jpeg'), 'rootPath' => './Upload/', 'driver' => 'LOCAL', 'subName' => array('date', 'Y-m'), 'savePath' => I('dir', 'uploads') . "/"); $upload = new \Think\Upload($config); $rs = $upload->upload($_FILES); if (!$rs) { $this->error($upload->getError()); } else { $images = new \Think\Image(); $images->open('./Upload/' . $rs['Filedata']['savepath'] . $rs['Filedata']['savename']); $newsavename = str_replace('.', '_thumb.', $rs['Filedata']['savename']); $vv = $images->thumb(I('width', 300), I('height', 300), I('thumb_type', 1))->save('./Upload/' . $rs['Filedata']['savepath'] . $newsavename); if (C('RTC_M_IMG_SUFFIX') != '') { $msuffix = C('RTC_M_IMG_SUFFIX'); $mnewsavename = str_replace('.', $msuffix . '.', $rs[$Filedata]['savename']); $mnewsavename_thmb = str_replace('.', "_thumb" . $msuffix . '.', $rs[$Filedata]['savename']); $images->open('./Upload/' . $rs[$Filedata]['savepath'] . $rs[$Filedata]['savename']); $images->thumb(I('width', 700), I('height', 700))->save('./Upload/' . $rs[$Filedata]['savepath'] . $mnewsavename); $images->thumb(I('width', 250), I('height', 250))->save('./Upload/' . $rs[$Filedata]['savepath'] . $mnewsavename_thmb); } $rs['Filedata']['savepath'] = "Upload/" . $rs['Filedata']['savepath']; $rs['Filedata']['savethumbname'] = $newsavename; $rs['status'] = 1; echo json_encode($rs); } }
public function submitSignup() { $upload = new \Think\Upload(C("UPLOAD_USER_PICS_CONFIG")); $info = $upload->uploadOne($_FILES['upic']); if (!$info) { $this->error($upload->getError()); } else { dump($info); $image = new \Think\Image(); $image->open(C("UPLOAD_USER_PICS_CONFIG.rootPath") . $info['savepath'] . $info['savename']); $thumb_url = C("UPLOAD_USER_PICS_CONFIG.rootPath") . $info['savepath'] . 'thumb_' . $info['savename']; $image->thumb(C("THUMB_USER_PIC_WIDTH"), C("THUMB_USER_PIC_HEIGHT"))->save($thumb_url); $_POST['upic'] = $info['savename']; } $_POST['upwd'] = I("upwd", "", md5); if (M("User")->create()) { $uid = M("User")->add(); if ($uid) { session("uid", $uid); session('uname', I('uname')); $this->redirect("Index/index"); } } $this->error("注册失败"); }
public function ajax() { error_reporting(0); $userimgurl = I('post.pho'); unlink('./Public/image/user/' . $userimgurl); $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 3145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->rootPath = './Public/image/user/'; // 设置附件上传根目录 $info = $upload->upload(); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { // 上传成功 $userimgurl = $info['photo']['savepath'] . $info['photo']['savename']; $image = new \Think\Image(); $image->open('./Public/image/user/' . $userimgurl); $image->thumb(180, 180, 3)->save('./Public/image/user/' . $userimgurl); $this->ajaxReturn($userimgurl); } }
public function uploads() { $upload = new \Think\Upload(); $upload->maxSize = 3145728; //设置上传图片大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); //设置附件上传类型 $upload->savePath = "Photo"; //上传(子)目录 $info = $upload->upload(); //上传文件 if (!$info) { $this->error($upload->getError()); } //保存图片名 $_POST['photo_name'] = $info['photo_name']['savepath'] . $info['photo_name']['savename']; $_POST['uid'] = $_SESSION['user']['id']; //用户id //生成缩略图 $image = new \Think\Image(); $bigimage = $info['photo_name']['savepath'] . $info['photo_name']['savename']; $srcimg = $upload->rootPath . $bigimage; $image->open($srcimg); $image->thumb(250, 150, \Think\Image::IMAGE_THUMB_CENTER); $smallimage = $info['photo_name']['savepath'] . 'thumb_' . $info['photo_name']['savename']; $image->save($upload->rootPath . $smallimage); $_POST['photo_name_thumb'] = $smallimage; }
/** * 获取图片 * @param * @return string */ function thumb($img = '', $width = 0, $height = 0) { if (empty($img)) { return __ROOT__ . '/default.jpg'; } $Uploads = '/Uploads/'; $file = '.' . $Uploads . $img; if (file_exists($file)) { if (empty($width)) { return __ROOT__ . substr($file, 1); } else { $pathinfo = pathinfo($file); $thumb_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '_' . $width . '-' . $height . '.' . $pathinfo['extension']; if (file_exists($thumb_file)) { return __ROOT__ . substr($thumb_file, 1); } else { $image = new \Think\Image(); $image->open($file); if (empty($height)) { $height = $image->height(); } $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_CENTER)->save($thumb_file); return __ROOT__ . substr($thumb_file, 1); } } } return __ROOT__ . '/default.jpg'; }
/** * [处理系统配置文件修改] */ public function ModSysHandle() { $upload = new \Think\Upload(); // 实例化上传类 $upload->rootPath = "./Public/"; //rootPath这个路径必须手动创建 $upload->savePath = "Logo/"; //这一块只能设置一层目录 $info = $upload->upload(); if ($info) { // 上传成功 获取上传文件信息 $image = new \Think\Image(); $image->open('./Public/' . $info['face']['savepath'] . $info['face']['savename']); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $image->thumb(230, 45)->save('./Public/Logo/Logo.png'); } $post = $_POST; $str = file_get_contents('Application/Common/Conf/config.php'); //将配置文件中内容读出 foreach ($post as $key => $val) { if (in_array($key, array_keys(C()))) { //判断键值是否存在于配置文件中 $zz[] = "/'{$key}'\\s*.*?,\\n/i"; $rep[] = "'{$key}' => '{$val}',\n"; } } $str = preg_replace($zz, $rep, $str); //在str中匹配 然后替换 //将修改后的文件重新写入到配置文件中 if (file_put_contents('Application/Common/Conf/config.php', $str)) { //$this->success('修改成功',U('System/index'),3); $this->success('修改成功'); } }
public function picAddHandle() { $config = array("savePath" => 'Weixin/', "maxSize" => 1000000, "exts" => array('jpg', 'gif', 'png', 'jpeg'), "subName" => array('date', 'Y/m-d')); $upload = new Upload($config); $info = $upload->upload(); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { // 上传成功 获取上传文件信息 // $file_path_full = Upload_PATH . $info['img']['savepath'] . $info['img']['savename']; $file_path_full = $info['img']['fullpath']; if (!defined('SAE_TMP_PATH')) { // 非SAE环境中 $image = new \Think\Image(); $image->open($file_path_full); $image->thumb(150, 150)->save($file_path_full); $img_url = "http://" . $_SERVER['SERVER_NAME'] . str_replace('index.php', '', __APP__) . $file_path_full; } else { // SAE环境中 $img_url = $file_path_full; } $ACCESS_TOKEN = $this->getAccess(); $post_data = array("media" => '@' . $file_path_full); $URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$ACCESS_TOKEN}&type=image"; $res = json_decode(simple_post($URL, $post_data), true); $data['type'] = $res['type']; $data['mediaId'] = $res['media_id']; $data['picurl'] = $img_url; $res = D('Weixinre')->data($data)->add(); if ($res) { $this->success('上传成功!', U('Weixin/Reply/index')); } } }
public function edit() { //实例化上传类 $upload = new \Think\Upload(); $upload->maxSize = 3145728; //设置上传图片大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); //设置附件上传类型 $upload->savePath = "First"; //上传(子)目录 $info = $upload->upload(); //上传文件 if (!$info) { $this->error($upload->getError()); } //保存图片名 $data['first'] = $info['first']['savepath'] . $info['first']['savename']; $data['uid'] = $_SESSION['user']['id']; //用户id //生产缩略图 $image = new \Think\Image(); $bigimage = $info['first']['savepath'] . $info['first']['savename']; $srcimg = $upload->rootPath . $bigimage; $image->open($srcimg); $image->thumb(250, 180, \Think\Image::IMAGE_THUMB_CENTER); $smallimage = $info['first']['savepath'] . 'thumb_' . $info['first']['savename']; $image->save($upload->rootPath . $smallimage); $id = $_POST['id']; $album = D('Album'); $album->where(array('id' => $id))->setField('first', $smallimage); $this->redirect('Album/index'); }
function get_thumb($url, $width, $height) { $name = end(explode("/", $url)); $image = new \Think\Image(); $image->open($url); $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_CENTER)->save($name); return $name; }
/** * @param $cate * @return mixed */ public function addAtc($cate) { //$title = $this->title; $upload = new \Think\Upload($this->config); $upload->saveName = ['Common\\Model\\PicgridModel::picgridName']; //$_POST['author']; $upload->subName = ['Common\\Model\\PicgridModel::picgridSub']; //$_POST['author']; $info = $upload->upload(); //$upload->saveName=$this->picgridName(); $cate_atc = M('cate_atc'); //引索存储 $image = new \Think\Image(); //制造缩略图 if (!$info) { // 上传错误提示错误信息 $this->error = $upload->getError(); } else { // 上传成功 获取上传文件信息 foreach ($info as $file) { // $file['title'] = $_POST['title']; // $file['content'] = $_POST['content']; $file = array_merge($file, I('post.')); $this->create($file); $atc_id = $this->add(); if ($atc_id == false) { $this->error = '存储失败'; return false; } $picName = './Public' . $file['savepath'] . $file['savename']; $picThunbName = './Public' . $file['savepath'] . 'thumb/' . $file['savename']; chmod('./Public' . $file['savepath'], 0777); chmod($picName, 0777); //echo './Public/'.substr($file['savepath'],2).'thumb/hehe.jpg'; $image->open($picName); if (!file_exists('./Public/' . $file['savepath'] . 'thumb')) { mkdir('./Public' . $file['savepath'] . 'thumb'); chmod('./Public' . $file['savepath'] . 'thumb', 0777); } $image->thumb(500, 600)->save($picThunbName); chmod($picThunbName, 0777); $cate_atc->atc_id = $atc_id; $cate_atc->cate = $cate; $cate_atc->title = $_POST['title']; //$file['savename'] ; $cate_atc->createtime = date('y-m-d H:i:s'); $cate_atc->model_id = $this->mid; $cate_atc->status = 1; if ($cate_atc->add()) { return true; } else { $this->delete($atc_id); $this->error = '添加失败'; return false; } } } }
public function teacherImageCrop($path, $ext, $width, $height) { //$path是要处理的文件路径 $image = new \Think\Image(); $res = $image->open($path); //将图片裁剪为100x100并保存为corp.jpg $thumb_url = (string) time() . (string) rand() . '.' . $ext; $image->thumb($width, $height)->save('TeacherImages/thumb/' . $thumb_url); return $thumb_url; }
public function update() { $id = I('id', 0, 'int'); $mod = M('photo'); $photo = M('photo')->where('id=' . $id)->find(); unlink("./Public/Uploads/pic/" . $photo['url']); unlink("./Public/Uploads/pic/" . $photo['bigurl']); if ($_FILES['pic']['name'] != "") { $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 3145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); //设置附件上传类型 $upload->rootPath = './Public/'; // 设置附件上传根目录 $upload->savePath = './Uploads/pic/'; // 设置附件上传目录 $upload->autoSub = true; //关闭子目录生成 $upload->subName = array('date', 'Y-m-d'); //子目录生成规则 $info = $upload->upload(); if (!$info) { echo $this->error($upload->getError()); } else { //上传图片成功 $time = date('Y-m-d'); $file = $info['pic']; var_dump($_FILES['pic']); die; $_POST['bigurl'] = $time . "/" . $file['savename']; $pic = __ROOT__ . "/Public/" . $file['savepath'] . $file['savename']; $img = new \Think\Image(); $img->open("./Public/" . $file['savepath'] . $file['savename']); $img->thumb(300, 300)->save("./Public/" . $file['savepath'] . "/s_" . $file['savename']); $_POST['url'] = $time . "/s_" . $file['savename']; } $_POST['time'] = time(); if ($mod->create()) { $mod->save(); $this->success("添加成功"); } else { $this->success("添加失败"); } } else { $_POST['time'] = time(); if ($mod->create()) { $mod->save(); $this->success("添加成功"); } else { $this->success("添加失败"); } } }
public function index() { if (!IS_POST) { date_default_timezone_set("Asia/Chongqing"); error_reporting(E_ERROR); header("Content-Type: text/html; charset=utf-8"); $CONFIG = json_decode(preg_replace("/\\/\\*[\\s\\S]+?\\*\\//", "", file_get_contents('.' . C('JS') . "ueditor1_4_3-utf8-php/php/config.json")), true); $action = I('get.action'); switch ($action) { case 'config': $result = json_encode($CONFIG); echo $result; break; case 'listimage': $result = new \Vendor\Imageslist(); // 实例化上传类 echo $result; break; default: $this->display(); break; } } else { //本地配置 $config = array('rootPath' => C('rootPath'), 'savePath' => '', 'saveName' => array('uniqid', ''), 'exts' => explode(',', C('upload_exts')), 'autoSub' => false); $type = 'Local'; $upload = new \Think\Upload($config, $type); // 实例化上传类 $info = $upload->upload(); //print_r($info); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { // 上传成功 foreach ($info as $file) { $imgurl = C('rootPath') . $file['savepath'] . $file['savename']; $image = new \Think\Image(); if (C('IS_thumb') == '1') { $image->open($imgurl); $image->thumb(C('thumb_width'), C('thumb_height'), C('thumb_leixin'))->save($imgurl); } if (C('IS_water') == '1') { if (C('water_lbs') == '10') { $lbs = rand(1, 9); } $image->open($imgurl)->water('.' . C('water'), $lbs, C('water_touming'))->save($imgurl); } echo json_encode(array('status' => 1, 'error' => 0, 'url' => str_replace('./', '/', $imgurl), 'original' => $file['name'], 'state' => 'SUCCESS')); } } } }
/** * 图片上传处理 * @param $isThumb 是否需要缩略图,默认true * @return [type] [description] */ function uploadImg(&$data, $options, $isThumb = true) { //设置上传文件的大小 $iniFileSize = intval(ini_get('upload_max_filesize')); //1 php.ini配置文件的上传文件大小 $localFileSize = C('MAX_FILE_SIZE'); //2 获取配置文件中的自定义上传文件大小 $allowFileSize = intval(min(iniFileSize, localFileSize)); //3 获取最小值 //设置上传根目录 $rootPath = C('ROOT_IMG_PATH'); //获取允许上传图片的后缀 $ext = C('ALLOW_IMG_EXT'); //设置配置信息 $config = array('maxSize' => $allowFileSize * 1024 * 1024, 'rootPath' => $rootPath, 'savePath' => 'Goods/', 'saveName' => array('uniqid', ''), 'exts' => $ext, 'subName' => array('date', 'Ymd'), 'autoSub' => true); $upload = new \Think\Upload($config); // 实例化上传类 $info = $upload->upload(); //获取上传文件信息 $filed = array_keys($info); //获取附件上传的表单name,对应数据表中的字段 //var_dump($filed[0]);exit; //判断是否获取成功 //上传错误信息4:代表没有文件上传 if ($info) { //保存路径 $imgName = $info[$filed[0]]['savepath'] . $info[$filed[0]]['savename']; //存入数据库 $data[$filed[0]] = $imgName; //判断是否需要缩略图 if ($isThumb) { //设置缩略图 $image = new \Think\Image(); //1 实例化Image对象 $image->open($rootPath . $imgName); //2 获取图像文件进行操作 //设置缩略图文件名 $thumbImgName = $info[$filed[0]]['savepath'] . 'thumb_' . $info[$filed[0]]['savename']; //保存缩略图到指定路径 $image->thumb(150, 150)->save($rootPath . $thumbImgName); //存入数据库 $data['goods_thumb'] = $thumbImgName; } //存储完毕,返回真 return array(true, '图片与缩略图上传成功'); } else { $error = $upload->getError(); // 获取上传的错误信息 return array(false, $error); //$this->error = $error; // 然后通过模型的getError()方法获取错误信息 } }
/** * swfupload 上传 */ public function swfupload() { if (IS_POST) { //上传处理类 $config = array('rootPath' => './' . C("UPLOADPATH"), 'savePath' => '', 'maxSize' => 11048576, 'saveName' => array('uniqid', ''), 'exts' => array('jpg', 'gif', 'png', 'jpeg', "txt", 'zip'), 'autoSub' => true); $upload = new \Think\Upload($config); // $info = $upload->upload(); //开始上传 if ($info) { //上传成功 //写入附件数据库信息 $first = array_shift($info); if (!empty($first['url'])) { $url = $first['savepath'] . $first['url']; } else { $url = C("TMPL_PARSE_STRING.__UPLOAD__") . $first['savepath'] . $first['savename']; } $textareaid = I('post.textareaid'); if ('goods_photos' === $textareaid) { // 商品缩略图 $bigimg = $first['savepath'] . $first['savename']; $image = new \Think\Image(); $srcimg = $upload->rootPath . $bigimg; $image->open($srcimg); $site_options = get_site_options(); $image->thumb($site_options['goods_thumb_width'], $site_options['goods_thumb_height']); $dirname = './' . C("UPLOADPATH_S") . $first['savepath']; if (!is_dir($dirname)) { mkdir($dirname); } $image->save($dirname . $first['savename']); } echo "1," . $url . "," . '1,' . $first['name']; exit; } else { //上传失败,返回错误 exit("0," . $upload->getError()); } } else { if (isset($_GET['file_upload_limit'])) { $file_upload_limit = I('get.file_upload_limit'); } else { $file_upload_limit = 1; } $textareaid = I('get.textareaid'); $this->assign('file_upload_limit', $file_upload_limit); $this->assign('textareaid', $textareaid); $this->display(':swfupload'); } }
public function insert() { $upload = new \Think\Upload(); $upload->autoSub = false; $upload->maxSize = 3145782; $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); $upload->rootPath = 'Public'; $upload->savePath = '/Uploads/posts/'; $info = $upload->upload(); if (!$info) { $this->error($upload->getError()); } else { //文件上传成功则生成缩略图,并把图片,标题等信息加入到数据库 foreach ($info as $file) { $img = "Public" . $file['savepath'] . $file['savename']; $image = new \Think\Image(); $image->open($img); $img2 = "Public" . $file['savepath'] . 's_' . $file['savename']; $img3 = "Public" . $file['savepath'] . 'a_' . $file['savename']; $image->thumb(730, 454)->save($img2); //在缩略图前加前缀 “s_” ! $image->thumb(150, 150)->save($img3); //在缩略图前加前缀 “s_” ! @unlink($img); $_POST['pic'] = $file['savename']; $_POST['addtime'] = time(); $_POST['user_id'] = 0; $posts = M('posts'); if ($posts->create()) { if ($posts->add()) { $this->redirect('posts/add'); } else { $this->redirect('posts/add'); } } } } }
function dothumb($picpath) { $arr = explode('/', $picpath); $filename = array_pop($arr); $big_name = get_big_name($filename); $imginfo = getImageSize($picpath); $imgw = $imginfo[0]; $imgh = $imginfo[1]; $image = new \Think\Image(); $image->open($picpath); $image->save('./Public/allimage/' . $big_name); $image->thumb(300, 300, \Think\Image::IMAGE_THUMB_SCALE)->save('./Public/allimage/' . $filename); unlink($picpath); return $filename; }
public function examimg() { $upload = new \Think\Upload(); $upload->maxSize = 3145728; $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); $upload->savePath = 'examimg/'; $info = $upload->upload(); var_dump($info); $src = './Uploads/' . $info['img']['savepath'] . $info['img']['savename']; echo "<img src='" . $src . "''>"; var_dump(extension_loaded()); $image = new \Think\Image(); $image->open($src); $image->thumb(300, 200)->save($src); echo "<img src='" . $src . "'' width='453' height='271'>"; }
public function CourseAddAction() { if (!IS_POST) { $this->error('非法请求', U('Index/index')); } $rules = array(array('course_name', 'require', '请填写课程名称', 0, '', 4), array('course_name', '0,12', '课程名称在12个字符以内', 0, 'length', 4), array('course_about', 'require', '请填写课程简介', 0, '', 4), array('course_about', '0,200', '课程名称在200个字符以内', 0, 'length', 4)); $courseModel = M('course'); $res = $courseModel->validate($rules)->create(); if (!$res) { $this->ajaxReturn(get_ajax_res(0, '添加课程失败:' . $courseModel->getError())); } $rootPath = './Uploads/Course/'; if (!is_dir($rootPath)) { mkdir($rootPath); } $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->teacher_id = session('UID'); $courseModel->course_image = $imageName; $courseModel->create_time = NOW_TIME; $courseModel->teacher_name = session('truename'); $courseModel->add(); // 处理课程缩略图 $imageObject = new \Think\Image(); $imageObject->open($imagePath); $imageObject->thumb(100, 100)->save($rootPath . $imageName); // 返回成功提示 $this->ajaxReturn(get_ajax_res(1, '新增课程成功')); }
/** * 生成缩略图 * @param $pic 图片资源路径 * @param null $path 保存路径 * @param null $savename 图片名字 */ public function thumb($pic, $info = array()) { $image = new \Think\Image(); $image->open($pic); $image->thumb(380, 380); $savepath = APP_PATH . $info['path']; $name = 's_' . $info['name']; if (!is_dir($savepath)) { mkdir($savepath, 0, true); } $image->save($savepath . $name); $data = array(); $data['name'] = $name; $data['path'] = $savepath; $data['pic_url'] = $info['savepath'] . $name; return $data; }
public function insert() { if ($_SESSION['user']) { $Liuyan = new \Home\Controller\LiuyanController(); $Liuyan->index(); die; } $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 3145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); //设置附件上传类型 $upload->rootPath = './Public/'; // 设置附件上传目录 $upload->savePath = './Uploads/pic/'; // 设置附件上传目录 $upload->autoSub = false; //关闭子目录生成 // 上传文件 $info = $upload->upload(); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { $file = $info['pic']; //获取当前文件的上传信息 $pic = __ROOT__ . "/Public/" . $file['savepath'] . $file['savename']; //生产缩略图 $img = new \Think\Image(); $img->open("./Public/" . $file['savepath'] . $file['savename']); //$img->thumb(300,300)->save("./Public/".$file['savepath'].'c_'.$file['savename']); $img->thumb(100, 100)->save("./Public/" . $file['savepath'] . $file['savename']); $_POST['shengri'] = $_POST['ageyear'] . '-' . $_POST['agemonth'] . '-' . $_POST['ageday']; $_POST['password'] = md5($_POST['password']); $guest = D('guest'); $_POST['bigpic'] = $file['savename']; $_POST['logintime'] = time(); $guest->create(); $id = $guest->add(); $user = $guest->where('id=' . $id)->find(); session('user', $user); $Liuyan = new \Home\Controller\LiuyanController(); $Liuyan->index(); } }
/** * 上传图片 */ public function uploadPic() { $config = array('maxSize' => 0, 'thumb ' => false, 'exts' => array('jpg', 'png', 'gif', 'jpeg'), 'rootPath' => './Upload/', 'driver' => 'LOCAL', 'subName' => array('date', 'Y-m'), 'savePath' => I('dir', 'uploads') . "/"); $upload = new \Think\Upload($config); $rs = $upload->upload($_FILES); if (!$rs) { $this->error($upload->getError()); } else { $images = new \Think\Image(); //生成缩略图,并修改文件名 $images->open('./Upload/' . $rs['Filedata']['savepath'] . $rs['Filedata']['savename']); $newsavename = str_replace('.', '_thumb.', $rs['Filedata']['savename']); $vv = $images->thumb(I('width', 100), I('height', 100), I('thumb_type', 1))->save('./Upload/' . $rs['Filedata']['savepath'] . $newsavename); $rs['Filedata']['savepath'] = "Upload/" . $rs['Filedata']['savepath']; $rs['Filedata']['savethumbname'] = $newsavename; $rs['status'] = 1; echo json_encode($rs); } }
function cut_image($img, $width, $height, $type = 3) { if (empty($width) && empty($height)) { return $img; } $imgDir = realpath(ROOT_PATH . $img); if (!is_file($imgDir)) { return $img; } $imgInfo = pathinfo($img); $newImg = $imgInfo['dirname'] . '/cut_' . $width . '_' . $height . '_' . $imgInfo["basename"]; $newImgDir = ROOT_PATH . $newImg; if (!is_file($newImgDir)) { $image = new \Think\Image(); $image->open($imgDir); $image->thumb($width, $height, $type)->save($newImgDir); } return $newImg; }
/** * 处理图像上传 * @param string $fileKey 比如是input标签中name的值 * @param int $maxSize 图像最大允许的大小 * @param string $rootPath 图片保存的根目录 * @param array $thumbSize 如果需要缩略图,则传入缩略后的长和宽数组, * 不需要则传入空值 * @return array|string 如果成功返回[相对文件路径],否则返回错误信息 */ protected function handleImgUpload($fileKey, $maxSize, $rootPath, $thumbSize = []) { $config = ['maxSize' => $maxSize, 'rootPath' => $rootPath, 'savePath' => '', 'saveName' => ['uniqid', ''], 'exts' => ['jpg', 'png', 'jpeg', 'bmp'], 'mines' => ['image/jpeg', 'image/png', 'image/nbmp', 'application/x-MS-bmp', 'image/vnd.wap.wbmp'], 'autoSub' => true, 'subName' => ['date', 'Y/md']]; $upload = new \Think\Upload($config); // 上传单个文件 $info = $upload->uploadOne($_FILES[$fileKey]); // 上传错误提示错误信息 if (!$info) { return $upload->getErro(); } // 上传成功 获取上传文件信息 $filepath = $info['savepath'] . $info['savename']; // 是否需要裁剪 if ($thumbSize) { $image = new \Think\Image(); $image->open($rootPath . $filepath); $image->thumb($thumbSize[0], $thumbSize[1], \Think\Image::IMAGE_THUMB_FIXED)->save($rootPath . $filepath); } return [$filepath]; }
public function saveajax() { $id = I('post.id', 0, 'int'); $data['title'] = I('post.title'); $data['c_id'] = I('post.c_id', 0, 'int'); $data['is_use'] = I('post.is_use', 0, 'int'); $data['keyword'] = I('post.keyword'); $data['desc'] = I('post.desc'); $data['content'] = I('post.content'); $data['picurl'] = I('post.picurl'); $data['is_top'] = I('post.is_top'); $data['createtime'] = NOW_TIME; //生成缩略图 if ($data['picurl']) { $image = new \Think\Image(); $image->open($_SERVER['DOCUMENT_ROOT'] . $data['picurl']); $path = dirname($data['picurl']) . '/thumb/'; if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $path)) { mkdir($_SERVER['DOCUMENT_ROOT'] . $path); } $data['s_picurl'] = $path . basename($data['picurl']); $image->thumb(100, 100)->save($_SERVER['DOCUMENT_ROOT'] . $data['s_picurl']); } else { unset($data['picurl']); } $ArticleModel = D('article'); if ($id) { $result = $ArticleModel->setOne($id, $data); if (false === $result) { $this->showerror('未知错误'); } $this->showok('更新成功', true); } else { $result = $ArticleModel->addOne($data); if (false === $result) { $this->showerror('未知错误'); } $this->showok('新建成功', true); } }
public function upload() { $stunum = I('post.stunum'); $name = I('post.name'); $sex = I('post.sex'); if ($sex == '1') { $sex = '女'; } else { if ($sex == '0') { $sex = '男'; } } $file = substr(strrchr($_FILES['picture']['name'], '.'), 1); if ($stunum && $name && $sex && $file) { if ($file == 'jpg' || $file == 'jpeg' || $file == 'png' || $file == 'gif') { $filename = $stunum . "." . $file; $path1 = "Public/upimage/" . $filename; move_uploaded_file($_FILES['picture']['tmp_name'], $path1); $bigname = $stunum . "_big." . $file; // $path2 = "Public/upimage/".$bigname; $imginfo = getImageSize($path1); $imgw = $imginfo[0]; $imgh = $imginfo[1]; $image = new \Think\Image(); $image->open($path1); $image->save('./Public/allimage/' . $bigname); $image->thumb(300, 300, \Think\Image::IMAGE_THUMB_SCALE)->save('./Public/allimage/' . $filename); // unlink($path1); $data = ['pic' => $filename, 'big_pic' => $bigname, 'uid' => $stunum, 'vote' => 0, 'sex' => $sex, 'time' => date('Y-m-d H:i:s', time()), 'is_pass' => 0]; M('image')->add($data); $data = ['stu_name' => $name, 'password' => '', 'vote_day' => '', 'uid' => $stunum, 'sex' => $sex, 'has_upload' => 1]; M('user')->add($data); echo "<script> alert('成功'); window.location.href='" . U('Uploadpic/index') . "'</script>"; } else { echo "<script>\n alert('格式不对');\n window.location.href='" . U('Uploadpic/index') . "'</script>"; } } else { echo "<script>\n alert('信息不完整');\n window.location.href='" . U('Uploadpic/index') . "'</script>"; } }
/** * @param $path 设置路径 * @param $width 缩略图剪裁宽度 * @param $height 缩略图剪裁高度 * 图片上传处理 */ public function _upload($path, $width, $height) { $path = './Public/Uploads/' . $path . '/'; $minPath = './Public/Uploads/' . $path . '/Min/'; $upload = new \Think\Upload(); $upload->maxSize = 3145728; $upload->exts = array('jpg', 'jpeg', 'png', 'gif'); $upload->rootPath = $path; $info = $upload->upload(); if (!$info) { $this->error($upload->getError()); } else { foreach ($info as $file) { echo $file['savepath'] . $file['savename']; } } p($file); // echo '<br/>' . $path . $file['savepath'].$file['savename']; $image = new \Think\Image(); $image->open($path . $file['savepath'] . $file['savename']); $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_CENTER)->save($minPath . 'min_' . $file['savename']); }
public function ajaxupload() { if (isset($_POST["PHPSESSID"])) { session_id($_POST["PHPSESSID"]); } else { exit; } $upload = new \Think\Upload(); $upload->maxSize = 3145728; $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); $data = array(); $info = $upload->uploadOne($_FILES['Filedata']); if (!$info) { $data = array('msg' => 0, "msgbox" => $upload->getError()); } else { $image = new \Think\Image(); $image->open(C('UPLOAD_SAVE_PATH') . $info['savepath'] . $info['savename']); $image->thumb(800, 800)->save(C('UPLOAD_SAVE_PATH') . $info['savepath'] . 'tc8' . $info['savename']); @unlink(C('UPLOAD_SAVE_PATH') . $info['savepath'] . $info['savename']); $data = array('msg' => 1, "msgbox" => $info['savepath'] . 'tc8' . $info['savename']); } echo json_encode($data); }
/** * 缩略图 * @param unknown $img_path * @param unknown $thumb_w * @param unknown $save_path * @param string $is_del */ function thumb_img($img_path, $thumb_w, $save_path, $is_del = true) { $image = new \Think\Image(); $image->open($img_path); $width = $image->width(); // 返回图片的宽度 if ($width > $thumb_w) { $width = $width / $thumb_w; //取得图片的长宽比 $height = $image->height(); $thumb_h = ceil($height / $width); } //如果文件路径不存在则创建 $save_path_info = pathinfo($save_path); if (!is_dir($save_path_info['dirname'])) { mkdir($save_path_info['dirname'], 0777); } $image->thumb($thumb_w, $thumb_h)->save($save_path); if ($is_del) { @unlink($img_path); } //删除源文件 }
public function upload() { $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 3145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->rootPath = './Public/Uploads/'; // 设置附件上传目录 // 上传单个文件 $info = $upload->uploadOne($_FILES['uploadImg']); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { // 上传成功 获取上传文件信息 $image = new \Think\Image(); $image->open($upload->rootPath . $info['savepath'] . $info['savename']); $image->thumb(300, 165, \Think\Image::IMAGE_THUMB_FIXED)->save($upload->rootPath . $info['savepath'] . 'sm_' . $info['savename']); echo $info['savepath'] . 'sm_' . $info['savename']; } }