npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lx-frontend/img-compress

v1.0.2

Published

一个用来操作图片和获取图片信息的工具库。

Downloads

11

Readme

@lx-frontend/img-compress

一个用来操作图片和获取图片信息的工具库。

安装

pnpm i -D @lx-frontend/img-compress

使用

导入函数并调用

const {
  getImgsUnderFloderNeedCompress,
  findAllImgsNeedMoveToLocal,
  getPngShouldTransferToJpg
} = require('@lx-frontend/img-compress')

getImgsUnderFloderNeedCompress(/* 文件夹路径 */)
// ...

| 函数 | 功能 | | ---- | ---- | | compressImage | 压缩单张图片 | | getImgsUnderFloderNeedCompress | 获取文件夹下需要压缩的图片 | | isImgNeedCompress | 判断图片是否需要压缩 | | compressImagesUnderFloderIfNeeded | 找到文件夹下可以压缩的图片,压缩并输出到目标文件夹 | | getAllImgInfoUnderFloderByExt | 获取文件夹下所有以参数ext为后缀名的图片,如'.png' | | isNeedMoveToLocal | 判断图片是否需要作为本地图片使用 | | findAllImgsNeedMoveToLocal | 获取文件夹下所有应该作为本地图片使用的图片列表 | | isPngNeedTransferToJpg | 判断png图盘是否需要转换成jpg | | getPngShouldTransferToJpg | 获取文件夹下所有需要转换成jpg的png图片 | | transferPngToJpg | 将png图片转换成jpg,并输出到目标路径 | | transferAllPngToJpgUnderFloderIfNeeded | 将文件夹下所有需要转换成jpg的png图片转换成jpg,并输出到目标文件夹 |

命令方式使用

| 命令 | 功能 | | ---- | ---- | | imgX | 显示所有可用命令 | | isImgNeedCompress | 检查图片是否需要压缩 | | getAllImgNeedCompressUnderFloder | 获取文件夹下所有需要压缩的图片 | | compressImgUnderFloderIfNeeded | 找到文件夹下所有需要压缩的图片,压缩后,输出到目标文件夹 | | compressImg | 压缩单张图片 | | isNeedMoveToLocal | 检查图片是否需要移动到本地 | | getAllNeedMoveToLocal | 获取文件夹下所有需要移动到本地的图片 | | isPngNeedTransToJpg | 检查png图片是否需要转换为jpg | | getAllPngShouldTransferToJpg | 获取指定文件夹下所有需要转换为jpg的png图片 | | transferPngToJpg | 将png图片转换为jpg | | transferAllPngToJpgUnderFloderIfNeeded | 找到所有需要转换为jpg的png图片,转换后,输出到目标文件夹 |

举例:imgX getAllImgNeedCompressUnderFloder path/to/src 打印path/to/src下所有需要压缩的图片路径

具体使用方式,请使用imgX [命令名] -h查看

imgX可以打印所有可以使用的命令和相关信息

特别说明

Q: 工具库压缩质量选取的是多少? A: 该库采用的sharp进行图片操作,压缩质量默认85。函数提供了修改这个值的参数,但是命令行没有提供修改的接口。

Q: 工具库是如何判断图片是否需要压缩? A: 工具库首先尝试对目标图片进行了压缩,然后获取压缩后的图片大小和压缩之前的大小对比,如果图片体积缩小了30%以上,可以认为图片需要压缩。

Q: 对压缩过的图片再进行是否需要压缩判断,是否会二次压缩? A: 不会,经过压缩的图片,再次压缩已经没有多少优化空间了,即使降低压缩质量,也只能获得微小的体积优化,远远不及30%。

Q: 如何判断图片是否应该放在远程还是本地? A: 默认图片如果小于1KB,放在本地,大于1KB,可按需要放在本地或者远程。暂时没有提供修改这个阈值的入口。

Q: 如何判断png是否需要转换为jpg? A: sharp可以遍历图片的所有像素点,如果所有的像素点都是不透明的,则可以判断可以转为jpg。