forked from songxiaofeng1981/best
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertDirectoryToWebp.js
More file actions
118 lines (90 loc) · 3.9 KB
/
convertDirectoryToWebp.js
File metadata and controls
118 lines (90 loc) · 3.9 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
function convertHeicToPng(inputFile, outputDir) {
const outputFileName = path.basename(inputFile, path.extname(inputFile)) + '.png';
const outputFile = path.join(outputDir, outputFileName);
execSync(`convert "${inputFile}" "${outputFile}"`);
return outputFile;
}
function compressToWebp(inputFile, outputDir) {
// 生成最终的 WebP 文件名
const outputFileName = path.basename(inputFile, path.extname(inputFile)) + '.webp';
const outputFile = path.join(outputDir, outputFileName);
// 创建一个临时文件路径,用于存储调整大小后的图片
const tempOutputFile = path.join(outputDir, 'temp_' + path.basename(inputFile));
// 首先,使用 convert 命令调整图片到 1080p 分辨率
execSync(`convert "${inputFile}" -resize 1920x1080 "${tempOutputFile}"`);
// 然后,将调整后的图片转换为 WebP 格式
execSync(`cwebp -q 75 "${tempOutputFile}" -o "${outputFile}"`);
// 可选:删除临时文件
fs.unlinkSync(tempOutputFile);
}
// 使用示例
// compressToWebp('path/to/your/input/file.jpg', 'path/to/output/directory');
function copyFileToBackup(inputFile, backupDir) {
const backupFile = path.join(backupDir, path.basename(inputFile));
fs.copyFileSync(inputFile, backupFile);
fs.unlinkSync(inputFile);
}
function processImages(inputDir, outputDir, backupDir) {
const files = fs.readdirSync(inputDir);
const supportedExtensions = ['.heic', '.png', '.jpg', '.jpeg']; // 添加其他支持的图片格式
files.forEach(file => {
const fileExt = path.extname(file).toLowerCase();
if (!supportedExtensions.includes(fileExt)) {
return; // 如果文件不是支持的格式,跳过处理
}
const inputFile = path.join(inputDir, file);
if (fileExt === '.heic') {
// 转换 HEIC 到 PNG
const convertedFile = convertHeicToPng(inputFile, outputDir);
// 压缩转换后的 PNG 到 WEBP
compressToWebp(convertedFile, outputDir);
// 删除中间的 PNG 文件
fs.unlinkSync(convertedFile);
} else {
// 压缩其他格式的图片到 WEBP
compressToWebp(inputFile, outputDir);
}
// 复制原始文件到备份目录并删除原文件
copyFileToBackup(inputFile, backupDir);
});
}
function convertDirectoryToWebp(directory) {
const supportedExtensions = ['.heic', '.png', '.jpg', '.jpeg']; // 支持的图片格式
// 递归遍历目录
function processDirectory(dir) {
const items = fs.readdirSync(dir, { withFileTypes: true });
items.forEach(item => {
const fullPath = path.join(dir, item.name);
if (item.isDirectory()) {
// 如果是目录,递归处理
processDirectory(fullPath);
} else {
// 处理文件
const fileExt = path.extname(item.name).toLowerCase();
if (supportedExtensions.includes(fileExt)) {
processFile(fullPath, fileExt, dir);
}
}
});
}
function processFile(filePath, fileExt, outputDir) {
if (fileExt === '.heic') {
// 转换 HEIC 到 PNG,然后到 WEBP
const pngFile = convertHeicToPng(filePath, outputDir);
compressToWebp(pngFile, outputDir);
fs.unlinkSync(pngFile); // 确保删除中间的 PNG 文件
fs.unlinkSync(filePath); // 删除原始的 HEIC 文件
} else {
// 直接转换到 WEBP
compressToWebp(filePath, outputDir);
fs.unlinkSync(filePath); // 删除原始文件
}
}
processDirectory(directory);
}
// 调用函数处理图片
const inputPath = '/app/input';
convertDirectoryToWebp(inputPath);