How to create a new image with [node] graphicsmagick using toBuffer
I am trying to create a new image that will eventually be inserted into the mongo database via gridfs. I would rather not write anything to the filesystem, so the best route seems to be to create a new image , write the result to a buffer, and then insert the buffer into the database. Unfortunately the toBuffer method does n't work for new images:
var gm = require( 'gm' );
gm(200, 200, '#000F')
.setFormat('png')
.fill('black')
.drawCircle( 50, 50, 60, 60 )
.toBuffer( 'PNG', function( error, buffer )
{
if( error ) { console.log( error ); return; }
console.log( 'success: ' + buffer.length );
}
);
... throws the following error:
[Error: stream gives empty buffer]
+3
source to share
1 answer
It looks like gm just doesn't like the argument 'PNG'
you are passing toBuffer
. I don't think this is necessary since you already specified the format as png earlier. Try to simply remove it:
var gm = require( 'gm' );
gm(200, 200, '#000F')
.setFormat('png')
.fill('black')
.drawCircle( 50, 50, 60, 60 )
.toBuffer(function( error, buffer )
{
if( error ) { console.log( error ); return; }
console.log( 'success: ' + buffer.length );
}
);
+4
source to share