【开源】用electron写了一个图片无损压缩软件TinyPanda

图片压缩在前端开发日常中的使用频率实在是太高了,使用得最高的就是tinypng但是它有限制,就想着能不能搞个没有限制的图片压缩工具出来。

然后在掘金上找技术方案看到了大佬粥里有勺糖的文章纯前端实现 JPG 图片压缩 | canvas - 掘金 (juejin.cn),技术上是已经完全通了,怎样把它做成一个好看好用的产品呢,于是就模仿tinypng的UI,用electron打包成了一个可以在业务场景下正常使用的产品。

GitHub地址 github.com/jddk/TinyPa...

产品效果

技术栈

vue3 electron

选择文件后的处理

这里的压缩其实只对png和jpg做了处理,并加了熊猫的状态变化,处理中的时候上坐着的,处理成功后熊猫就开心的站起来了

javascript 复制代码
async function changeFile(event) {
  happy.value = false;
  // 取出原始文件,取出不同项
  const differentItems = [...event.target.files].filter((item1) => {
    return !org_files.value.some((item2) => {
      let bol = item2.size == item1.size && item2.name == item1.name;
      return bol;
    });
  });
  org_files.value = [...org_files.value, ...differentItems];

  let compressedFile = null;
  //  遍历压缩
  differentItems.forEach(async (file, i) => {
    if (await isPNG(file)) {
      compressedFile = await compressPNGImage(file, {
        quality: 1,
        noCompressIfLarger: true,
      });
    }else {
      compressedFile = await compressJPGImage(
        file,
        "browser-image-compression",
        {
          quality: 1,
          noCompressIfLarger: true,
        }
      );
    }
    compressedFile.org_size = differentItems[i].size;

    comp_files.value.push(compressedFile);
  });
  event.srcElement.value = null;
  setTimeout(() => {
    calcTotal();
  }, 500);
}

统计压缩比率

javascript 复制代码
function calcTotal() {
  let total_org = 0;
  let total_comp = 0;
  comp_files.value.forEach((item) => {
    total_org += item.org_size;
    total_comp += item.size;
  });
  total.value = Number.parseInt(((total_org - total_comp) / total_org) * 100);
  saveSpace.value = formatSize(total_org - total_comp);
  happy.value = true;
}

全部下载转zip

javascript 复制代码
function downAll() {
  const zip = new JSZip();

  // 将每个 File 对象添加到 zip 文件中
  comp_files.value.forEach((file, index) => {
    zip.file(file.name, file);
  });

  // 生成 zip 文件
  zip.generateAsync({ type: "blob" }).then((content) => {
    downloadFile(content, `TinyJpg${new Date().getTime()}.zip`);
  });
}

github中的TinyPanda源码地址

github中的tinyPanda安装包

TinyJpg - 在 Windows 上免费下载并安装 |Microsoft Store

相关推荐
程序员阿鹏5 分钟前
List和Set的区别
java·开发语言·数据结构·后端·list
小oo呆5 分钟前
【自然语言处理与大模型】LangChainV1.0入门指南:核心组件Messages
前端·javascript·easyui
IT 行者9 分钟前
SpringBoot版本升级插件:用OpenRewrite 轻松升级 Spring Boot 2 到 4
java·spring boot·后端
摸鱼仙人~25 分钟前
企业级 RAG 问答系统开发上线流程分析
后端·python·rag·检索
果壳~34 分钟前
【前端】【canvas】图片颜色填充工具实现详解
前端
Bigger35 分钟前
Tauri (23)——为什么每台电脑位置显示效果不一致?
前端·rust·app
¥懒大王¥36 分钟前
XSS-Game靶场教程
前端·安全·web安全·xss
ssshooter41 分钟前
为什么移动端 safari 用 translate 移动元素卡卡的
前端·css·性能优化
我命由我1234542 分钟前
Python 开发问题:No Python interpreter configured for the project
开发语言·后端·python·学习·pycharm·学习方法·python3.11
闲云一鹤1 小时前
Claude Code 接入第三方AI模型(MiMo-V2-Flash)
前端·后端·claude