Use GD library in NodeJS to copy and resample image

I try, unsuccessfully, to find an equivalent in NodeJS of PHP GD function imagecopyresampled.

This is not simply resize an image, but get a portion of an image and then put it into another image.

I find this libraries :

  • gm
  • canvas
  • imagemagick
  • easyimage
  • node-gd

But they have no equivalent. It's the same thing with the function imagecreatetruecolor and they just simply resize/crop image without select part of image with offset and specified width/height selection.

Does anyone know the NodeJS equivalent ?

Ok, I found the response, I use node-gd. In previous search, i find an obsolete gd library for node.

This is the correct library : https://github.com/mikesmullin/node-gd

And to create empty imgage, use this function : createTrueColor(width, height) To resample or cut image, use this : copyResampled()

This is similar to PHP functions, with same parameters. The wiki is available here : https://github.com/taggon/node-gd/wiki

And basic example :

var fs   = require('fs');
var path = require('path');
var gd   = require('gd');
var source = './test.png';
var target = './test.thumb.png';

if (path.exists(target)) fs.unlink(target);

gd.openPng(
    source,
    function(png, path) {
        if(png) {
            var w = Math.floor(png.width/2), h = Math.floor(png.height/2);
            var target_png = gd.createTrueColor(w, h);

            png.copyResampled(target_png,0,0,0,0,w,h,png.width,png.height);
            target_png.savePng(target, 1, gd.noop);
    }
});