推荐一个使用成熟的解决方案:https://github.com/think2011/...html
文章已同步个人github笔记 https://github.com/ymblog/blog,欢迎你们加star~~,加star后人生更加美好……
移动端图片上传,经过FileReader生成base64图片资源进行预览,经过canvas进行图片的压缩,将图片url转换成Blob对象上传。ios
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图片上传</title> </head> <style> img{ display:block; width:200px; } </style> <body> <input type="file" accept="image/*" multiple id="upload" onchange="previewImg(this,'preview-img')"> <img src="" alt="" id="preview-img"> </body> </html>
<input type="file" id="upload" class="upload" accept="image/*" multiple />
/** * [previewImg 预览图片] * @param {[type]} input [文件上传input] * @param {[type]} obj [description] * @return {[type]} [description] */ function previewImg(input,imgObj) { var maxsize = 300 * 1024;//超过300k进行压缩 //是否支持 if (typeof FileReader === 'undefined') { alert("抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操做!"); input.setAttribute('disabled','disabled'); } if(input.files && input.files[0]){ var file = input.files[0], reader = new FileReader(); if(!/image\/\w+/.test(file.type)) { alert('不是有效的图片文件!'); return; } reader.onload = function(e){ var result = this.result;//获取到base64的图片 //大于300k图片进行压缩 if(result.length >= maxsize){ var img = new Image(); img.src = result; img.onload = function(){ compressSrc = compress(img,0.8,100); $(imgObj).setAttribute('src',compressSrc); //ajax dataURLtoBlob(compressSrc); } }else{ $(imgObj).setAttribute('src',result); //ajax dataURLtoBlob(result); } } } }
/** * [compress 压缩图片] * @param {[dom]} sourceImg [图片dom] * @param {[int]0-1} scale [缩小的尺寸比例] * @param {[int]0-100} quality [清晰质量] * @return {[object]} [图片源] */ function compress(sourceImg,scale,quality){ var area = sourceImg.width * sourceImg.height,//源图片的总大小 height = sourceImg.height * scale, width = sourceImg.width * scale, compressCvs = document.createElement('canvas');//压缩的图片画布 //压缩的图片配置宽高 compressCvs.width = width; compressCvs.height = height; var compressCtx = compressCvs.getContext("2d"); //解决ios 图片大于2000000像素没法用drawImage的bug if(area > 2000000 && navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)){ //瓦片绘制 var smallCvs = document.createElement("canvas"), smallCtx = smallCvs.getContext("2d"), count = Math.ceil(area / 1000000),//分割的数量 cvsWidth = width / count,//每一个小canvas的宽度 picWidth = sourceImg.width / count;//分割成小图片的宽度 smallCvs.height = compressCvs.height; smallCvs.width = width / count; //拼凑成大的canvas for(var i = 0;i < count;i ++){ smallCtx.drawImage(sourceImg,i*picWidth,0,picWidth,sourceImg.height,0,0,cvsWidth,height); compressCtx.drawImage(smallCvs,i*cvsWidth,0,cvsWidth,height); } }else{ compressCtx.drawImage(sourceImg,0,0,sourceImg.width,sourceImg.height,0,0,width,height); } //将canvas转换成base64 return compressCvs.toDataURL('image/jpeg',quality/100); } function $(id){ return document.getElementById(id); }
/** * [dataURLtoBlob 将base64转换为blob对象] * @param {[type]} dataurl [图片源base64] * @return {[object]} [图片源blob对象] */ function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type:mime}); }
这个只是一个移动端预览压缩上传图片实现的demo,会有一些兼容性和bug问题,你们能够本身修改和扩展git
结束,撒花~~~github
文章已同步个人github笔记 https://github.com/ymblog/blog,欢迎你们加star~~,加star后人生更加美好……