feat: 配置话裁剪功能
parent
7649b7b40a
commit
b5e7b83f22
@ -1,14 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "sharp_img",
|
"name": "sharp_img",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
},
|
"cover": "node ./src/product_cover_img.js"
|
||||||
"author": "",
|
},
|
||||||
"license": "ISC",
|
"author": "",
|
||||||
"dependencies": {
|
"license": "ISC",
|
||||||
"sharp": "^0.33.5"
|
"dependencies": {
|
||||||
}
|
"sharp": "^0.33.5"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,63 @@
|
|||||||
|
const sharp = require('sharp');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const { getInputDir } = require('../utils/index');
|
||||||
|
const { cover } = require('../config/index');
|
||||||
|
|
||||||
|
//图片文件夹路径
|
||||||
|
let inputDir = getInputDir();
|
||||||
|
|
||||||
|
if (!inputDir) {
|
||||||
|
console.log('未执行裁剪主图,工作目录不存在');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//创建输出文件夹
|
||||||
|
cover.scaleArr.forEach((item) => {
|
||||||
|
item.dir = `${inputDir}/${item.scale.join('x')}`;
|
||||||
|
if (!fs.existsSync(item.dir)) {
|
||||||
|
fs.mkdirSync(item.dir);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function resizeImage(imageDir, outputDir, scale) {
|
||||||
|
let imgData = await sharp(imageDir).metadata();
|
||||||
|
let config = {
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
};
|
||||||
|
if (imgData.height / scale[1] > imgData.width / scale[0]) {
|
||||||
|
config.width = imgData.width;
|
||||||
|
config.height = Math.floor(imgData.width / (scale[0] / scale[1]));
|
||||||
|
config.top = 0;
|
||||||
|
config.left = 0;
|
||||||
|
await sharp(imageDir).extract(config).toFile(outputDir);
|
||||||
|
} else {
|
||||||
|
config.width = imgData.height;
|
||||||
|
config.height = Math.floor(imgData.height / (scale[0] / scale[1]));
|
||||||
|
await sharp(imageDir)
|
||||||
|
.resize(config.width, config.height, {
|
||||||
|
fit: 'cover',
|
||||||
|
position: 'center',
|
||||||
|
})
|
||||||
|
.toFile(outputDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//读取输入目录下所有文件
|
||||||
|
fs.readdirSync(inputDir).forEach(async (file) => {
|
||||||
|
const ext = file.split('.').pop().toLowerCase();
|
||||||
|
if (cover.imgFormat.has(ext)) {
|
||||||
|
const inputPath = path.join(inputDir, file);
|
||||||
|
cover.scaleArr.forEach((item) => {
|
||||||
|
const outputPath = path.join(item.dir, file);
|
||||||
|
resizeImage(inputPath, outputPath, item.scale)
|
||||||
|
.then(() => {
|
||||||
|
console.log(`${file}的${item.name}图片已生成`);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(`${file}的${item.name}图片生成失败`, err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const { base } = require('../config/index');
|
||||||
|
|
||||||
|
// 获取最终工作目录
|
||||||
|
const getInputDir = () => {
|
||||||
|
if (base.inputDir) {
|
||||||
|
return base.inputDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
base.inputDir = getLatestFolder(base.directoryPath);
|
||||||
|
return base.inputDir;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 获取该文件夹下最新创建的文件夹作为工作目录
|
||||||
|
const getLatestFolder = (directory) => {
|
||||||
|
let currentInputDir = null;
|
||||||
|
|
||||||
|
// 读取指定目录
|
||||||
|
fs.readdirSync(directory, { withFileTypes: true }).forEach((file) => {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
const folderPath = path.join(directory, file.name);
|
||||||
|
const stats = fs.statSync(folderPath);
|
||||||
|
const creationTime = stats.birthtimeMs; // 获取创建时间
|
||||||
|
|
||||||
|
// 找到最新创建的文件夹
|
||||||
|
let latestTime = 0;
|
||||||
|
if (creationTime > latestTime) {
|
||||||
|
latestTime = creationTime;
|
||||||
|
currentInputDir = folderPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (currentInputDir) {
|
||||||
|
console.log('工作目录:', currentInputDir);
|
||||||
|
} else {
|
||||||
|
console.log('未找到工作目录.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentInputDir;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { getInputDir, getLatestFolder };
|
||||||
Loading…
Reference in New Issue