It is possible to create image thumbnails with pure Node.js

Asked

Viewed 768 times

3

Well I found a library, which is great for image manipulation, and it works perfectly, in case the Graphicsmagick for Node.js, but there is a problem, because there are dependencies of other software installed on the machine where Node will be executed, in case the Graphicsmagick, without it installed does not roll, because I believe that "the Node library is just an interface for calling the methods of the software installed, which may be developed in C or C++ (I think)".

This is my 1° project with Node.js, so I’m not really sure if he’s able to do that and where to start. And whenever I try to search for something like "Create thumbnail picture with Node.js", the results always refer to the previously mentioned libraries.

  • So I would like to know first, if it is possible to create image thumbnails only using Node.js?
  • "Secondarily", if possible, if there is any library that assists in this task?
  • And "mentally", the path of stones, or some idea of how to implement this?

Obs¹: My problem in using the cited library (Graphicsmagick for Node.js), is the need to install a software in hosting, which can be a problem for me depending on the company’s security policy.

Obs²: The only resource I need from this library is the resize.

  • Although it should be possible, it is much simpler to use a library. Your server does not let you run npm?

  • @bfavaretto, by npm i install the Node library, but it is also necessary to have the software installed on the machine (and that’s what I can’t). I’ve done the tests on my machine and it doesn’t work without installing the software.

  • What about Imagemagick, your server supports? It’s quite common, and the module gm lets you choose to use it instead of Graphicsmagick.

  • @bfavaretto, the problem is that some clients use windows normal as server, and this is not common in windows, even if it is server.

  • @bfavaretto, just one question, for example a hosting like the Heroku or openshift, you already have that available?

  • I don’t know, Fernando, but on their website you should say.

  • I use Openshift and they allow you ssh access and there you install these things with yum package manager. But be careful, using the same server for HTTP and image compression is not performative. It is better to use something like Resque (Ruby) or Celery (Python)

  • I found this article. Maybe at least part of it is useful to you: http://tonyspiro.com/uploading-resizing-images-fly-node-js-express/

  • 1

    @Pedrovinícius, falls into the same problem, look at the end of the article brew install imagemagick, it is necessary to install Imagemagick, but still thank you for the help.

Show 4 more comments

1 answer

4

Hello, it is possible to create thumbnails through Node.JS, however, it will require a much more advanced knowledge. Currently using external libraries we find the following problems:

  • npm modules require you to pre-install some library like to Imagemagick;
  • Need to start a new external process for each image a be sued.

There is a library called LWIP she works with images JPG, the creator Eyal Arubas, thought exactly on its idea to minimize the problems with image manipulations excluding at most the dependency of other libraries, being necessary only one module npm.

How to use LWIP:

Perform the installation using the command:

npm install lwip

Create the server.js file:

require('lwip').open('sheldon.jpg', function(err, image){

  // manipulação do evento, caso não consiga abrir a img.
  image.batch()
    .resize(45, 45)  

    .writeFile('output.jpg', function(err){
      // manipulação do evento
    });

});

Method .resize() of the LWIP:

image.resize(width, height, inter, callback)
  1. width {Integer}: Pixel width.
  2. height {Integer}: Pixel height (optional). If not specified, shall be calculated by width..
  3. inter {String}: Interpolation(optional) method. By default it is used "Lanczos", possible values:
    • "Nearest-neighbor"
    • "Moving-Average"
    • "linear"
    • "grid"
    • "Cubic"
    • "Lanczos"
  4. callback {Function(err, image)}
  • It seems like a good option, I haven’t tried it yet (because it’s a particular project), but I looked at the documentation and it’s something I hoped for. Just one question you mentioned: "she works with JPG images", and only JPG? In the documentation I found nothing citing this restriction, but I saw that all the examples are with JPG. Do you use this library? If so, she performs well?

  • Unfortunately it only works with JPG, in case you try to send a PNG it will return: PNG is not yet supported. Her performance is good, I’ve used it and her response time is better than expected.

  • 5

    Thanks for using the module, I Hope it’s helpful. PNG support is Planned for v0.0.2.

  • From what I understand the author already add support to PNG and GIF. I didn’t test because I’m not currently developing for Node.js. (@Eyalar, EN: From what I understand the Author has add support PNG and GIF. I not tested for I am not Currently Developing for Node.js).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.