Initial commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import sharp from "sharp";
|
||||
|
||||
class ThumbnailerService {
|
||||
async fromStream(source, resizeOptions, fit) {
|
||||
const imageBuffer = await this.#streamToBuffer(source);
|
||||
// const dimensions = this.getDimensions(imageBuffer, percentage, {width, height});
|
||||
return sharp(imageBuffer)
|
||||
.resize({
|
||||
...resizeOptions,
|
||||
fit: fit || 'contain'
|
||||
})
|
||||
.withMetadata()
|
||||
.png()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
#streamToBuffer(stream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const buffers = [];
|
||||
stream.on("data", function (data) {
|
||||
buffers.push(data);
|
||||
});
|
||||
|
||||
stream.on("end", function () {
|
||||
resolve(Buffer.concat(buffers));
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const instance = new ThumbnailerService();
|
||||
export {instance as thumbnailer};
|
||||
Reference in New Issue
Block a user