上传图片压缩后回显页面

mac2024-05-28  58

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>前端压缩上传图片</title> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> </head> <style> .outer{ width: 100%; height: 100%; } .update { position: absolute; /*border: 1px solid red;*/ top: 34%; left: 10%; width: 80%; height: 49%; } .update input { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; } .update img { width: 100%; height: 100%; } </style> <body> <div class="outer"> <div class="update"> <input type="file" id="picFile"  onchange="readFile(this)"/> <img id="img" src="https://sucai.suoluomei.cn/sucai_zs/images/20191028141957-tinjai.png" alt=""/> </div> </div> <script> function readFile(obj) { var file = obj.files[0]; //判断类型是不是图片 if (!/image\/\w+/.test(file.type)) { alert("请确保文件为图像类型"); return false; } var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { dealImage(this.result, {quality: 0.5}, function (base) { //调用 var blob = dataURLtoBlob(base); var newFile = new File([blob], file.name, {type: file.type}); console.log(newFile) let r = new FileReader(); //本地预览 r.onload = function () { // $('#img').attr("src", r.result); //本地返回的图片 ; } r.readAsDataURL(newFile); //Base64 upload(newFile); }); } } //将base64转换为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}); } function upload(file) { var that = this; // 创建form对象 let param = new FormData(); // 通过append向form对象添加数据 param.append('file', file); // 文件大小 // param.append('size', file.size); param.append("key", "Gn1xVdBiTClSSHKZifg0pTQSU5XGagWO"); for (var n in that.params) { param.append(n, that.params[n]); } // 创建ajax var xhr = new XMLHttpRequest(); xhr.onload = function (res) { console.log(xhr.responseText) let res1 = JSON.parse(res.currentTarget.response) console.log("我是谁,我在哪儿?",JSON.parse(res.currentTarget.response)) $('#img').attr('src', res1.info.url) //接口返回的图片 } xhr.open("POST", "http://api110.herbplantist.com/sucai/public/index.php/post/post/uploadFile", true); // 发送表单数据 xhr.send(param); } function dealImage(path, obj, callback) { var img = new Image(); img.src = path; img.onload = function () { var that = this; // 默认按比例压缩 var w = that.width, h = that.height, scale = w / h; w = obj.width || w; h = obj.height || (w / scale); var quality = obj.quality || 0.7; // 默认图片质量为0.7 //生成canvas var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); // 创建属性节点 var anw = document.createAttribute("width"); anw.nodeValue = w; var anh = document.createAttribute("height"); anh.nodeValue = h; canvas.setAttributeNode(anw); canvas.setAttributeNode(anh); ctx.drawImage(that, 0, 0, w, h); // 图像质量 if (obj.quality && obj.quality <= 1 && obj.quality > 0) { quality = obj.quality; } // quality值越小,所绘制出的图像越模糊 var base64 = canvas.toDataURL('image/jpeg', quality); // 回调函数返回base64的值 callback(base64); } } </script> </body> </html>
最新回复(0)