vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮詳解
簡(jiǎn)介
vue項(xiàng)目中,上傳圖片時(shí)如果圖片很大,通過(guò) image-conversion 壓縮到指定大小
1. 安裝依賴
npm i image-conversion --save
2. 引用
import * as imageConversion from 'image-conversion'
3. 使用
const newFile = new Promise((resolve) => {
// 壓縮到500KB,這里的500就是要壓縮的大小,可自定義
imageConversion.compressAccurately(file, 500).then(res => {
resolve(res)
}).finally(() => {
console.log('將圖片文件壓縮到了500kb')
})
})
4. 實(shí)際場(chǎng)景應(yīng)用
<!-- 上傳按鈕 --> <el-upload action="" class="upload" multiple accept=".png, .jpg, .jpeg" :before-upload="beforeDocumentUpload" :http-request="beforeAvatarUpload" :on-preview="handlePictureCardPreview" :before-remove="handlerBeforeRemove" :file-list="pictureList" :limit="10" :on-exceed="handleExceed" list-type="picture-card" > <i class="el-icon-plus" /> </el-upload> <!-- 預(yù)覽大圖 --> <el-dialog :visible.sync="imgVisible" :append-to-body="true"> <img width="100%" :src="dialogImageUrl" alt=""> </el-dialog>
methods:
methods: {
// 上傳前
beforeDocumentUpload(file) {
const size = file.size / 1024 / 1024
// 上傳的圖片大小不能超過(guò)10M
if (size > 10) {
this.$message.warning('文件大小不能超過(guò)10M!')
return false
}
const extension = this.getFileType(file)
// 只支持 png, jpg, jpeg 格式
if (!['png', 'jpg', 'jpeg'].includes(extension)) {
this.$message.warning('只能上傳png、jpg、jpeg格式文件!')
return false
}
// 大于0.5M壓縮成0.5M
if (size > 0.5) {
const loading = this.$loading({
lock: true,
text: '加載中'
})
// 壓縮
const newFile = new Promise((resolve) => {
// 壓縮到500KB,這里的500就是要壓縮的大小,可自定義
imageConversion.compressAccurately(file, 500).then(res => {
resolve(res)
}).finally(() => {
loading.close()
})
})
console.log('newFIle', newFile)
return newFile
}
return true
},
// 上傳
beforeAvatarUpload(file) {
const self = this
const reader = new FileReader()
reader.readAsDataURL(file.file)
reader.onload = function(e) {
// const img_base64 = e.target.result
// 自定義數(shù)組對(duì)象,傳給后臺(tái)的數(shù)據(jù)
self.imgBase64Array.push({
uid: file.file.uid,
base64Str: file
// base64Str: img_base64
})
}
},
// 預(yù)覽大圖
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url
this.imgVisible = true
},
// 刪除圖片
handlerBeforeRemove(file, fileList) {
this.imgBase64Array = this.imgBase64Array.filter((p) => p.uid !== file.uid)
},
handleExceed() {
this.$message.warning('圖片數(shù)量最多為10張')
},
},總結(jié)
到此這篇關(guān)于vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮的文章就介紹到這了,更多相關(guān)vue image-conversion實(shí)現(xiàn)圖片壓縮內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用vue組件自定義v-model實(shí)現(xiàn)一個(gè)Tab組件方法示例
這篇文章主要給大家介紹了關(guān)于利用vue組件自定義v-model實(shí)現(xiàn)一個(gè)Tab組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Vue實(shí)現(xiàn)拖放排序功能的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了Vue中實(shí)現(xiàn)拖放排序功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vue2.0如何借用vue-pdf實(shí)現(xiàn)在線預(yù)覽pdf文件
這篇文章主要介紹了vue2.0如何借用vue-pdf實(shí)現(xiàn)在線預(yù)覽pdf文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue踩坑記錄之?dāng)?shù)組定義和賦值問(wèn)題
這篇文章主要給大家介紹了關(guān)于vue踩坑記錄之?dāng)?shù)組定義和賦值問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
vue3?使用?vue3-video-play實(shí)現(xiàn)在線視頻播放
這篇文章主要介紹了vue3?使用?vue3-video-play?進(jìn)行在線視頻播放,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

