I am trying to resize image in node.js by this program .
https://github.com/LearnBoost/node-canvas/blob/master/examples/resize.js
var Canvas = require('canvas')
, Image = Canvas.Image
, fs = require('fs');
开发者_JS百科var img = new Image
, start = new Date;
img.src = __dirname + 'flowers.jpg';
console.log('Resized and saved in %dms');
img.onload = function(){
console.log('Resized and saved in buffer');
try{
var width = img.width / 2
, height = img.height / 2
, canvas = new Canvas(width, height)
, ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height);
canvas.toBuffer(function(err, buf){
console.log('Resized and saved in buffer');
fs.writeFile(__dirname + '/resize.jpg', buf, function(){
console.log('Resized and saved in %dms', new Date - start);
});
});
}catch(e){
console.log(sys.inspect(e));
}
};
img.onerror = function(err){
throw err;
};
the program is not going in the onload function why ? Edit : give this error while trying img.src after attaching the onload and onerror events?
`Resized and saved in NaNms
/home/reach121/rahul/knox/index.js:33
throw err;
^
Error: error while reading from input stream
at Object.<anonymous> (/home/reach121/rahul/knox/index.js:35:9)
at Module._compile (module.js:404:26)
at Object..js (module.js:410:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at Array.0 (module.js:423:10)
at EventEmitter._tickCallback (node.js:170:26)
Using Image Magic giving me this error :
reach121@youngib:~/rahul/knox$ sudo node index.js
node.js:178
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Command failed: execvp(): No such file or directory
at ChildProcess.<anonymous> (/usr/local/lib/node/.npm/imagemagick/0.1.2/package/imagemagick.js:64:15)
at ChildProcess.emit (events.js:67:17)
at Socket.<anonymous> (child_process.js:172:12)
at Socket.emit (events.js:64:17)
at Array.<anonymous> (net.js:826:12)
at EventEmitter._tickCallback (node.js:170:26)
Code :
var im = require('imagemagick');
im.resize({
srcPath: __dirname + '/flowers.jpg',
dstPath: __dirname + '/flowers-small.jpg',
width: '50%'
}, function(err, stdout, stderr){
if (err) throw err
console.log('resized')
});
Have you already tried setting img.src
after attaching the onload
and onerror
events? Just noticed this as a difference between the original example and yours.
Another question: is the onerror
event triggered? If so, the thrown exception could be helpful.
Update
If you just want to resize images and don't need to use any canvas-specific operations, simply use ImageMagick and node-imagemagick. I just did a small test and it worked out of the box:
var im = require('imagemagick');
im.resize({
srcPath: __dirname + '/koala.jpg',
dstPath: __dirname + '/koala-small.jpg',
width: '50%'
}, function(err, stdout, stderr){
if (err) throw err
console.log('resized')
});
For the node_imagemagick module to work you need to install the imagemagick CLI interfaces,
On a mac it can be as easy as:
brew install imagemagick
But this really depends on your specific system.
__dirname
does not have a trailing slash.
Change it to this:
img.src = __dirname + '/flowers.jpg';
And attach event handlers before setting img.src like @schaermu said.
FYI, your console.log statement uses a %d but there's no variable after the quoted string. (I'm not sure this solves your other errors.)
精彩评论