Node.js:不使用ImageMagick调整图像大小

Node.js:不使用ImageMagick调整图像大小

问题描述:

我正在Node.js(+ express 4)上开发一个Web应用程序,用户可以通过将其上传到服务器来设置他们的个人资料图像。我们已经限制了文件mimetype和max filesize,因此用户无法上传超过200KB的png或jpeg图像。

I'm developing a web app on Node.js (+ express 4) where users can set their profile image by uploading it to the server. We already limit the file mimetype and max filesize, so the user can't upload more than 200KB png or jpeg images.

问题是我们要调整大小(服务器端)上传的图像分辨率为200x200,以改善页面加载并节省磁盘空间。经过一些研究,所有答案都指向使用基于ImageMagick或GraphicsMagick的任何模块。

The problem is we'd like to resize (serverside) the uploaded image resolution to 200x200 to improve page loading and saving space on disk. After some research, all answers pointed to using any module based on ImageMagick or GraphicsMagick.

但是,必须安装ImageMagick / GraphicsMagick进行简单的图像调整大小对我来说似乎太过分了,所以除了这个以外还有其他解决方案吗?

However, having to install ImageMagick/GraphicsMagick to do a simple image resizing seems too overkill for me, so, is there any other solution other than this for Node.js?

我最近开始为NodeJS开发一个没有任何运行时依赖性的图像处理模块(阅读原因)。它仍处于早期阶段,但已经可以使用。

I have recently started developing an image processing module for NodeJS without any runtime dependencies (read why). It's still at early stages, but already usable.

您要求的内容如下:

image.resize(200, 200, function(err, image){
    // encode resized image to jpeg and get a Buffer object
    image.toBuffer('jpg', function(err, buffer){
        // save buffer to disk / send over network / etc.
    });
});

模块的更多信息 Github回购