UNPKG

@rytass/file-converter-adapter-image-watermark

Version:

Rytass Utils Storages Images Watermark

34 lines (31 loc) 919 B
import sharp, { gravity } from 'sharp'; import { Readable } from 'stream'; sharp.cache(false); class ImageWatermark { options; constructor(options){ this.options = options; sharp.concurrency(options.concurrency ?? 1); } async convert(file) { let converter; if (file instanceof Buffer) { converter = sharp(file); } else { converter = sharp(); } converter.composite(this.options.watermarks.map((watermark)=>({ input: watermark.image, gravity: watermark.gravity || gravity.southeast }))); // Stream cannot throw when format not supported if (file instanceof Readable) { file.pipe(converter); } if (file instanceof Buffer) { return converter.toBuffer(); } return converter; } } export { ImageWatermark };