node-gd

libgd C++ bindings for Node.js

node-gd

GD graphic library (libgd) C++ bindings for Node.js.

This version is the community-maintained official NodeJS.org node-gd repo. Be aware that since node-gd version 0.3.x only libgd2 version 2.1.x is fully supported. Run gdlib-config --version to check the version of libgd2 on your system.

$ 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

Please open an issue if you have the answer. I'm sure it works, I just don't have ready the exact commands.

# Require library 
gd = require 'node-gd'
 
# Create blank new image in memory 
output_img = gd.create widthheight
 
# Load existing image file on disk into memory 
gd.openPng "test.png"(err, input_img) ->
  console.log "width: "input_img.width
  console.log "height: "input_img.width
 
# Render input over the top of output 
input_img.copyResampled output_imgdstXdstYsrcXsrcYdstWdstHsrcWsrcH
 
# Write image buffer to disk 
output_img.savePng "out.png"0(err) ->
  console.log "image saved!"
// Require library 
var gd = require('node-gd');
 
// Create blank new image in memory 
var img = gd.create(200, 80);
 
// Set background color 
img.colorAllocate(0, 255, 0);
 
// Set text color 
var txtColor = img.colorAllocate(255, 0, 255);
// Set full path to font file 
var fontPath = '/full/path/to/font.ttf';
 
// Render string in image 
img.stringFT(txtColor, fontPath, 24, 0, 10, 60, 'Hello world!');
 
// Write image buffer to disk 
img.savePng('output.png', 1, function(err) {
  if(err) {
    throw err;
  }
});
 
// Destroy buffer 
img.destroy();
 

As usual, for the latest examples, review the easy-to-follow ./test/test.coffee.

The function gdImageGrayScale is not available in libgd2 version 2.0.x. Therefore, the following script although it works, will output a green image on systems with that version. This means: the function call to img.toGrayscale just doesn't do anything and returns only this.

var gd = require('../js/node-gd.js');
 
var img = gd.create(200, 80);
 
img.colorAllocate(0, 255, 0);
 
img.toGrayscale();
 
img.savePng('output.png', 1, function(err) {
  if(err) {
    throw err;
  }
  img.destroy();
});
$ npm test

The test/output directory contains the resulting images of the test script. The tests use, in some cases, a GNU Freefont font, which is licensed under the GNU General Public License v3.