考虑一种情况,我想在图像的不同位置添加水印并生成每个图像的单独副本。
图像已上传到AWS S3。
const AWS = require('aws-sdk');
const Jimp = require('jimp');
我正在使用AWS开发工具包来获取这些图像作为缓冲区
const dynamoS3Bucket = new AWS.S3();
let a = dynamoS3Bucket.getObject({ Bucket: 'bucket.name', Key: 'raw_uploads/Sample-image10-highres.jpg' }).promise();
let b = dynamoS3Bucket.getObject({ Bucket: 'bucket.name', Key: 'raw_uploads/Sample-image10-highres.jpg' }).promise();
现在,我正在尝试添加水印,其中 a 是图片,而 b 是徽标
Promise.all([a, b]).then(async resp => {
let aa = watermark(30, resp[0].Body, resp[1].Body, 0, 1);
let bb = watermark(30, resp[0].Body, resp[1].Body, 1, 0);
return Promise.all([aa, bb]);
}).then(respArr => {
return respArr[1].writeAsync('fileName2.jpg');
});
水印功能不变,
async function watermark(percent = 10, base, logo, x_axis = 0, y_axis = 0){
percent = Math.ceil(percent);
if(percent > 70) percent = 70;
baseIMG = await Jimp.read(base);
logoIMG = await Jimp.read(logo);
logoIMG = await logoIMG.resize(Math.ceil(baseIMG.bitmap.width * percent / 100), Jimp.AUTO, Jimp.RESIZE_BEZIER);
x_axis = Math.ceil(x_axis);
y_axis = Math.ceil(y_axis);
if(x_axis !== 0) x_axis = x_axis/Math.abs(x_axis);
if(y_axis !== 0) y_axis = y_axis/Math.abs(y_axis);
console.log({percent, baseIMG, logoIMG, x_axis, y_axis });
if(x_axis === 0) x_axis = Math.ceil((baseIMG.bitmap.width - logoIMG.bitmap.width)/2)
else if (x_axis === 1) x_axis = Math.ceil(baseIMG.bitmap.width - logoIMG.bitmap.width)
else x_axis = 0;
if(y_axis === 0) y_axis = Math.ceil((baseIMG.bitmap.height - logoIMG.bitmap.height)/2)
else if (y_axis === -1) y_axis = Math.ceil(baseIMG.bitmap.height - logoIMG.bitmap.height)
else y_axis = 0;
return baseIMG.composite(logoIMG, x_axis, y_axis, {
mode: Jimp.BLEND_SOURCE_OVER,
opacitySource: 0.4,
opacityDest: 0.3
});
}
问题:第一个处理的图像非常好,因为第二个图像具有2个水印,如下图所示。
我尝试通过一次处理一张图像来解决此问题 嵌套承诺,而不是 Promise.all()。有没有可能的方法来实现这一目标,或者对为什么会发生这种现象做出任何解释?