How do I create a new image with [node] graphicsmagick using toBuffer

余生长醉 提交于 2019-12-10 21:16:27

问题


I'm trying to create a new image which will eventually be insert into a mongo database via gridfs. I'd prefer to avoid writing anything to the filesystem so the best route seems to create a new image, write the result to a buffer, then insert the buffer into the database. Unfortunately, the toBuffer method doesn'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 );
    }
);

... yeilds the following error:

[Error: Stream yields empty buffer]


回答1:


It looks like gm simply doesn't like the 'PNG' argument you're giving to toBuffer. I don't think its necessary either since you've already specified the format as png earlier on. Try simply removing 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 );
    }
);


来源:https://stackoverflow.com/questions/30361361/how-do-i-create-a-new-image-with-node-graphicsmagick-using-tobuffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!