1.安装
npm i img-compressor -S
2.创建实例对象
import { Create } from 'img-compressor';
const imgCompressor = new Create({
width: 300,
height: 90,
mineType: 'image/png',
quality: 0.6,
});
3.压缩图片
视图
<input type="file" accept="image/*" ref="txFile" @change="onFileChange">
onFileChange
onFileChange() {
const fileInput = this.$refs.txFile;
let file = fileInput.files[0];
imgCompressor.compress(file, {
success: (result: File) => {
console.log('图片压缩成功');
},
error: (err: Error) => {
console.log('图片压缩失败:', err);
},
});
}
4.GitHub地址
https://github.com/innovational/img-compressor