GD graphics library (libgd) C++ bindings for Node.js
GD graphics library, libgd, C++ bindings for Node.js. This version is the community-maintained official NodeJS node-gd repo. With
node-gd you can easily create, manipulate, open and save paletted and true color images from and to a variety of image formats including JPEG, PNG, GIF and BMP.
$ sudo apt-get install libgd2-xpm-dev # libgd$ npm install node-gd
$ sudo yum install gd-devel$ npm install node-gd
$ brew install gd$ npm install node-gd
...or using MacPorts
$ sudo port install gd2$ npm install node-gd
Hopefully will be working on this soon. Up until then: sorry, won't build on Windows.
There are different flavours of images, of which the main ones are palette-based (up to 256 colors) and true color images (millions of colors). GIFs are always palette-based, PNGs can be both palette-based or true color. JPEGs are always true color images.
gd.create() will create a palette-based base image while
gd.createTrueColor() will create a true color image.
Full API documentation and more examples can be found in the doc directory.
gd.createTrueColor are async. You will have to modify your code. This means that you should either change all
gd.create calls to
gd.createTrueColorSync or rewrite those call and wrap further calls in a callback.
Example of synchronously creating a rectangular image with a bright green background and in magenta the text "Hello world!"
// Require libraryvar gd = require'node-gd';// Create blank new image in memoryvar img = gdcreateSync200 80;// Set background colorimgcolorAllocate0 255 0;// Set text colorvar txtColor = imgcolorAllocate255 0 255;// Set full path to font filevar fontPath = '/full/path/to/font.ttf';// Render string in imageimgstringFTtxtColor fontPath 24 0 10 60 'Hello world!';// Write image buffer to diskimgsavePng'output.png' 1iferrthrow err;;
Asynchronous example of drawing a red lined hexagon on a black background:
var gd = require'node-gd';gdcreateTrueColor200200if error throw error;var points =x: 100 y: 20x: 170 y: 60x: 170 y: 140x: 100 y: 180x: 30 y: 140x: 30 y: 60x: 100 y: 20;imgsetThickness4;imgpolygonpoints 0xff0000;imgbmp'test1.bmp' 0;;
var gd = require'node-gd';gdopenFile'/path/to/file.jpg'if errthrow err;imgemboss;imgbrightness75;imgsaveFile'/path/to/newFile.bmp'if errthorw err;;;
Some output functions are synchronous because they are handled by libgd. An example of this is the creation of animated GIFs.
Copyright (c) 2010-2015 the contributors. Apparently since December 27th 2012, node-gd is licensed under an MIT license.
The current version is based on code created by taggon, here the original author's repo, and on the additions by mikesmullin. Porting node-gd to nan and extending the API is done by y-a-v-a, on Twitter as @_y_a_v_a_.