in source/image-handler/image-handler.ts [260:299]
public async getOverlayImage(bucket: string, key: string, wRatio: string, hRatio: string, alpha: string, sourceImageMetadata: sharp.Metadata): Promise<Buffer> {
const params = { Bucket: bucket, Key: key };
try {
const { width, height } = sourceImageMetadata;
const overlayImage: S3.GetObjectOutput = await this.s3Client.getObject(params).promise();
const resizeOptions: ResizeOptions = {
fit: ImageFitTypes.INSIDE
};
// Set width and height of the watermark image based on the ratio
const zeroToHundred = /^(100|[1-9]?[0-9])$/;
if (zeroToHundred.test(wRatio)) {
resizeOptions.width = Math.floor((width * parseInt(wRatio)) / 100);
}
if (zeroToHundred.test(hRatio)) {
resizeOptions.height = Math.floor((height * parseInt(hRatio)) / 100);
}
// If alpha is not within 0-100, the default alpha is 0 (fully opaque).
const alphaValue = zeroToHundred.test(alpha) ? parseInt(alpha) : 0;
const imageBuffer = Buffer.isBuffer(overlayImage.Body) ? overlayImage.Body : Buffer.from(overlayImage.Body as Uint8Array);
return await sharp(imageBuffer)
.resize(resizeOptions)
.composite([
{
input: Buffer.from([255, 255, 255, 255 * (1 - alphaValue / 100)]),
raw: {
width: 1,
height: 1,
channels: 4
},
tile: true,
blend: 'dest-in'
}
])
.toBuffer();
} catch (error) {
throw new ImageHandlerError(error.statusCode ? error.statusCode : StatusCodes.INTERNAL_SERVER_ERROR, error.code, error.message);
}
}