Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: image resizing without ImageMagick

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.

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.

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?

Edit: I've changed the accepted solution to sharp as the previous solution (lwip) is no longer maintained. Thanks for all your feedback!

like image 816
zacr0 Avatar asked Jun 03 '14 22:06

zacr0


People also ask

How do I resize an image in node JS?

NodeJS – Resize() is an inbuilt function that is used to resize the images to the desired size. We can use resize to set the height and width using a 2-pass bilinear algorithm. It can resize an image into any size as declared by the user. We can take input from the user or resize it into fixed Width*Height size.

How do I resize an image in Visual Studio?

Hold down the Shift key and drag a sizing handle until the image is the right size.

How do I resize Imagemagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new. png.


2 Answers

I would vote for sharp:

sharp('input.jpg')   .resize(200, 200)   .toFile('ouput.jpg', function(err) {     // output.jpg is a 200 pixels wide and 200 pixels high image     // containing a scaled and cropped version of input.jpg   }); 

It's fast, typically 6x faster than the fastest imagemagick-based node bindings, and runs in very little memory, perhaps 10x less. sharp links to the libvips image library directly, there is no shelling out to an external program, and the library itself is faster and more efficient than *magick at this task. It supports useful things like stream, buffer and filesystem input and output, colour management, transparency, promises, overlays, WebP, SVG, and more.

As of sharp 0.20, npm will automatically download complete pre-compiled binaries on most platforms, so there's no need for node-gyp. Just enter:

npm install sharp 

or:

yarn add sharp 

And off you go.

like image 140
jcupitt Avatar answered Sep 18 '22 17:09

jcupitt


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.

What you are asking for would be done as follows:

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

More info at the module's Github repo.

like image 25
EyalAr Avatar answered Sep 20 '22 17:09

EyalAr