Node js dynamic images- error

微信扫一扫,分享到朋友圈

Node js dynamic images- error

This piece of code is used to create dynamic images based on the height and width parameters set ..Say localhost:3000/50/50 would give an image of width 50 and height 50..I am using this code I got from github..I have installed imageMagick in my system.

var http = require('http');
var url = require('url');
var fs = require('fs');
var gm = require('gm');
var server = http.createServer(function(request, response){
var url_parts = url.parse(request.url).path.substring(1).split("/");
var width = parseInt(url_parts[0]);
var height = parseInt(url_parts[1]);
var max = Math.max(width, height);
if(!isNaN(width) && !isNaN(height))
{
response.writeHead(200, {'content-type': 'image/png'});
gm('nodejs.png').
resize(max, max).
crop(width, height, 0, 0).
stream(function(err, stdout, stderr){
if(err) {
console.log(err)
}
else {
stdout.pipe(response);
}
});
}
else {
response.writeHead(400, {'content-type' : 'text/plain'});
response.end();
}
})
.listen(3000);

This is the error I get

events.js:72 throw er; // Unhandled 'error' event ^ Error: spawn ENOENT at errnoException (child_process.js:980:11) at Process.ChildProcess._handle.onexit (child_process.js:771:34)

The file nodejs.png exists in the same directory as that of the project.What is is that I am doing wrong?

Problem courtesy of: Prasanna Aarthi

Solution

You need to add this line.. of code after installign imageMagicks in your system

var gm = require('gm').subClass({ imageMagick: true });

This did the trick and it works now..

Solution courtesy of: Prasanna Aarthi

Discussion

Almost certainly you need to install ImageMagic or GraphicsMagic. My guess is that gm
module is just a wrapper around the graphic management command line tools. So when you invokesomething like resize()
node
will try to invoke /usr/bin/convert
which is not found, thus you receive spawn child_process
error.

To install imagemagic you could type sudo apt-get install imagemagic
in Ubuntu.

Discussion courtesy of: exebook

This recipe can be found in it’s original form on Stack Over Flow
.

微信扫一扫,分享到朋友圈

Node js dynamic images- error

小米10出征:堆料这双旧鞋,高端那条老路

上一篇

疫情下的数据化应用思考:3个表现,1套工具,4项评测

下一篇

你也可能喜欢

Node js dynamic images- error

长按储存图像,分享给朋友