Skip to content

Commit

Permalink
图片上传处理,包括缩略图的生成
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Aug 15, 2016
1 parent 5e9a78b commit ff93f7a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 55 deletions.
94 changes: 41 additions & 53 deletions Backend/Home/Controller/FileController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function uploadFile($thumbWidth = 100, $thumbHeight = 100)
//unlink($thumb_file); //上传生成缩略图以后删除源文件
}
//url拼接
$version_url = __ROOT__ . '/Uploads/' . iconv('UTF-8','GBK',$file_arr['savepath']) . iconv('UTF-8','GBK',$file_arr['savename']);
$version_url = __ROOT__ . '/Uploads/' . iconv('UTF-8', 'GBK', $file_arr['savepath']) . iconv('UTF-8', 'GBK', $file_arr['savename']);

$db = M('PublishTable');
$db->software_name = $software_name;
Expand Down Expand Up @@ -95,28 +95,28 @@ public function image()
* @param [String] $thumbHeight [缩略图高度]
* @return [Array] [图片上传信息]
*/
public function imageUpload($path = 'Images',$thumb = FALSE ,$thumbWidth = '' , $thumbHeight = '')
public function imageUpload($path = 'Images', $thumb = FALSE, $thumbWidth = 100, $thumbHeight = 100)
{
// 检查配置目录是否存在,如果不存在,则创建一个
if (!is_dir(C('UPLOAD_PATH'))){
if (!is_dir(C('UPLOAD_PATH'))) {
mkdir(iconv('UTF-8', 'GBK', C('UPLOAD_PATH')), 0777, true);
}
//============支持在实例化后动态赋值上传参数===================
$obj = new \Think\Upload();// 实例化上传类
$obj->maxSize = C('UPLOAD_MAX_SIZE') ;// 设置附件上传大小
$obj->rootPath = C('UPLOAD_PATH') ;// 保存根路径
$obj->savePath = $path.'/'; // 设置附件上传目录
$obj->exts = C('UPLOAD_EXTS');// 设置附件上传类型
$obj->saveName = array('uniqid','');//文件名规则
$obj->maxSize = C('UPLOAD_MAX_SIZE');// 设置附件上传大小
$obj->rootPath = C('UPLOAD_PATH');// 保存根路径
$obj->savePath = $path . '/'; // 设置附件上传目录
$obj->exts = C('UPLOAD_EXTS');// 设置附件上传类型
$obj->saveName = array('uniqid', '');//文件名规则
$obj->replace = true;//存在同名文件覆盖
$obj->autoSub = true;//使用子目录保存
$obj->subName = array('date','Ym');//子目录创建规则,
$obj->subName = array('date', 'Y-m-d');//子目录创建规则,
$info = $obj->upload();
// 没有上传成功,则直接返回错误信息
if(!$info) return array('status' =>0, 'msg'=> $obj->getError());
if (!$info) return array('status' => 0, 'msg' => $obj->getError());

//=============是否生成缩略图==================================
if($thumb) {
if (TRUE == $thumb) {
$image = new \Think\Image();
// 循环获取上传文件信息
foreach ($info as $file) {
Expand All @@ -132,76 +132,64 @@ public function imageUpload($path = 'Images',$thumb = FALSE ,$thumbWidth = '' ,
$mime = $image->mime(); // 返回图片的mime类型
$size = $image->size(); // 返回图片的尺寸数组 0 图片宽度 1 图片高度
//使用thumb方法生成缩略图,IMAGE_THUMB_FILLED = 2 ; 缩放后填充类型
$image->thumb(150, 150, \Think\Image::IMAGE_THUMB_FILLED);
$image->thumb($thumbWidth, $thumbHeight, \Think\Image::IMAGE_THUMB_SCALE);
$image->save($savePath);
// 返回文件信息
return array(
'status' => 1,
'savepath' => $file['savepath'],
'savename' => $file['savename'],
'width' => $width,
'height' => $height,
'type' => $type,
'sha1' => $file['sha1'],
'md5' => $file['md5'],
'mime' => $mime,
'pic_path' => $file['savepath'] . $file['savename'],
'mini_pic' => $file['savepath'] . 'mini_' . $file['savename']
);
//@unlink($thumb_file); //上传生成缩略图以后删除源文件
}
}else{
foreach($info as $file)
{
//=================================是否生成缩略图==================================
} else {
foreach ($info as $file) {
// 返回文件信息
return array(
'status' => 1,
'savepath' => $file['savepath'],
'savename' => $file['savename'],
'pic_path' => $file['savepath'] . $file['savename'],
'mini_pic' => $file['savepath'] . 'mini_' .$file['savename']
'ext' => $file['ext'],
'size' => $file['size'],
'sha1' => $file['sha1'],
'md5' => $file['md5'],
'type' => $file['type'],
'pic_path' => $file['savepath'] . $file['savename']
);
}
}
}

/**
* uploadify 文件上传
* uploadify 文件上传,通过调用imageUpload()方法
*/
public function uploadImage()
{
$result = $this->imageUpload('Img',TRUE,150,150);
var_dump($result);
}

if (!empty($_FILES))
{
//图片上传设置
$config = array(
'maxSize' => 3145728,
'savePath' => '',
'saveName' => array('uniqid', ''),
'exts' => array('jpg', 'png', 'jpeg'),
'autoSub' => true,
'subName' => array('date', 'Y-m-d'),
);
//得到上传的临时文件流
$tempFile = $_FILES['Filedata']['tmp_name'];

//允许的文件后缀
$fileTypes = array('jpg', 'jpeg', 'gif', 'png');

//得到文件原名
$fileName = iconv("UTF-8", "GB2312", $_FILES["Filedata"]["name"]);
$fileParts = pathinfo($_FILES['Filedata']['name']);

//接受动态传值
$files = $_POST['typeCode'];

$upload = new \Think\Upload($config);// 实例化上传类
$images = $upload->upload();
//判断是否有图
if ($images == false) {
echo $fileName . "上传失败!";
} else {
echo $fileName . "上传成功!";
}
}
/**
* uploadify 文件上传,通过调用imageUpload()方法
*/
public function uploadifyUploadImage()
{
$result = $this->imageUpload('Face',TRUE,150,150);
$this->ajaxReturn(json_encode($result),'JSON');
}

public function test(){
if (!is_dir(C('UPLOAD_PATH'))){
public function test()
{
if (!is_dir(C('UPLOAD_PATH'))) {
var_dump(mkdir(iconv('UTF-8', 'GBK', C('UPLOAD_PATH')), 0777, true));
}
echo 1111111;
Expand Down
16 changes: 15 additions & 1 deletion Backend/Home/View/File/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ <h4>文件上传管理</h4>
</div>


<div class="space-4"></div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"> 选择上传图片 </label>

<div class="col-sm-9">
<img src="
<if condition='$user["face180"]'>
__ROOT__/Uploads/{$user.face180}
<else/>
__PUBLIC__/Images/noface.gif
</if>" width='180' height='180' id='face-img'/>
</div>
</div>
<div class="space-4"></div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"> 选择上传图片 </label>
Expand Down Expand Up @@ -88,8 +101,9 @@ <h4>文件上传管理</h4>
var i = 0;//初始化数组下标
jQuery(function ($) {
var sessionId = "{:session_id()}";
var uploaderUrl = "{:U('Home/File/imageUpload')}";
var uploaderUrl = "{:U('Home/File/uploadifyUploadImage')}";
var swfUrl = "__PUBLIC__/Backend/js/uploadify/uploadify.swf";
var ROOT = '__ROOT__';
$('#photo_upload').uploadify({
'auto': true,//关闭自动上传
'removeTimeout': 1,//文件队列上传完成1秒后删除
Expand Down
2 changes: 1 addition & 1 deletion Backend/Home/View/File/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h4>文件上传管理</h4>
<div class="widget-body">
<div class="widget-main no-padding">
<p></p>
<form class="form-horizontal" role="form" action="{:U('File/uploadFile')}" enctype="multipart/form-data" method="post">
<form class="form-horizontal" role="form" action="{:U('File/uploadImage')}" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 文件中文名称 </label>

Expand Down

0 comments on commit ff93f7a

Please sign in to comment.