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);
// 請求接口上傳圖片到服務器
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可選
需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的左上角 X 軸坐標。
sy可選
需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的左上角 Y 軸坐標。
sWidth可選
需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的寬度。如果不說明,整個矩形(裁剪)從坐標的sx和sy開始,到image的右下角結(jié)束。
sHeight可選
需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的高度。
dx
image的左上角在目標canvas上X 軸坐標。
dy
image的左上角在目標canvas上Y 軸坐標。
dWidth可選
image在目標canvas上繪制的寬度。 允許對繪制的image進行縮放。 如果不說明, 在繪制時image寬度不會縮放。
dHeight可選
image在目標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://www.dhdzp.com/article/248830.htm
到此這篇關(guān)于vue js實現(xiàn)圖片壓縮封裝方法的文章就介紹到這了,更多相關(guān)vuejs圖片壓縮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 使用beforeEach實現(xiàn)登錄狀態(tài)檢查功能
今天小編就為大家分享一篇Vue 使用beforeEach實現(xiàn)登錄狀態(tài)檢查功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
vue的Virtual Dom實現(xiàn)snabbdom解密
這篇文章主要介紹了vue的Virtual Dom實現(xiàn)- snabbdom解密,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
vue-router中hash模式與history模式的區(qū)別
為了構(gòu)建 SPA(單頁面應用),需要引入前端路由系統(tǒng),這就是 Vue-Router 存在的意義,而這篇文章主要給大家介紹了關(guān)于vue-router中兩種模式區(qū)別的相關(guān)資料,分別是hash模式、history模式,需要的朋友可以參考下2021-06-06

