|
|
|
@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
const scaleDir = `${inputDir}/1x1`;
|
|
|
|
|
|
|
|
if (!inputDir || !scaleDir) {
|
|
|
|
|
|
|
|
console.log('未执行拼接商品主图背景功能,工作目录不存在');
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function mergeMainImgBg(folderPath, row = 2) {
|
|
|
|
|
|
|
|
const images = fs.readdirSync(folderPath);
|
|
|
|
|
|
|
|
const imagePaths = images
|
|
|
|
|
|
|
|
.map((image) => path.join(folderPath, image))
|
|
|
|
|
|
|
|
.filter((img) => {
|
|
|
|
|
|
|
|
// 这里使用Array.from将Set类型转为数组
|
|
|
|
|
|
|
|
return Array.from(cover.imgFormat).some((item) => img.endsWith(item));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 删除之前生成的商品主图背景
|
|
|
|
|
|
|
|
imagePaths.some((item, index) => {
|
|
|
|
|
|
|
|
if (item.endsWith('商品主图背景.png')) {
|
|
|
|
|
|
|
|
fs.rmSync(item, { force: true });
|
|
|
|
|
|
|
|
imagePaths.splice(index, 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (imagePaths.length < 4) {
|
|
|
|
|
|
|
|
console.log(`当前1x1文件夹下少于4张图片`);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//去掉多余的图片,只需要4张来拼背景
|
|
|
|
|
|
|
|
imagePaths.length = 4;
|
|
|
|
|
|
|
|
// 处理剩余的图片
|
|
|
|
|
|
|
|
const remainingImagesInfo = await Promise.all(
|
|
|
|
|
|
|
|
imagePaths.map(async (imagePath) => {
|
|
|
|
|
|
|
|
const { data, info } = await sharp(imagePath).resize({ width: 1000, fit: 'inside' }).toBuffer({ resolveWithObject: true });
|
|
|
|
|
|
|
|
return { data, info }; // 返回图片数据及其高度
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
// 获取第一张图片的高度
|
|
|
|
|
|
|
|
const { width, height } = remainingImagesInfo[0].info;
|
|
|
|
|
|
|
|
const outputImagePath = path.join(folderPath, `商品主图背景.png`);
|
|
|
|
|
|
|
|
await sharp({
|
|
|
|
|
|
|
|
create: {
|
|
|
|
|
|
|
|
width: width * 2, // 宽度使用第一张图片的宽度
|
|
|
|
|
|
|
|
height: height * 2, // 高度为两张图片的总高度
|
|
|
|
|
|
|
|
channels: 4,
|
|
|
|
|
|
|
|
background: { r: 255, g: 255, b: 255, alpha: 0 }, // 背景透明
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.composite([
|
|
|
|
|
|
|
|
...remainingImagesInfo.map(({ data, info }, index) => {
|
|
|
|
|
|
|
|
const currentRow = Math.floor(index / row);
|
|
|
|
|
|
|
|
const currentCol = index % row;
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
input: data,
|
|
|
|
|
|
|
|
top: currentRow * info.height, // 计算纵向位置
|
|
|
|
|
|
|
|
left: currentCol * info.width, // 计算横向位置
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
.toFile(outputImagePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`生成商品主图背景成功`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mergeMainImgBg(scaleDir);
|