function avatar() { $users_model = M("Users"); $id = I("get.id", 0, "intval"); $find_user = $users_model->field('avatar')->where(array("id" => $id))->find(); $avatar = $find_user['avatar']; $image = new \Think\Image(); $should_show_default = false; if (empty($avatar)) { $should_show_default = true; } else { if (strpos($avatar, "http") === 0) { header("Location: {$avatar}"); exit; } else { $avatar_dir = C("UPLOADPATH") . "avatar/"; $avatar = $avatar_dir . $avatar; if (file_exists($avatar)) { $image->open($avatar); $mime = $image->mime(); header("Content-type: {$mime}"); $image->save(null); } else { $should_show_default = true; } } } if ($should_show_default) { $image->open("statics/images/headicon.png"); $mime = $image->mime(); header("Content-type: {$mime}"); $image->save(null); } exit; }
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; }
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 savepic($filename) { $picpath = './Public/upimage/' . $filename; $image = new \Think\Image(); $image->open('./Public/upimage/' . $filename); $image->save('./Public/allimage/' . $filename); unlink($filename); }
/** * 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'); } }
/** * 生成缩略图 * @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 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>"; } }
function avatar_update() { if (!empty($_SESSION['avatar'])) { $targ_w = intval($_POST['w']); $targ_h = intval($_POST['h']); $x = $_POST['x']; $y = $_POST['y']; $jpeg_quality = 90; $avatar = $_SESSION['avatar']; $avatar_dir = C("UPLOADPATH") . "avatar/"; if (sp_is_sae()) { //TODO 其它存储类型暂不考虑 $src = C("TMPL_PARSE_STRING.__UPLOAD__") . "avatar/{$avatar}"; } else { $src = $avatar_dir . $avatar; } $avatar_path = $avatar_dir . $avatar; if (sp_is_sae()) { //TODO 其它存储类型暂不考虑 $img_data = sp_file_read($avatar_path); $img = new \SaeImage(); $size = $img->getImageAttr(); $lx = $x / $size[0]; $rx = $x / $size[0] + $targ_w / $size[0]; $ty = $y / $size[1]; $by = $y / $size[1] + $targ_h / $size[1]; $img->crop($lx, $rx, $ty, $by); $img_content = $img->exec('png'); sp_file_write($avatar_dir . $avatar, $img_content); } else { $image = new \Think\Image(); $image->open($src); $image->crop($targ_w, $targ_h, $x, $y); $image->save($src); } $userid = sp_get_current_userid(); $result = $this->users_model->where(array("id" => $userid))->save(array("avatar" => $avatar)); $_SESSION['user']['avatar'] = $avatar; if ($result) { $this->success("头像更新成功!"); } else { $this->error("头像更新失败!"); } } }
function add() { $Goods = D('Goods'); if (!empty($_POST)) { if (!empty($_FILES)) { $config = array('rootPath' => './public/', 'savePath' => 'Uploads/'); $UpLoad_Img = new \Think\Upload($config); $UpLoad_Img_re = $UpLoad_Img->uploadOne($_FILES['goods_Img']); if ($UpLoad_Img_re === false) { show_bug($UpLoad_Img->getError()); } else { //show_bug($UpLoad_Img_re); $Img_URL = $UpLoad_Img_re['savepath'] . $UpLoad_Img_re['savename']; $_POST['goods_big_img'] = $Img_URL; } //生成图片缩略图 $Small_Img = new \Think\Image(); //打开将要缩略的图 $imgname = $UpLoad_Img->rootPath . $Img_URL; $Small_Img->open($imgname); //生成缩略图片 $Small_Img->thumb(120, 120); //保存缩略图 $Img_small_URL = $UpLoad_Img_re['savepath'] . 'small_' . $UpLoad_Img_re['savename']; //通过$Upload_Img类的_get()方法可以直接调用rootPath; $Small_Img->save($UpLoad_Img->rootPath . $Img_small_URL); $_POST['goods_small_img'] = $Img_small_URL; } $Goods->create(); $data = $Goods->add(); if ($data) { echo 'sucess'; } else { echo 'fail'; } } else { } $this->display(); }
function thumb($image = null, $width = 0, $height = 0, $mode = 3) { if (!$image) { $defaults = D('Banner')->getBannerByName('default_thumb'); if ($defaults) { $image = $defaults[rand(0, count($defaults) - 1)]['path']; } } if (!is_file(C('WWW_ROOT') . $image)) { return ''; } $image = '/' . trim($image, '/'); // gif图片不处理 if (stripos($image, '.gif') !== false) { return $image; } if (!$width && !$height) { return $image; } if (C('IMAGE_CUT_TYPE') == 'image') { $mode = 'i'; } $postfix = '-' . $width . '-' . $height . '-' . $mode; $result_image = preg_replace('#(\\w+)\\.(\\w+)$#', "\\1{$postfix}.\\2", $image); $result_path = C('WWW_ROOT') . $result_image; if (!file_exists($result_path)) { if (C('IMAGE_CUT_TYPE') == 'image') { Org\Util\Image::convert(C('WWW_ROOT') . $image, $result_path, $width, $height); } else { // support png transparent $obj = new \Think\Image(C('WWW_ROOT') . $image); $obj->thumb($width, $height, $mode); $obj->save($result_path); } } return $result_image; }
public function uploadAction($ph) { //在操作者不知道要点击上传按钮时执行此方法 $upload = new \Think\Upload(); $upload->maxSize = 5145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->rootPath = './Uploads/'; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->uploadOne($ph); if (!$info) { $this->error('文章封面上传有误!'); } else { //把已上传的图片产生缩略图 $smailImg = new \Think\Image(); $bigImg = $info['savepath'] . $info['savename']; $ImgUrl = $upload->rootPath . $bigImg; //拼接要打开图片的路径 $smailImg->open($ImgUrl); $smailImg->thumb(900, 500); //设置图片大小 $smImg = $upload->rootPath . $info['savepath'] . "fm_" . $info['savename']; //保存缩略的图片路径 $sm = $_SERVER['HTTP_HOST'] . $upload->rootPath . $info['savepath'] . "fm_" . $info['savename']; $smailImg->save($smImg); unlink($ImgUrl); //删除之前上传过的原图 return $smImg; } }
protected function _thumb1($file, $imgname, $width, $height) { $image = new \Think\Image(); if (!empty($file)) { $image->open($file); $image->thumb($width, $height, 1); $image->save($imgname); } }
/** * 上传文件 * @param 文件信息数组 $files ,通常是 $_FILES数组 */ public function upload($files='') { if('' === $files){ $files = $_FILES; } if(empty($files)){ $this->error = '没有上传的文件!'; return false; } /* 检测上传根目录 */ if(!$this->uploader->checkRootPath($this->rootPath)){ $this->error = $this->uploader->getError(); return false; } /* 检查上传目录 */ if(!$this->uploader->checkSavePath($this->savePath)){ $this->error = $this->uploader->getError(); return false; } /* 逐个检测并上传文件 */ $info = array(); if(function_exists('finfo_open')){ $finfo = finfo_open ( FILEINFO_MIME_TYPE ); } // 对上传文件数组信息处理 $files = $this->dealFiles($files); foreach ($files as $key => $file) { $file['name'] = strip_tags($file['name']); if(!isset($file['key'])) $file['key'] = $key; /* 通过扩展获取文件类型,可解决FLASH上传$FILES数组返回文件类型错误的问题 */ if(isset($finfo)){ $file['type'] = finfo_file ( $finfo , $file['tmp_name'] ); } /* 获取上传文件后缀,允许上传无后缀文件 */ $file['ext'] = pathinfo($file['name'], PATHINFO_EXTENSION); /* 文件上传检测 */ if (!$this->check($file)){ continue; } /* 获取文件hash */ if($this->hash){ $file['md5'] = md5_file($file['tmp_name']); $file['sha1'] = sha1_file($file['tmp_name']); } /* 调用回调函数检测文件是否存在 */ $data = call_user_func($this->callback, $file); if( $this->callback && $data ){ if ( file_exists('.'.$data['path']) ) { $info[$key] = $data; continue; }elseif($this->removeTrash){ call_user_func($this->removeTrash,$data);//删除垃圾据 } } /* 生成保存文件名 */ $savename = $this->getSaveName($file); if(false == $savename){ continue; } else { $file['savename'] = $savename; } /* 检测并创建子目录 */ $subpath = $this->getSubPath($file['name']); if(false === $subpath){ continue; } else { $file['savepath'] = $this->savePath . $subpath; } /* 对图像文件进行严格检测 */ $ext = strtolower($file['ext']); if(in_array($ext, array('gif','jpg','jpeg','bmp','png','swf'))) { $imginfo = getimagesize($file['tmp_name']); if(empty($imginfo) || ($ext == 'gif' && empty($imginfo['bits']))){ $this->error = '非法图像文件!'; continue; } } /* 保存文件 并记录保存成功的文件 */ //缩略图 if($this->thumb){ mk_dir($this->rootPath); /* move_uploaded_file($file['tmp_name'],$this->rootPath.$savename); $file['tmp_name'] = $this->rootPath.$savename; */ //有缩略图先生成缩略图 $basename = basename($file['savename']); if(move_uploaded_file($file['tmp_name'],$basename)){ $image = getimagesize($basename); if (false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',', $this->thumbMaxWidth ? $this->thumbMaxWidth : C('thumbMaxWidth')); $thumbHeight = explode(',', $this->thumbMaxHeight ? $this->thumbMaxHeight : C('thumbMaxHeight')); //原图压缩 if($this->thumbPrefix=='Original'){ $thumbPrefix[] = ''; }else{ $thumbPrefix = explode(',', $this->thumbPrefix ? $this->thumbPrefix : C('thumbPrefix')); } $thumbSuffix = explode(',', $this->thumbSuffix); $thumbFile = explode(',', $this->thumbFile); // 生成图像缩略图 $realFilename = $this->autoSub ? basename($file['savename']) : $file['savename']; $file_name = substr($realFilename, 0, strrpos($realFilename, '.')); $ext = substr($realFilename,strrpos($realFilename, '.'));//后缀 for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { //[缩略图名 = 缩略图前缀 + 文件名 + 缩略图后缀名 + 文件后缀] $thumbname = $thumbPrefix[$i] . $file_name . $thumbSuffix[$i] . $ext; //生成缩略图 $Image = new \Think\Image(1,$realFilename); $Image->thumb($thumbWidth[$i],$thumbHeight[$i]); //dump(basename($thumbname)); $thumb = $Image->save(basename($thumbname)); //原图改变为上一张缩略图 $realFilename = $thumbname; //组装数组 $thumbs[] = basename($thumbname); } if(!$this->thumbRemoveOrigin){ //原图也上传 $thumbs[] = basename($basename); }else{ unlink(basename($basename)); } //移动上传图片 foreach($thumbs as $thumbname){ $file['tmp_name'] = $thumbname; $file['savename'] = $thumbname; if ($this->uploader->save($file,$this->replace)) { unset($file['error'], $file['tmp_name']); } else { $this->error = $this->uploader->getError(); } //删除原件 if(file_exists($thumbname)){ unlink(basename($thumbname)); } } $info[$key] = $file; } }else{ $this->error = 'FTP上传失败'; return false; } }else{ if ($this->uploader->save($file,$this->replace)) { unset($file['error'], $file['tmp_name']); $info[$key] = $file; } else { $this->error = $this->uploader->getError(); } } } if(isset($finfo)){ finfo_close($finfo); } return empty($info) ? false : $info; }
function add(){ $goods = D("Goods"); if(!empty($_POST)){ //判断附件是否有上传 //如果有则实例化Upload,把附件上传到服务器指定位置 //然后把附件的路径名获得到,存入$_POST if(!empty($_FILES)){ $config = array( 'rootPath' => './public/', //根目录 'savePath' => 'upload/', //保存路径 ); //附件被上传到路径:根目录/保存目录路径/创建日期目录 $upload = new \Think\Upload($config); //uploadOne会返回已经上传的附件信息 $z = $upload -> uploadOne($_FILES['goods_img']); if(!$z){ show_bug($upload->getError()); //获得上传附件产生的错误信息 }else { //拼装图片的路径名 $bigimg = $z['savepath'].$z['savename']; $_POST['goods_big_img'] = $bigimg; //把已经上传好的图片制作缩略图Image.class.php $image = new \Think\Image(); //open();打开图像资源,通过路径名找到图像 $srcimg = $upload->rootPath.$bigimg; $image -> open($srcimg); $image -> thumb(150,150); //按照比例缩小 $smallimg = $z['savepath']."small_".$z['savename']; $image -> save($upload->rootPath.$smallimg); $_POST['goods_small_img'] = $smallimg; } } $goods -> create(); //收集post表单数据 $z = $goods -> add(); if($z){ //$this ->success('添加商品成功', U('Goods/showlist')); echo "success"; } else { //$this ->error('添加商品失败', U('Goods/showlist')); echo "error"; } }else { } $this -> display(); }
public function update($goods_id) { $goods = new \Model\GoodsModel(); if (!empty($_POST)) { if ($_FILES['goods_img']['error'] === 0) { $cfg = array('rootPath' => './Admin/Public/goods/'); $up = new \Think\Upload($cfg); $z = $up->uploadOne($_FILES['goods_img']); $_POST['goods_big_img'] = $up->rootPath . $z['savepath'] . $z['savename']; $bigimg = $_POST['goods_big_img']; //对上传图片进行缩略图处理 $im = new \Think\Image(); //实例化对象 $im->open($bigimg); //相对服务器路径打开图片 $im->thumb(165, 165, 3); //自适应大小制作缩略图 $smallimg = $up->rootPath . $z['savepath'] . "small_" . $z['savename']; $im->save($smallimg); //保存 $_POST['goods_small_img'] = $smallimg; //var_dump($_POST['goods_big_img']);die; } $arr = $goods->create(); $result = $goods->save($arr); if ($result) { $this->redirect('showlist', array(), 2, '修改商品成功'); } else { var_dump($goods->getError()); } } $category = new \Model\CategoryModel(); $cats = $category->select(); $this->assign('cats', $cats); $info = $goods->find($goods_id); $this->assign('info', $info); $this->display(); }