gdimage v0.0.3
Yet Another GD Graphics Library Wrapper For Node.JS
This software wraps popular LibGD graphics library into Node.JS module which helps interpreting graphics inside Node.JS.
Rather than writing dozens of native codes, this software uses ffi to wrap libgd api methods into JavaScript functions.
Usage
gdimage requires ref and node-ffi which are native modules, so you need a C++ compiler (gcc, llvm, msvc etc.)
before installing it.
Also, GD native library is required. In Linux / OSX, you can install it with a package manager
npm install gdimage
(sudo) apt-get install libgd3 # in debian/ubuntu
(sudo) yum install gd # in fedora/centos
brew install gd # in osxIn Windows, you need to download and install libgd manually, see Gd for Windows
Usage example:
var gd = require('gdimage');
var image = gd.create(200, 100);
var white = image.allocateColor('#ffffff'),
red = image.allocateColor('#ff0000');
image.fill(0, 0, 200, 100, white);
image.line(0, 0, 200, 100, red, true);
var buf = image.encode('png');
require('fs').writeFileSync('out.png', buf);API Reference
function create
function create(width:int, height:int, trueColor:boolean = true): GDImageCreates a new GDImage instance.
When trueColor set to true, the image is in TrueColor mode, the total color count is unlimited.
Parameters:
widththe image width, in pixelsheightthe image height, in pixelstrueColorwhether TrueColor mode is enabled, default to true.
Returns:
a new GDImage instance
function decode
function decode(buf:Buffer, format:string = 'auto'): GDImageDecodes a image file content into a GDImage instance.
Parameters:
bufa buffer that contains file content.formata string or mime type denoting the format of the image. The format name is case insensitive. When set to'auto', the file format is guessed by the file head. Supported image formats are:bmpimage/bmpapplication/x-ms-bmpapplication/x-bmpjpgjpejpegjfifjfijifimage/jpeggifimage/gifpngimage/pngtifftifimage/tiff
Returns:
a GDImage instance, which maybe TrueColor or not, depending on the file format.
function trueColor
function trueColor(r, g, b, a): Color
function trueColor(rgb:string): Color
function trueColor(rgba:string): ColorResolves a 32 bit RGBA true color. trueColor accepts several types of arguments:
gd.trueColor('#RRGGBB'), where 'RRGGBB' is hex color codes. For Example#FF0000denotesredgd.trueColor('#RRGGBBAA')gd.trueColor(r, g, b), wherer,g,bare digits between0~255gd.trueColor(r, g, b, a), whereais a number between0and1
So gd.trueColor('#ff00ff') is equivalent to gd.trueColor('#ff00ffff'), as well as gd.trueColor(255, 0, 255, 1)
class GDImage
A GDImage object wraps a native libgd image instance, and wraps several native methods into javascript functions
which helps us working with it. Methods and fields available are as follows.
int GDImage::width, int GDImage::height
the width and height of the image, in pixels.
GDIMage::toTrueColor
function toTrueColor(): GDImageConverts a platte image to true color. Calling this on a true color image has no effects.
Returns the GDImage itself.
GDImage::destroy
function destroy()Destroys the image, frees memories and resources. A GDImage must be freed manually to prevent memory leak.
GDImage::allocateColor
function allocateColor(r, g, b, a): ColorAllocates a color in the color space. image.allocateColor accepts same arguments as gd.trueColor.
If the image is in true color mode, image.allocateColor acts like gd.trueColor.
If the image is in platte mode, for example it is decoded from a PNG8 or GIF file, the color allocation
may fail because the color platte cannot have more colors. To prevent the potential failure, you can
- convert the image to true color mode by calling
image.toTrueColor(). - find a closest color from the platte by calling
image.getClosestColor(rgba) - use
image.resolveColor(rgba)
GDImage::getColor
Gets an existing color from the color space. If none matching, an exception is thrown.
image.getColor accepts same arguments as gd.trueColor.
Not that image.GetColor acts like gd.trueColor in true color mode, it will never fail.
GDImage::getClosestColor
Gets an existing color closest to the rgba value from the color space.
image.getClosestColor accepts same arguments as gd.trueColor.
Not that image.GetColor acts like gd.trueColor in true color mode, it will never fail.
GDImage::resolveColor
This method will always return a color instance. First it tries to find a matching color, if none matching, it tries to allocate a new color. If both failed, it returns a closest color from the color space.
Not that image.GetColor acts like gd.trueColor in true color mode, it will never fail.
GDImage::scale
function scale(new_width:int, new_height:int, auto_destroy:boolean = false): GDImageScales the image into new size. If auto_destroy is set to true, the current image is destroyed after the it is scaled.
Returns a new GDImage created.
GDImage::rotate
function rotate(angle:float, bg_color:Color = null, auto_close:boolean = false): GDImageRotates the image clockwise. angle shold be between 0~360. When angle is not 90 180 270, the bg_color is used
to fill the empty.
Returns a new GDImage created.
GDImage::line
function line(x1:int, y1:int, x2:int, y2:int, color:Color, anti_aliased:boolean = false): GDImageDraws a solid line from (x1, y1) to (x2, y2). If anti_aliased is set to true, anti-aliasing is enabled
Returns the GDImage itself.
GDImage::text
function text(str:string, x:int, y:int, size:double, color:Color, angle:float = 0, font:string = "arial"): Array.<number>Writes text with true type font, returns a rect which wraps the text. Only true type fonts are supported.
Parameters:
strtext to writexystart coordinate of the baselinesizetext size in dots/pixelscolorforeground colorangleangle of baseline, in degreesfontname/path of font, such asarialtimescourier, orSymbol.ttf, or/System/Library/Fonts/Symbol.ttfetc. You can supply multi names with;
Returns an array of 4 digits denoting an rectangle of the bounds: [x, y, w, h]
Find out system font names:
In Windows, check C:\WINDOWS\FONTS C:\WINNT\FONTS for files named *.ttf.
In Unix/Linux, check /usr/share/fonts/TrueType /usr/lib/X11/fonts etc.
In Mac/OSX, check /Library/Fonts /System/Library/Fonts.
Note that modern os uses ttc file format which is not supported by libgd.
If you want to use some fonts that the os does not supply, you should download and install the font files
in ttf format or put them in your project directory and specify the font path as below.
Use Your custom font paths:
To use custom font paths, you can supply an environment variable with the paths, separated with : or;
# in posix shell:
GDFONTPATH="/Users/kyrios.li/fonts:/Library/Fonts" node start
# in windows cmd:
set GDFONTPATH="C:\\Users\\kyrios.li\\fonts;C:\\Windows\\Fonts"Or just set the font path in node.js:
process.env.GDFONTPATH = require('path').resolve(__dirname, 'fonts') + ':/Library/Fonts';GDImage::encode
function encode(format:string, auto_close:boolean = false): BufferEncodes the image into image file content. Supported formats are:
bmppngjpgjpeggiftiffWebp