迁移
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import sharp from 'sharp';
|
||||
|
||||
const imgDir = path.join(process.cwd(), 'src/assets/image'); // 图片根目录
|
||||
const imgTypes = ['.png', '.jpg', '.jpeg', '.gif']; // 支持的图片类型
|
||||
|
||||
async function convertImagesToWebp(dir) {
|
||||
try {
|
||||
const files = await fs.readdir(dir);
|
||||
for (const file of files) {
|
||||
const filePath = path.join(dir, file);
|
||||
const extname = path.extname(filePath);
|
||||
const itemInfo = await fs.stat(filePath);
|
||||
|
||||
if (itemInfo.isFile() && imgTypes.includes(extname)) {
|
||||
const outputFilePath = path.join(dir, `${path.basename(file, extname)}.webp`);
|
||||
await sharp(filePath)
|
||||
.webp({ quality: 80 }) // 调整质量
|
||||
.toFile(outputFilePath);
|
||||
|
||||
console.log(`Converted: ${filePath} to ${outputFilePath}`);
|
||||
} else if (itemInfo.isDirectory()) {
|
||||
await convertImagesToWebp(filePath); // 递归处理子目录
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
convertImagesToWebp(imgDir);
|
||||
Reference in New Issue
Block a user