前端部分
<form target="avatar-frame" method="post" action="{:U('Member/avatar')}" enctype="multipart/form-data" id="avatar-form"> <div class="user-photo" id="preview"><empty name="user['avatar']"><i class="fa fa-user-circle"></i><else/><img src="__ROOT__{$user.avatar}" alt=""></empty></div> <input type="file" name="avatar" class="avatar-input" id="changeAvatar" onchange="previewImage(this)" > </form> <iframe name="avatar-frame" style="display: none;"></iframe>
js部分
<script src="__JS__/jquery.form.js"></script> <script> //图片上传预览 IE是用了滤镜。 function previewImage(file) { var MAXWIDTH = 260; var MAXHEIGHT = 180; var div = document.getElementById('preview'); if (file.files && file.files[0]) { div.innerHTML ='<img id=imghead>'; var img = document.getElementById('imghead'); // img.onload = function(){ // var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); // img.width = rect.width; // img.height = rect.height; // img.style.marginLeft = rect.left+'px'; // img.style.marginTop = rect.top+'px'; // } var reader = new FileReader(); reader.onload = function(evt){img.src = evt.target.result;} reader.readAsDataURL(file.files[0]); // console.log(document.getElementById('avatar-form'),$('#avatar-form')) document.getElementById('avatar-form').submit(); // document.getElementById('avatar-form').ajaxSubmit({ type:'post', url:"{:U('Upload/avatarUpload')}", success:function(data){ alert(123); alert("uuuuuuuu");}); } else //兼容IE { var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="'; file.select(); var src = document.selection.createRange().text; div.innerHTML = '<img id=imghead>'; var img = document.getElementById('imghead'); img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src; var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height); div.innerHTML = "<div id=divhead style='"+rect.width+"px;height:"+rect.height+"px;"+sFilter+src+""'></div>"; // div.innerHTML = "<div id=divhead style='"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+""'></div>"; } } </script>
php部分
/** * 修改上传头像 */ public function avatar(){ // var_dump($_FILES,$_POST);exit; if($_FILES){ $imgConfig = array( 'maxSize' => 0, //上传的文件大小限制 (0-不做限制) 'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀 'autoSub' => true, //自动子目录保存文件 'subName' => array('date', 'Ymd'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组 'rootPath' => './Uploads/avatar/', //保存根路径 'savePath' => '', //保存路径 'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组 ); // var_dump($_FILES['avatar']['size']);exit; $upload = new ThinkUpload($imgConfig); $result = $upload->uploadOne($_FILES['avatar']); if ($result){ $rootpath = trim($imgConfig['rootPath'],"."); $avatar['avatar'] = '/Uploads/Avatar/'.$result['savepath'].$result['savename']; $userId = UID; $where['uid'] = $userId; if(M('member')->where($where)->save($avatar)){ $_SESSION['user']['avatar'] = $avatar['avatar']; } // if($_POST['oldAvatar']){ // $_POST['oldAvatar'] = '.'.$_POST['oldAvatar']; // $bool = delDir($_POST['oldAvatar'],true); // } echo '修改成功'; }else { echo '修改失败'; } } }