根据网上 各位大神的资料整理,自己已经实现的用法。。
但是稍加修改,还是可以用的
1.贴出图片上传处理类
/**
* 图片上传处理
* @param [String] $path [保存文件夹名称]
* @param [String] $thumbWidth [缩略图宽度]
* @param [String] $thumbHeight [缩略图高度]
* @return [Array] [图片上传信息]
*/
Private function _upload($path,$thumbWidth = '' , $thumbHeight = '') {
$obj = new \Think\Upload();// 实例化上传类
$obj->maxSize = C('UPLOAD_MAX_SIZE') ;// 设置附件上传大小
$obj->savePath =C('UPLOAD_PATH').$path.'/'; // 设置附件上传目录
$obj->exts = C('UPLOAD_EXTS');// 设置附件上传类型
$obj->saveName = array('uniqid','');//文件名规则
$obj->replace = true;//存在同名文件覆盖
$obj->autoSub = true;//使用子目录保存
$obj->subName = array('date','Ym');//子目录创建规则,
$info = $obj->upload();
if(!$info) {
return array('status' =>0, 'msg'=> $obj->getError() );
}else{
if($info){ //生成缩略图
$image = new \Think\Image();
foreach($info as $file) {
$thumb_file = C('UPLOAD_PATH') . $file['savepath'] . $file['savename'];
$save_path = C('UPLOAD_PATH') .$file['savepath'] . 'mini_' . $file['savename'];
$image->open( $thumb_file )->thumb( $thumbWidth, $thumbHeight,\Think\Image::IMAGE_THUMB_FILLED )->save( $save_path );
return array(
'status' => 1,
'savepath' => $file['savepath'],
'savename' => $file['savename'],
'pic_path' => $file['savepath'] . $file['savename'],
'mini_pic' => $file['savepath'] . 'mini_' .$file['savename']
);
//@unlink($thumb_file); //上传生成缩略图以后删除源文件
}
}else{
foreach($info as $file) {
return array(
'status' => 1,
'savepath' => $file['savepath'],
'savename' => $file['savename'],
'pic_path' => $file['savepath'].$file['savename']
);
}
}
}
}2、 将图像处理单独封装,以便于多出调用,其中头像调用:
/*
* 头像上传
*/
Public function uploadFace (){
if (!IS_POST) {
E('页面不存在');
}
$upload = $this->_upload('Face','180','180');
$data = json_encode($upload);
//$data = json_decode($obj);
//$this->ajaxReturn(json_encode($upload),'JSON');
$this->ajaxReturn($data,'JSON');
}3、修改用户头像 /*
* 修改用户头像
*/
Public function editFace(){
if(!IS_POST){
E('页面不存在');
}
$db = M('userinfo');
$where = array('uid' => session('uid'));
$field = array('face180');
$old = $db->where($where)->field($field)->find();
//p($_POST);
//die;
if ($db->where($where)->save($_POST)) {
if (!empty($old['face180'])) {
@unlink('./Uploads/'.$old['face180']);//删除之前的缩略图
}
$this->success('修改成功',U('index'));
}else{
$this->error('修改失败,请重试。。。');
}
}4、uploadify 回调函数://头像上传Uploadify插件
$('#face').uploadify({
swf : PUBLIC + '/Uploadify/uploadify.swf',//引入Uploadify核心flash文件
uploader : uploadUrl,//PHP处理脚本地址
width : 120,//上传按钮样式
height : 30,
buttonImage : PUBLIC + '/Uploadify/browse-btn.png',//上传按钮背景图地址
fileTypeDesc : 'Image File', //选择文件提示文字
fileTypeExts : '*.jpeg; *.jpg; *.png; *.gif', //允许选择的文件类型
formData : {'session_id' : sid},
//上传成功后的回调函数
onUploadSuccess : function (file, data, response){
eval('var data=' + data);
var data = JSON.parse(data); //由JSON字符串转换为JSON对象,json转成object
//alert(data.pic_path);
//alert(data.mini_pic);
//alert(data.status);
if(data.status){
$('#face-img').attr('src',ROOT + '/Uploads/' + data.mini_pic);
$('input[name=face180]').val(data.mini_pic);
}else {
alert(msg.info);
return false;
}
}
});5、前端页面 js 解析php 规则: <script type='text/javascript'>
var PUBLIC = '__PUBLIC__';
var uploadUrl = '{:U("Common/uploadFace")}';
var sid = '{:session_id()}';
var ROOT = '__ROOT__';
</script>前端实现代码:<div class='form hidden'>
<form action="{:U('editFace')}" method='post'>
<div class='edit-face'>
<img src="
<if condition='$user["face180"]'>
__ROOT__/Uploads/{$user.face180}
<else/>
__PUBLIC__/Images/noface.gif
</if>" width='180' height='180' id='face-img'/>
<p>
<input type="file" name='face' id='face'/>
</p>
<p>
<input type="hidden" name='face180' value=''/>
<input type="submit" value='保存修改' class='edit-sub'/>
</p>
</div>
</form>
</div>6、 config.php 部分变量: //图片上传
'UPLOAD_MAX_SIZE' => 2000000, //最大上传大小
'UPLOAD_PATH' => './Uploads/', //文件上传保存路径
//'UPLOAD_PATH' => './', //文件上传保存路径
'UPLOAD_EXTS' => array('jpg','jpeg','gif','png'), 最佳答案