-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_icons.js
More file actions
57 lines (48 loc) · 1.46 KB
/
Copy pathconvert_icons.js
File metadata and controls
57 lines (48 loc) · 1.46 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
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
// 确保已安装sharp库
try {
require.resolve('sharp');
} catch (e) {
console.log('正在安装sharp库,用于图像处理...');
exec('pnpm install sharp', (error) => {
if (error) {
console.error('安装sharp失败,请手动安装: pnpm install sharp');
process.exit(1);
} else {
console.log('sharp安装成功,继续处理图标...');
convertIcons();
}
});
return;
}
// 转换图标的主函数
async function convertIcons() {
const sharp = require('sharp');
const sizes = [16, 48, 128];
const svgPath = path.join(__dirname, 'public', 'images', 'icon.svg');
try {
// 检查SVG文件是否存在
if (!fs.existsSync(svgPath)) {
console.error(`SVG文件不存在: ${svgPath}`);
return;
}
// 读取SVG文件
const svgBuffer = fs.readFileSync(svgPath);
// 为每个尺寸创建PNG
for (const size of sizes) {
const outputPath = path.join(__dirname, 'public', 'images', `icon${size}.png`);
await sharp(svgBuffer)
.resize(size, size)
.png()
.toFile(outputPath);
console.log(`已创建 ${size}x${size} 图标: ${outputPath}`);
}
console.log('所有图标已成功转换!');
} catch (error) {
console.error('转换图标时出错:', error);
}
}
// 如果sharp已安装,直接执行转换
convertIcons();