I have a Node.js app. In this app, I'm loading an image file from Azure Storage using this API. Specifically, I'm using the createReadStream
function. That function provides a stream to read from the image. Right now, my code looks like this:
let chunks = [];
let sharp = require('sharp');
let fileStream = this.fileService.createReadStream('[MY_SHARE]', '[MY_DIRECTORY]', '[MY_PICTURE_NAME]', (err, fileResult, res) => {
console.log('Sharpening image...');
console.log(imageChunks);
sharp(chunks).resize(100, 100).toFile('resized.png', function(err1, info) {
if (err1) {
console.log('oops');
}
});
});
fileStream.on('data', function(chunk) {
chunks.push(chunk);
});
In this block, notice that I'm trying to use the Sharp node module. Before the line sharp(chunks)...
, I'm printing out image chunks to the console. When printed in the console, I see the following:
[ <Buffer 87 52 4e 47 0d 0a 1a 0a 01 02 04 0d 49 48 44 52 00 00 06 18 00 00 06 0f 08 06 00 00 00 75 c2 f0 a2 00 00 00 04 67 42 4d 41 00 00 b2 8f 0a fc 61 05 00 ... > ]
However, when I call Sharp, I get an error that says:
Error: Unsupported input object
According to the docs, the Sharp Constructor allows for either a String or a Buffer. Looking at what's printed to the console as shown above, it looks like I'm passing a Buffer to Sharp. Am I misunderstanding something? If so, what?
Please change the following line of code:
sharp(chunks).resize(100, 100).toFile('resized.png', function(err1, info) {
if (err1) {
console.log('oops');
}
});
to
sharp(Buffer.concat(chunks)).resize(100, 100).toFile('resized.png', function(err1, info) {
if (err1) {
console.log('oops');
console.log(err1);
}
});
and that should fix the problem. If I am not mistaken, chunks
actually is an array of Buffer
whereas sharp
library expects a Buffer
so we would need to create a new buffer using all the array elements.