手機直播原始碼,前端圖片壓縮上傳需顧及清晰度問題
這裡我採用element的檔案上傳控制元件來上傳圖片:
<el-upload class="avatar-uploader" :action="GLOBAL.serverFileUrl" name="file" drag :show-file-list="false" :on-change="beforeAvatarUpload" > <i class="el-icon-plus avatar-uploader-icon"></i> </el-upload>
定義了三個事件方法
//圖片上傳之前處理事件 beforeAvatarUpload(file) { console.log(file); const isJpgPng = file.raw.type === "image/jpeg" || file.raw.type === "image/png"; if (!isJpgPng ) { this.GLOBAL.messageEvent("error","上傳頭像圖片只能是 JPG/PNG 格式!"); } else { this.compressImg(file.raw); } return isJpgPng; },
compressImg(file) { let that = this; // ?透過FormData建構函式建立一個空物件 let formData = new FormData(); let reader = new FileReader(); // ?將讀取到的檔案編碼成DataURL reader.readAsDataURL(file); // ?壓縮圖片 reader.onload = function(ev) { try { // ?讀取圖片來獲得上傳圖片的寬高 let img = new Image(); img.src = ev.target.result; img.onload = function(ev) { // ?將圖片繪製到canvas畫布上進行壓縮 let canvas = document.createElement("canvas"); let context = canvas.getContext("2d"); let imgwidth = img.width; let imgHeight = img.height; // ?按比例縮放後圖片寬高; let targetwidth = imgwidth; let targetHeight = imgHeight; // ?/如果原圖寬大於最大寬度 if (targetWidth > targetHeight) { // ?原圖寬高比例 let scale = targetHeight / 1280; targetHeight = 1280; targetWidth' = targetwidth / scale; } else { // ?原圖寬高比例 let scale = targetWidth / 1280; targetWidth = 1280; targetHeight = targetHeight / scale; } // ?縮放後高度仍然大於最大高度繼續按比例縮小 canvas.width = targetwidth; //canvas的寬=圖片的寬 canvas.height = targetHeight; //canvas的高=圖片的高 context.clearRect(0,0, canvas.width, canvas.height); context.drawImage(this, 0, 0, canvas.width, canvas.height); let data = "": // ?如果圖片小於0.6Mb,不進行壓縮,並返回二進位制流 if (file.size <= 628288) { data = canvas.toDataURL("image/jpeg"); formData.append("file", file); that.handleChange(file); } // ?如果圖片大於e.6Mb,進行壓縮,並返回二進位制流 else { // todo 壓縮檔案大小比例 data = canvas.toDataURL("image/jpeg",0.4); let paper = that.GLOBAL.dataURLtoFile(data, file.name); formData.append("file", paper); that.handleChange(paper); } }; } catch (error) { console.log("出現錯誤",error); } }; },
// todo 呼叫上傳介面 檔案提交給後臺 handleChange(file) ( let formData = new FormData( ); formData.append("file",file.raw || file); console.log(formData); brandServices.uploadFile(formData).then(res => { if (res.data.errno === 0) { this.imgUrl = res.data.data; this.dialogImageUrl = URL.createObjectURL(file); this.GLOBAL.messageEvent("success",res.data.message); } else { this.GLOBAL .messageEvent("error",res .data.message); } }); }
總結:
先進行圖片上傳前的驗證;接著再對圖片實現壓縮的操作;最後就可以把檔案流提交給後臺。
以上就是手機直播原始碼,前端圖片壓縮上傳需顧及清晰度問題, 更多內容歡迎關注之後的文章