在node.js中调整图像大小
我正在尝试通过此程序调整 node.js 中的图像大小。
https://github.com/LearnBoost/node-canvas/blob /master/examples/resize.js
var Canvas = require('canvas')
, Image = Canvas.Image
, fs = require('fs');
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;
};
程序没有进入 onload 函数,为什么? 编辑 : 在附加 onload 和 onerror 事件后尝试 img.src 时出现此错误?
`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)
使用 Image Magic 给我这个错误:
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)
代码:
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')
});
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');
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')
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否已尝试在附加
onload
和onerror
事件后设置img.src
?只是注意到这是原始示例和您的示例之间的区别。另外一个问题:
onerror
事件是否被触发?如果是这样,抛出的异常可能会有所帮助。更新
如果您只想调整图像大小并且不需要使用任何特定于画布的操作,只需使用 ImageMagick 和 node-imagemagick。我刚刚做了一个小测试,它开箱即用:
Have you already tried setting
img.src
after attaching theonload
andonerror
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:
为了使 node_imagemagick 模块工作,您需要安装 imagemagick CLI 界面,
在 Mac 上,它可以很简单:
但这实际上取决于您的特定系统。
For the node_imagemagick module to work you need to install the imagemagick CLI interfaces,
On a mac it can be as easy as:
But this really depends on your specific system.
__dirname
没有尾部斜杠。将其更改为:
并在设置 img.src 之前附加事件处理程序,如 @schaermu 所说。
__dirname
does not have a trailing slash.Change it to this:
And attach event handlers before setting img.src like @schaermu said.
仅供参考,您的 console.log 语句使用 %d 但带引号的字符串后面没有变量。 (我不确定这是否能解决您的其他错误。)
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.)