上传图片裁剪
- 格式:docx
- 大小:30.58 KB
- 文档页数:9
上传图片裁剪编辑实现方式1.单独创建一个jsp例如:img.jsp1.1内容如下:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""/TR/ xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="/1999/xh tml">1.2导入样式:<link href="/res/js/imgjquery/css/border-anim-h.gif" rel="stylesheet"type="text/css" /><link href="/res/js/imgjquery/css/border-anim-v.gif" rel="stylesheet"type="text/css" /><link href="/res/js/imgjquery/css/border-h.gif" rel="stylesheet" type="text/css"/><link href="/res/js/imgjquery/css/border-v.gif" rel="stylesheet" type="text/css"/><link href="/res/js/imgjquery/css/imgareaselect-deprecated.css" rel="stylesheet"type="text/css" /><link href="/res/js/imgjquery/css/imgareaselect-default.css" rel="stylesheet"type="text/css" /><link href="/res/js/imgjquery/css/imgareaselect-animated.css" rel="stylesheet"type="text/css" />1.3导入js:<script src="/res/js/imgjquery/scripts/jquery.imgareaselect.js"type="text/javascript"></script><script src="/res/js/imgjquery/scripts/jquery.imgareaselect.min.js"type="text/javascript"></script><script src="/res/js/imgjquery/scripts/jquery.imgareaselect.pack.js"type="text/javascript"></script>1.4Jsp中div 准备<div class="main_right_mr"><h2>头像设置</h2><div class="headimg_form"><form><input type ="file" id="Member_headimg" /></form></div><div id="img"><div class="hull"><div id="localImag"><img id="preview" width=-1 height=-1 style="diplay:none" /></div><div id="show_img"><p>大头像</p><div style="height:100px;width:100px;border:1pxsolid#ccc;overflow:hidden;"><div id="big_img"style="height:100px;width:100px;border:1pxsolid#ccc;overflow:hidden;"> <img id ="big_photo"width=-1height=-1style= "diplay:none"/></div></div><p>小头像</p><div style ="height:75px;width:75px;border:1pxsolid#ccc;overflow:hidden;"><div id="small_img"style="height:75px;width:75px;border:1px solid#ccc;overflow:hidden;"><img id ="small_photo"width=-1height=-1 style="diplay:none" /> </div></div><p><input type ="button" value="确认修改" /></p></div></div></div></body></div>1.5jsp中的js加载:<script type="text/javaScript">/**setImagePreview()*@paramfileObjStrimg表示上传域的ID*@parampreviewObjString表示显示图片的ID*@paramlocalImgString表示显示图片外层的div的ID*@paramwidth表示显示图片的宽*@paramheight表示显示图片的高*/functionsetImagePreview(fileObj,previewObj,localImg,width,height){ vardocObj=document.getElementById(fileObj);varimgObjPreview=document.getElementById(previewObj);if(docObj.files&&docObj.files[0]){ //火狐下,直接设img属性imgObjPreview.style.display='block';imgObjPreview.style.width=width+'px';imgObjPreview.style.height=height+'px';//imgObjPreview.src=docObj.files[0].getAsDataURL();//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);}else{//IE下,使用滤镜docObj.select();varimgSrc=document.selection.createRange().text;varlocalImagId=document.getElementById(localImg);//必须设置初始大小localImagId.style.width=width+'px';localImagId.style.height=height+'px';/ /图片异常的捕捉,防止用户修改后缀来伪造图片try{localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImage Loader(sizingMethod=scale)";localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader ").src=imgSrc;}catch(e){alert("您上传的图片格式不正确,请重新选择!");return false;}imgObjPreview.style.display='none';document.selection.empty();}return true;}$(function(){ //给上传区添加onchang事件,掉用函数setImagePreview() $("#Member_headimg").change(function(){ setImagePreview('Member_he adimg','preview','localImag',400,300);setImagePreview('Member_headimg','small_photo','small_img',75,75);setImagePreview('Member_headimg','big_photo','big_img',100,100); }) //调用截图插件需用的$("#localImag").imgAreaSelect({/* x1: 120, y1: 90, x2: 280, y2: 210 , */aspectRatio:"4:1",//选中区域是正方形handles:true,onSelectChange:set,//执行选择后执行的函数fadeSpeed:400//出来效果});function set(img,selection){if(!selection.width||!selection.height)return;varscaleX=100/selection.width;varscaleY=100/selection.height;if(erAgent.indexOf("MSIE")>=1){$('#big_img').css({ width:Math.round(scaleX*400),height:Math.round(scal eY*300),marginLeft:-Math.round(scaleX*selection.x1),marginTop:-Math.round(scaleY *selection.y1)});$('#small_img').css({ width:Math.round(scaleX*400*3/4),height:Math.rou nd(scaleY*300*3/4), marginLeft:-Math.round(scaleX* selection.x1*3/4),marginTop:-Math.round(scaleY*selection.y1*3/4)});}else{$('#big_photo').css({ width:Math.round(scaleX*400),height:Math.round(sc aleY*300), marginLeft:-Math.round(scaleX*selection.x1),marginTop:-Math.round(scaleY*selection.y1)});$('#small_photo').css({ width:Math.round(scaleX*400*3/4),height:Math.roun d(scaleY*300*3/4), marginLeft:-Math.round(scaleX* selection.x1*3/4),marginTop:-Math.round(scaleY*selection.y1*3/4)});}}})</script>1.6 <style type="text/css">#img {height: 300px;}#img.hull {width: 400px;}#localImag {height: 300px;width: 400px;border: 1pxsolid #ccc;float: left;}#show_img {float: right;}#old_imgimg {width: 400px;height: 300px;}</style>2需要编辑图片的引入img.jsp做相应处理处理方法如下:2.1 参数设置:OPTIONS参数2.2 方法。