前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的詳細(xì)過程
安裝
npm install compressorjs
使用
在使用ElementUI或者其他UI框架的上傳組件時(shí),都會(huì)有上傳之前的鉤子函數(shù),在這個(gè)函數(shù)中可以拿到原始file,這里我用VantUI的上傳做演示
afterRead 函數(shù)是上傳之前的鉤子,可以獲取到原始file
<template>
<div>
<van-uploader
:max-count="prop.limit"
v-model="state.fileList"
:after-read="afterRead"
:before-read="beforeRead"
@delete="deleteFile"
/>
</div>
</template>
<script setup>
import { reactive, defineEmits, defineProps, watch } from 'vue'
import { FileUploadFun } from '@/api/index.js'
import { useCustomFieldValue } from '@vant/use'
import { Toast } from 'vant'
import Compressor from 'compressorjs'
const prop = defineProps({
url: {
type: String,
default: '',
},
limit: {
type: Number,
default: 5,
},
})
const emit = defineEmits(['onSuccess'])
const state = reactive({
fileList: [],
})
watch(
() => prop.url,
() => {
if (prop.url) {
state.fileList = []
prop.url.split(',').forEach((item) => {
state.fileList.push({
url: item,
})
})
}
}
)
// 文件上傳之前對(duì)圖片進(jìn)行壓縮
const beforeRead = (file) => {
return new Promise((resolve, reject) => {
new Compressor(file, {
// 壓縮質(zhì)量,0-1之間。數(shù)字越小,壓縮比越高
quality: 0.2,
success(result) {
// 默認(rèn)返回result是blob類型的文件,可以轉(zhuǎn)成file并返回,交給afterRead鉤子進(jìn)行上傳
let newFile = new File([result], file.name, { type: file.type })
resolve(newFile)
},
error(err) {
reject(err)
},
})
})
}
// 文件上傳后觸發(fā)
const afterRead = (file) => {
file.status = 'uploading'
file.message = '上傳中...'
FileUploadFun(file.file)
.then((res) => {
file.status = 'done'
file.message = '上傳成功'
let urls = state.fileList.map((i) => i.url)
urls.pop()
urls.push(res.data.url)
// 通知外界上傳成功
emit('onSuccess', urls.join(','))
})
.catch(() => {
state.fileList.pop()
file.status = 'done'
Toast('上傳失敗')
})
}
// 文件刪除后觸發(fā)
const deleteFile = () => {
emit('onSuccess', state.fileList.map((i) => i.url).join(','))
}
// 用于返回信息
useCustomFieldValue(() => state.fileList.map((item) => item.url).join(','))
</script>
示例
| Quality | 原始大小 | 壓縮后大小 | 壓縮比 | Description |
|---|---|---|---|---|
| 0 | 2.12 MB | 114.61 KB | 94.72% | - |
| 0.2 | 2.12 MB | 349.57 KB | 83.90% | - |
| 0.4 | 2.12 MB | 517.10 KB | 76.18% | - |
| 0.6 | 2.12 MB | 694.99 KB | 67.99% | 推薦 |
| 0.8 | 2.12 MB | 1.14 MB | 46.41% | 推薦 |
| 1 | 2.12 MB | 2.12 MB | 0% | 不推薦 |
| NaN | 2.12 MB | 2.01 MB | 5.02% | - |
測(cè)試效果

可以看到壓縮前的圖片大小3.29M,壓縮后343KB

下面是前后的圖片對(duì)比
原圖

壓縮后的圖

總結(jié)
到此這篇關(guān)于前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的文章就介紹到這了,更多相關(guān)前端Compressor.js圖片壓縮上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何在JavaScript中優(yōu)雅的提取循環(huán)內(nèi)數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于如何在JavaScript中優(yōu)雅的提取循環(huán)內(nèi)數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
js實(shí)現(xiàn)PC端和移動(dòng)端刮卡效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)PC端和移動(dòng)端刮卡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
JS實(shí)現(xiàn)京東首頁(yè)之頁(yè)面頂部、Logo和搜索框功能
這篇文章主要實(shí)現(xiàn)京東的頁(yè)面頂部,logo和搜索框功能,本文有效果展示,頁(yè)面布局詳細(xì)分析,具體實(shí)現(xiàn)代碼,介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
EasyUi中的Combogrid 實(shí)現(xiàn)分頁(yè)和動(dòng)態(tài)搜索遠(yuǎn)程數(shù)據(jù)
jquery easyui中的combogrid比較特殊,算是combo和grid的組合,combogrid結(jié)合一個(gè)可編輯的文本框和下拉數(shù)據(jù)網(wǎng)格面板,可以讓用戶迅速找到并選擇,又可以進(jìn)行搜索,展示與當(dāng)前輸入的字符相匹配的數(shù)據(jù)。下面給大家介紹EasyUi中的Combogrid 實(shí)現(xiàn)分頁(yè)和動(dòng)態(tài)搜索遠(yuǎn)程數(shù)據(jù)2016-04-04
微信小程序?qū)崿F(xiàn)時(shí)間預(yù)約功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)時(shí)間預(yù)約基本功能,一個(gè)類似電商的時(shí)間預(yù)約功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
JS使用tofixed與round處理數(shù)據(jù)四舍五入的區(qū)別
js中一般使用tofixed與round處理數(shù)據(jù)四舍五入,那么tofixed與round有什么區(qū)別呢?下面小編給大家分享JS使用tofixed與round處理數(shù)據(jù)四舍五入的區(qū)別,需要的朋友參考下吧2017-10-10

