Vue使用canvas實(shí)現(xiàn)圖片壓縮上傳
本文實(shí)例為大家分享了Vue使用canvas實(shí)現(xiàn)圖片壓縮上傳的具體代碼,供大家參考,具體內(nèi)容如下
場(chǎng)景:如用戶頭像等
對(duì)于大尺寸圖片的上傳,在前端進(jìn)行壓縮除了省流量外,最大的意義是極大的提高了用戶體驗(yàn)。
兩方面:
1、由于上傳圖片尺寸比較小,因此上傳速度會(huì)比較快,交互會(huì)更加流暢,同時(shí)大大降低了網(wǎng)絡(luò)異常導(dǎo)致上傳失敗風(fēng)險(xiǎn)。
2、很多網(wǎng)站的圖片上傳功能都會(huì)對(duì)圖片的大小進(jìn)行限制,尤其是頭像上傳,限制5M或者2M以內(nèi)是非常常見(jiàn)的(但是我用單反拍了個(gè)頭像,照片超過(guò)2M很正常,要對(duì)圖片進(jìn)行處理才能上傳)。如果可以在前端進(jìn)行壓縮,則理論上對(duì)圖片尺寸的限制是沒(méi)有必要的。

示例:
主要技術(shù):使用canvas的drawImage()方法。(附:canvas.toDataURL()或者canvas.toBlob())
ctx.drawImage(image, dx, dy); ctx.drawImage(image, dx, dy, dWidth, dHeight); ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
示例:
// html
<input id="file" type="file">
// JS
var eleFile = document.querySelector('#file');
// 壓縮圖片需要的一些元素和對(duì)象
var reader = new FileReader(), img = new Image();
// 選擇的文件對(duì)象
var file = null;
// 縮放圖片需要的canvas
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
// base64地址圖片加載完畢后
img.onload = function () {
// 圖片原始尺寸
var originWidth = this.width;
var originHeight = this.height;
// 最大尺寸限制
var maxWidth = 400, maxHeight = 400;
// 目標(biāo)尺寸
var targetWidth = originWidth, targetHeight = originHeight;
// 圖片尺寸超過(guò)400x400的限制
if (originWidth > maxWidth || originHeight > maxHeight) {
if (originWidth / originHeight > maxWidth / maxHeight) {
// 更寬,按照寬度限定尺寸
targetWidth = maxWidth;
targetHeight = Math.round(maxWidth * (originHeight / originWidth));
} else {
targetHeight = maxHeight;
targetWidth = Math.round(maxHeight * (originWidth / originHeight));
}
}
// canvas對(duì)圖片進(jìn)行縮放
canvas.width = targetWidth;
canvas.height = targetHeight;
// 清除畫布
context.clearRect(0, 0, targetWidth, targetHeight);
// 圖片壓縮
context.drawImage(img, 0, 0, targetWidth, targetHeight);
// canvas轉(zhuǎn)為blob并上傳
canvas.toBlob(function (blob) {
// 圖片ajax上傳
var xhr = new XMLHttpRequest();
// 文件上傳成功
xhr.onreadystatechange = function() {
if (xhr.status == 200) {
// xhr.responseText就是返回的數(shù)據(jù)
}
};
// 開(kāi)始上傳
xhr.open("POST", 'upload.php', true);
xhr.send(blob);
}, file.type || 'image/png');
};
// 文件base64化,以便獲知圖片原始尺寸
reader.onload = function(e) {
img.src = e.target.result;
};
eleFile.addEventListener('change', function (event) {
file = event.target.files[0];
// 選擇的文件是圖片
if (file.type.indexOf("image") == 0) {
reader.readAsDataURL(file);
}
});
注意:
移動(dòng)端會(huì)出現(xiàn)圖片變形,需要根據(jù)設(shè)備的dpr對(duì)canvas進(jìn)行放大,再用css進(jìn)行強(qiáng)制恢復(fù)
// 獲取設(shè)備dpr
getPixelRatio: function(context) {
let backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
return (window.devicePixelRatio || 1) / backingStore;
}
// 大概這樣
const ctx = this.canvas.getContext("2d");
const dpr = this.getPixelRatio(ctx);
this.$refs.postImg.crossOrigin = "Anonymous";
var oldWidth = this.canvas.width;
var oldHeight = this.canvas.height;
this.canvas.style.width = oldWidth + 'px';
this.canvas.style.height = oldHeight + 'px';
this.canvas.width = oldWidth * dpr;
this.canvas.height = oldHeight * dpr;
ctx.scale(dpr, dpr);
//進(jìn)行正常的操作
ctx.drawImage(this.$refs.cropImg, 0, 0, 250, 400);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue入門之?dāng)?shù)據(jù)綁定(小結(jié))
本篇文章主要介紹了探索Vue高階組件的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
elementUI多選框反選的實(shí)現(xiàn)代碼
這篇文章主要介紹了elementUI多選框反選的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vuex 如何動(dòng)態(tài)引入 store modules
這篇文章主要介紹了vuex 如何動(dòng)態(tài)引入 store modules,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue2?自定義?el-radio-button?的樣式并設(shè)置默認(rèn)值的方法
這篇文章主要介紹了vue2?自定義?el-radio-button?的樣式并設(shè)置默認(rèn)值的操作方法,代碼分為html部分和css修改樣式代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
vue.js 實(shí)現(xiàn)點(diǎn)擊展開(kāi)收起動(dòng)畫效果
這篇文章主要介紹了vue.js 實(shí)現(xiàn)點(diǎn)擊展開(kāi)收起動(dòng)畫效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Vue如何獲取new Date().getTime()時(shí)間戳
在Web開(kāi)發(fā)中,前端使用Vue.js獲取的是毫秒級(jí)時(shí)間戳,而PHP后端則是秒級(jí)時(shí)間戳,處理此類問(wèn)題時(shí),需要將PHP的時(shí)間戳乘以1000轉(zhuǎn)換為毫秒級(jí),以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10
vant IndexBar實(shí)現(xiàn)的城市列表的示例代碼
這篇文章主要介紹了vant IndexBar實(shí)現(xiàn)的城市列表的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

