2

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 ?

4

1 回答 1

2

好的,我找到了响应,我使用 node-gd。在之前的搜索中,我找到了一个过时的节点 gd 库。

这是正确的库:https ://github.com/mikesmullin/node-gd

要创建空图像,请使用此功能:createTrueColor(width, height) 要重新采样或剪切图像,请使用此功能:copyResampled()

这类似于 PHP 函数,具有相同的参数。该 wiki 可在此处获得:https ://github.com/taggon/node-gd/wiki

和基本示例:

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);
    }
});
于 2013-07-08T14:32:08.597 回答