taggon edited this page May 25, 2011 · 9 revisions
Clone this wiki locally


The node-gd lets you create an image using GD graphic library. You should load the module by require() before you use it.

var gd = require('gd');

All functions were derived from GD library. For example, createFromPng() is originated from gdImageCreateFromPng(). Some functions such as openPng(), openGif() and saveJpeg() support asynchronous I/O. To get use asynchronous I/O, you must pass a callback function as a last argument.

Here is a short example:

var gd = require('gd');

// Asynchronous I/O
    function(img){ // Pass a callback function to load async
        var color = img.colorAllocate(255, 0, 0); // allocate red color
        img.line(0, 0, 100, 50, color); // draw red line from (0, 0) to (100, 50)
        // Pass a callback function to save async
        img.saveGif("path/to/image2.gif", gd.noop); // noop is a null function

// Synchronous I/O
var jpeg = gd.createFromJpeg("path/to/image.jpg");
var thumb = gd.createTrueColor(
    0, 0,
    0, 0,
    thumb.width, thumb.height,
    jpeg.width, jpeg.height

// second argument means jpeg quality
thumb.saveJpeg("path/to/image_thumb.jpg", 80);