Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Usage

taggon edited this page · 9 revisions
Clone this wiki locally

Basics

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
gd.createFromPng("path/to/image.png",
    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(
    Math.floor(jpeg.width/2),
    Math.floor(jpeg.height/2)
);
jpeg.copyResampled(
    thumb,
    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);
Something went wrong with that request. Please try again.