在node.js中调整图像大小

发布于 2024-11-04 11:34:30 字数 2455 浏览 1 评论 0原文

我正在尝试通过此程序调整 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆蝶 2024-11-11 11:34:30

您是否已尝试在附加 onloadonerror 事件后设置 img.src ?只是注意到这是原始示例和您的示例之间的区别。

另外一个问题:onerror事件是否被触发?如果是这样,抛出的异常可能会有所帮助。

更新

如果您只想调整图像大小并且不需要使用任何特定于画布的操作,只需使用 ImageMagicknode-imagemagick。我刚刚做了一个小测试,它开箱即用:

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')
});

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')
});
一身骄傲 2024-11-11 11:34:30

为了使 node_imagemagick 模块工作,您需要安装 imagemagick CLI 界面,

在 Mac 上,它可以很简单:

brew install imagemagick

但这实际上取决于您的特定系统。

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.

妳是的陽光 2024-11-11 11:34:30

__dirname 没有尾部斜杠。

将其更改为:

img.src = __dirname + '/flowers.jpg';

并在设置 img.src 之前附加事件处理程序,如 @schaermu 所说。

__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.

十年不长 2024-11-11 11:34:30

仅供参考,您的 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文