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

webp-batch-convert-p

v0.0.6

Published

webp 图片批量转换。将指定目录内 png/jpg/jpeg/bmp/gif 格式的图片批量转换为 webp 格式。

Downloads

10

Readme

Webp Batch Convert

webp 图片批量转换。将指定目录内 png/jpg/jpeg/bmp/gif 格式的图片批量转换为 webp 格式。

该工具是基于lzwme/webp-batch-convert二次开发, 添加一些配置项,以及细节优化。

### 新增配置
1. replace_suffix: true, 开启替换webp后缀,默认保留源文件后缀。
2. gif: true 过滤gif图片, 直接将gif图片copy到新文件夹,保留动效。
3. cover: true 开启后强制覆盖,默认跳过,如图片已被转换则跳过。
4. del_old_dir: true 开启删除当前文件夹,将格式化后目录名改为已删除文件夹名。

### 优化
1. 当文件超出一定数量 递归执行会造成内存溢出 需要优化 递归调用方式
“事件驱动” (Event-Driven)的特性
在 JavaScript 中,由于其 “事件驱动” (Event-Driven)的特性,使用 "setTimeout"、 “nextTick” 等方式对指定函数的调用,实际上是将该函数的引用(指针)储存起来,并在适当的时候调用。[参考](https://github.com/pfan123/Articles/issues/50)。
2. 空文件过滤,file.size 为零判空。
3. 目标嵌套目录可深度工作。

快速上手

在项目目录中安装

npm install --save-dev webp-batch-convert-p

使用示例(nodejs 模块 API 方式)

//import convert from 'webp-batch-convert-p';
const convert = require('webp-batch-convert-p');
let res;

// 示例一: 生成 img 目录下的图片文件至 webp 目录
res = convert.cwebp('./img', './webp');
console.log('total: ', res);

// 示例二: 生成 img 目录下的图片文件至 webp 目录,附带质量等参数
// 更多参数参考:https://developers.google.com/speed/webp/docs/cwebp?csw=1#options
// 也可以执行如下命令通过 cwebp 帮助信息了解: `yarn cwebp --longhelp`
const cwebpOpts = {
    /** don't print anything */
    quiet: true,
    /** quality factor (0:small..100:big), default=75 */
    q: 75,
    /** transparency-compression quality (0..100), default=100 */
    alpha_q: 100,
    /** spatial noise shaping (0:off, 100:max), default=50 */
    sns: 50,
    /** filter strength (0=off..100), default=60 */
    f: 60,
    /** use simple filter instead of strong */
    nostrong: false,
    /** 是否替换图片后缀 */
    replace_suffix: true
};
// 清空输出目录
convert.utils.delDir('./webp');
res = convert.cwebp('./img','./webp', cwebpOpts);
console.log('total: ', res);

best-practice

命令行方式使用(cwebp-batch)

全局安装

npm install -g webp-batch-convert-p

使用示例

cwebp-batch --in img-folder --out webp-folder <-q 75 -quiet>

或者局部安装,然后如下方式使用:

./node_modules/.bin/cwebp-batch --in img-folder --out webp-folder <-q 75 -quiet>

API

  • .cwebp(imgDir, webpDir, cwebpOptions)

批量转换生成 webp。示例:

// 将 img 目录下的所有图片转换为 webp 文件,输出至 webp 目录
const res = convert.cwebp('./img','./webp', {
    quiet: true, // 不输出详情
    q: 60        // 质量
});
console.log('total: ' + res);
  • .utils.mkDir(dirPath)

创建一个(深度的)目录。示例:

// 创建目录
convert.utils.mkDir('./src/assets/webp');
  • .utils.delDir(dirPath, ext)

清空一个(非空的)目录。示例:

// 删除 webp 目录
convert.utils.delDir('./webp');
// 删除 webp 目录下的所有 webp 后缀的文件
convert.utils.delDir('./webp', 'webp');
// 删除 webp 目录下的所有 .webp 后缀的文件
convert.utils.delDir('./webp', /\.webp$/);

二次开发

  • 依赖安装 yarn install
  • 修改/新增功能
  • 添加测试并执行 yarn test
  • cwebp-batch 命令行命令全局安装与测试 npm i -g ./

License

webp-batch-convert-p is released under the MIT license.