前端 react 实现图片上传前压缩 缩率图
创始人
2024-11-04 19:34:42
0

目录

一、安装

二、编写工具类

三、获取压缩后的File对象


一、安装

npm install compressorjs 或 yarn add compressorjs

官方文档:compressorjs - npm (npmjs.com)

二、编写工具类

/**  * @author Dragon Wu  * @since 2024/8/4 12:23  * 图片压缩工具  */ import Compressor from 'compressorjs';  // 压缩图片方法 (中间件) export function compressor(file: File, defaultQuality: number = 0.6) {     // console.log('压缩前: ', (file.size / 1024 / 1024).toFixed(1), 'M');//控制台打印图片大小     const filesize: number = parseFloat((file.size / 1024 / 1024).toFixed(1));     let quality = defaultQuality;     if (filesize < 0.8) { //这里可以设置自己的压缩规则         quality = 1;     } else if (filesize < 1) {         quality = 0.8;     } else if (filesize < 1.5) {         quality = 0.7;     } else if (filesize < 2) {         quality = 0.5;     } else if (filesize < 3) {         quality = 0.33;     } else if (filesize < 4) {         quality = 0.25;     } else if (filesize < 5) {         quality = 0.2;     } else if (filesize < 8) {         quality = 0.125;     } else if (filesize < 10) {         quality = 0.1;     } else {         return new Promise((resolve, reject) => {             reject({                 msg: '图片不能超过10M'             })         });     }     // console.log('压缩比例: ', quality);      return new Promise((resolve) => {         new Compressor(file, {             quality: quality,             success(result: File | Blob) {                 // console.log('压缩后: ', (result.size / 1024 / 1024).toFixed(1), 'M');                 if (result instanceof Blob) {                     //@ts-ignore                     result = new File([result], "f" + (result?.name as string).slice(-8), {type: result.type})                 }                 resolve(result);             },             error(err) {                 // 压缩报错的话 返回原图片                 resolve(file);             },         });     }); }  

三、获取压缩后的File对象

const handleBeforeUpload = (file: FileType) => {         const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';         if (!isJpgOrPng) {             message.warning('只能上传JPG/PNG格式!').then();         }         const isLt2M = file.size / 1024 / 1024 < 10;         if (!isLt2M) {             message.warning('图片不得超过10MB!').then();         }          compressor(file).then(res => {             if (isJpgOrPng && isLt2M) {                 onChange(res)             }         })          return false;     }

四、测试效果

可以看到源图片已经被压缩了,这样处理后再提交至服务器就能节省带宽,提高前端加载效率了,注意质量数值设置的过小图片过大可能导致图片失帧。

总结到此!

相关内容

热门资讯

第八分钟辅助!pokerwor... 第八分钟辅助!pokerworld软件(透视)一直是真的挂,安装教程(有挂工具)-哔哩哔哩1、不需要...
透视攻略!wepoker手机版... 透视攻略!wepoker手机版辅助,wepoker辅助是真的吗,妙招教程(有挂存在)-哔哩哔哩1)w...
透视计算!wpk模拟器多开(透... 透视计算!wpk模拟器多开(透视)都是是有挂,分享教程(有挂插件)-哔哩哔哩1、操作简单,无需注册,...
开挂辅助工具!wepoker开... 开挂辅助工具!wepoker开挂功能,wepoker透视开挂挂,(查到实测外挂辅助工具)是一款可以让...
六分钟解谜!wepoker底牌... 六分钟解谜!wepoker底牌透视脚本(透视)本来是真的挂,安装教程(竟然有挂)-哔哩哔哩1、wep...
透视脚本!hhpoker有没有... 透视脚本!hhpoker有没有辅助挂,hardrock透视工具,诀窍教程(真的有挂)-哔哩哔哩暗藏猫...
辅助工具!wepoker软件有... 无需打开直接搜索;操作使用教程:辅助工具!wepoker软件有挂吗,wepoker有没有挂透视挂,(...
透视模拟器!wepoker辅助... 透视模拟器!wepoker辅助真的假的(透视)竟然真的有挂,揭秘攻略(有挂测试)-哔哩哔哩1、完成w...
第二分钟关于!wepoker好... 第二分钟关于!wepoker好友助力码(透视)其实真的有挂,细节揭秘(有挂技术)-哔哩哔哩1、用户打...
透视存在!hhpoker免费透... 透视存在!hhpoker免费透视脚本,pokemmo辅助器,绝活教程(有挂分享)-哔哩哔哩进入游戏-...