1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| $('#file').on('change',function (e) { var t = $(this); var file = this.files[0]; if(file){ if(!/image\/\w+/.test(file.type)){ alert("请确保文件为图像类型"); return false; } }else{ return false; } var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function(){ var img = new Image(); img.src = reader.result; img.onload = function(){ if(img.naturalWidth > 800){ var w = 800*img.naturalHeight/img.naturalWidth; var cvs = document.createElement('canvas'); cvs.width=800; cvs.height=w; var ctx = cvs.getContext('2d'); ctx.drawImage(img,0,0,800,w); headImgData = cvs.toDataURL('image/png'); console.log(headImgData); } } } })
|