This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.mjs
70 lines (61 loc) · 1.74 KB
/
convert.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* jpg pngをwebpに変換してsrc/imagesに保存します。
* windows環境でavifへの変換がエラーになるため、未完成です。
* avif画像は別の手段で生成してsrc/imagesディレクトリに入れてください。
*/
import { ImagePool } from "@squoosh/lib";
import { cpus } from "os";
import { readFile, writeFile } from "fs/promises";
import path from "path";
import glob from "glob";
const IMAGE_SRC_DIR = "./src/images";
// 圧縮オプション
const webpEncodeOptions = {
webp: {
lossless: 1,
},
};
// windows環境でエラーになるため対応していません。
// const avifEncodeOptions = {
// avif: {
// cqLevel: 0,
// },
// };
// 画像フォルダ内のJPGとPNGを抽出
const convertImageFiles = glob.sync(`${IMAGE_SRC_DIR}/**/*.+(jpg|png)`);
// 抽出したファイルの情報をconvertImagePoolListにセット
const convertImagePool = new ImagePool(cpus().length);
const convertImagePoolList = await Promise.all(convertImageFiles.map(async (file) => {
const imageFile = await readFile(file);
const { dir, name } = path.parse(file)
const image = convertImagePool.ingestImage(imageFile);
await image.decoded;
return { dir, name, image };
}));
// 圧縮
await Promise.all(
convertImagePoolList.map(async (item) => {
const { image } = item;
await image.encode(webpEncodeOptions)
})
);
// 画像をwebpに変換して出力
await Promise.all(
convertImagePoolList.map(async (item) => {
const {
name,
dir,
image: { encodedWith },
} = item;
// 圧縮したデータを格納
let data;
if (encodedWith.webp) {
data = await encodedWith.webp;
}
// 書き込み
if (data) {
await writeFile(`${dir}/${name}.webp`, data.binary);
}
})
)
await convertImagePool.close();