【开源】用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 小时前
图解 MongoDB 04|索引模型:每建一个索引,就是在 B+-tree 森林里多栽一棵
后端·mongodb·agent
anOnion6 小时前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
用户47949283569157 小时前
claude Fable用不了?把Gpt 5.5pro接到你的claude code里
前端·后端
GetcharZp9 小时前
告别 Nginx 复杂配置!这款带 Web 面板的万能代理神器,让端口转发变得如此简单
后端
zhangxingchao9 小时前
Kotlin常用的Flow 操作符整理
前端
IT_陈寒11 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic12 小时前
SwiftUI 手势笔记
前端·后端
金銀銅鐵12 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
橙子家12 小时前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
前端