Vue使用mixins實現(xiàn)壓縮圖片代碼
更新時間:2018年03月14日 10:29:26 作者:一念
本篇文章主要介紹了Vue使用mixins實現(xiàn)壓縮圖片代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本文介紹了Vue使用mixins實現(xiàn)壓縮圖片代碼,分享給大家,具體如下:
圖片壓縮
創(chuàng)建mixins image-compress.js
export default {
methods: {
/**
* 檢查并壓縮圖片大小
*/
checkAndHandleCompression(file) {
return new Promise((resolve, reject) => {
this.imgBase64(file, (image, canvas) => {
let maxSize = 2 * 1024; // 2M (單位KB)
let fileSize = file.size / 1024; // 圖片大小 (單位KB)
let uploadSrc, uploadFile;
if (fileSize > maxSize) { // 如果圖片大小大于maxSize,進(jìn)行壓縮
uploadSrc = canvas.toDataURL(file.type, maxSize / fileSize);
uploadFile = this.convertBase64UrlToFile(uploadSrc, file.name); // 轉(zhuǎn)成file文件
} else {
uploadSrc = image.src;
uploadFile = file;
}
let compressedSize = uploadFile.size / 1024;// 壓縮后圖片大小 (單位KB)
// 判斷圖片大小是否小于maxSize,如果大于則繼續(xù)壓縮至小于為止
if (compressedSize.toFixed(2) > maxSize) {
this.checkAndHandleUpload(uploadFile);
} else {
let fileOptions = {uploadSrc, uploadFile};
resolve(fileOptions);
}
});
});
},
/**
* 將圖片轉(zhuǎn)化為base64
*/
imgBase64(file, callback) {
// 看支持不支持FileReader
if (!file || !window.FileReader) return;
// 創(chuàng)建一個 Image 對象
let image = new Image();
// 綁定 load 事件處理器,加載完成后執(zhí)行
image.onload = function () {
// 創(chuàng)建 canvas DOM 對象
let canvas = document.createElement('canvas');
// 返回一個用于在畫布上繪圖的環(huán)境, '2d' 指定了您想要在畫布上繪制的類型
let ctx = canvas.getContext('2d');
// 如果高度超標(biāo) // 參數(shù),最大高度
let MAX_HEIGHT = 3000;
if (image.height > MAX_HEIGHT) {
// 寬度等比例縮放 *=
image.width *= MAX_HEIGHT / image.height;
image.height = MAX_HEIGHT;
}
// 獲取 canvas的 2d 環(huán)境對象,
// 可以理解Context是管理員,canvas是房子
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 重置canvas寬高
canvas.width = image.width;
canvas.height = image.height;
// 將圖像繪制到canvas上
ctx.drawImage(image, 0, 0, image.width, image.height);
callback(image, canvas);
};
if (/^image/.test(file.type)) {
// 創(chuàng)建一個reader
let reader = new FileReader();
// 將圖片將轉(zhuǎn)成 base64 格式
reader.readAsDataURL(file);
// 讀取成功后的回調(diào)
reader.onload = function () {
// 設(shè)置src屬性,瀏覽器會自動加載。
// 記住必須先綁定事件,才能設(shè)置src屬性,否則會出同步問題。
image.src = this.result;
};
}
},
/**
* 把Base64轉(zhuǎn)換成file文件
*/
convertBase64UrlToFile(dataurl, filename) {
let 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 File([u8arr], filename, {type: mime});
}
}
};
example
<template> ... </template> <script> import imageUploadMixins from '@/mixins/image-compress'; export default { mixins: [imageUploadMixins], ... methods:{ handleUploadImage(e){ let file = e.target.files[0]; this.checkAndHandleCompression(file).then( fileOptions => { // let {uploadSrc, uploadFile} = fileOptions; // 壓縮完成使用 uploadSrc, uploadFile ... }); } } ... } </script> <style> ... </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用vuetify實現(xiàn)全局v-alert消息通知的方法
使用強(qiáng)大的Vuetify開發(fā)前端頁面,結(jié)果發(fā)現(xiàn)官方?jīng)]有提供簡便的全局消息通知組件,所以自己動手寫了一個簡單的組件,接下來通過本文給大家介紹使用vuetify實現(xiàn)全局v-alert消息通知的詳細(xì)代碼,感興趣的朋友跟隨小編一起看看吧2024-02-02
Vue基礎(chǔ)之MVVM,模板語法和數(shù)據(jù)綁定
這篇文章主要為大家介紹了Vue之MVVM,模板語法和數(shù)據(jù)綁定,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12
使用vite創(chuàng)建vue3之vite.config.js的配置方式
這篇文章主要介紹了使用vite創(chuàng)建vue3之vite.config.js的配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
vue draggable resizable gorkys與v-chart使用與總結(jié)
這篇文章主要介紹了vue draggable resizable gorkys與v-chart使用與總結(jié),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09

