vue.js實現(xiàn)圖片壓縮封裝方法
全局main.js引入:
// 引入imgUpload方法 import * as imgUpload from "./utils/imgUpload" //外部使用 Vue.prototype.$imgUpload = imgUpload
新建imgUpload.js:
const dataURLtoFile = (dataurl, filename) => { // 將base64轉(zhuǎn)換為file文件 let arr = dataurl.split(',') let mime = arr[0].match(/:(.*?);/)[1] let bstr = atob(arr[1]) let n = bstr.length let u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new File([u8arr], filename, { type: mime }) }; const imgZip = (file) => { var _this = this; let imgZipStatus = new Promise((resolve, reject) => { let canvas = document.createElement("canvas"); // 創(chuàng)建Canvas對象(畫布) let context = canvas.getContext("2d"); let img = new Image(); img.src = file.content; // 指定圖片的DataURL(圖片的base64編碼數(shù)據(jù)) var Orientation = ''; img.onload = () => { //根據(jù)情況定義 // canvas.width = 400; // canvas.height = 300; canvas.width = img.width; canvas.height = img.height; context.drawImage(img, 0, 0, canvas.width, canvas.height); file.content = canvas.toDataURL(file.file.type, 0.5); // 0.92為默認壓縮質(zhì)量 let files = dataURLtoFile(file.content, file.file.name); resolve(files) } }) return imgZipStatus; }; export { imgZip, }
頁面中使用:
//上傳方法 afterCard(file) { this.$imgUpload.imgZip(file).then(resData => { const formData = new FormData(); formData.append("file", resData); // 請求接口上傳圖片到服務(wù)器 uploadImg(formData).then(res => { }) }) }
備注:
HTMLCanvasElement.getContext()
方法返回canvas的上下文,如果上下文沒有定義則返回null.
在同一個canvas上以相同的contextType
多次調(diào)用此方法只會返回同一個上下文。
var ctx = canvas.getContext(contextType);var ctx = canvas.getContext(contextType, contextAttributes);
上下文類型(contextType)
是一個指示使用何種上下文,可能的值是:
"2d"
"webgl"
"webgl2"
"bitmaprenderer"
上下文屬性(contextAttributes)
你可以在創(chuàng)建渲染上下文的時候設(shè)置多個屬性,例如:
canvas.getContext("webgl", { antialias: false, depth: false });
Canvas 2D API 中的CanvasRenderingContext2D.drawImage()
方法提供了多種方式在Canvas上繪制圖像。
ctx.drawImage(image, dx, dy); ctx.drawImage(image, dx, dy, dWidth, dHeight); ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
參數(shù):
image
繪制到上下文的元素。允許任何的 canvas 圖像源
sx
可選
需要繪制到目標(biāo)上下文中的,image
的矩形(裁剪)選擇框的左上角 X 軸坐標(biāo)。
sy
可選
需要繪制到目標(biāo)上下文中的,image
的矩形(裁剪)選擇框的左上角 Y 軸坐標(biāo)。
sWidth
可選
需要繪制到目標(biāo)上下文中的,image
的矩形(裁剪)選擇框的寬度。如果不說明,整個矩形(裁剪)從坐標(biāo)的sx
和sy
開始,到image
的右下角結(jié)束。
sHeight
可選
需要繪制到目標(biāo)上下文中的,image
的矩形(裁剪)選擇框的高度。
dx
image
的左上角在目標(biāo)canvas上X 軸坐標(biāo)。
dy
image
的左上角在目標(biāo)canvas上Y 軸坐標(biāo)。
dWidth
可選
image
在目標(biāo)canvas上繪制的寬度。 允許對繪制的image
進行縮放。 如果不說明, 在繪制時image
寬度不會縮放。
dHeight
可選
image
在目標(biāo)canvas上繪制的高度。允許對繪制的image
進行縮放。 如果不說明, 在繪制時image
高度不會縮放。
示例:
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var image = document.getElementById('source'); ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
Vue vant-ui使用van-uploader實現(xiàn)頭像圖片上傳
http://chabaoo.cn/article/248830.htm
到此這篇關(guān)于vue js實現(xiàn)圖片壓縮封裝方法的文章就介紹到這了,更多相關(guān)vuejs圖片壓縮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項目中如何實現(xiàn)element-ui組件按需引入
這篇文章主要介紹了vue項目中如何實現(xiàn)element-ui組件按需引入,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05vue中created、watch和computed的執(zhí)行順序詳解
由于vue的雙向數(shù)據(jù)綁定,自動更新數(shù)據(jù)的機制,在數(shù)據(jù)變化后,對此數(shù)據(jù)依賴?的所有數(shù)據(jù),watch事件都會被更新、觸發(fā),下面這篇文章主要給大家介紹了關(guān)于vue中created、watch和computed的執(zhí)行順序,需要的朋友可以參考下2022-11-11詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
本篇文章主要介紹了詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03vue-cli腳手架build目錄下utils.js工具配置文件詳解
這篇文章主要介紹了vue-cli腳手架build目錄下utils.js工具配置文件詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09解決vue做詳情頁跳轉(zhuǎn)的時候使用created方法 數(shù)據(jù)不會更新問題
這篇文章主要介紹了解決vue做詳情頁跳轉(zhuǎn)的時候使用created方法 數(shù)據(jù)不會更新問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07