module gd
The gd module is a module for image processing. It provides the functions of creating, operating, and storing images, and provides a wealth of color, filter, drawing and other related operations.
Here is a sample code that uses the gd module to create and manipulate an image:
1
2
3
4
5
6
7
8
9
10var gd = require('gd');
var img = gd.create(440, 240); // create a 440x240 truecolor image
var r = img.colorAllocate(255, 0, 0); // allocate two colors
var b = img.colorAllocate(0, 0, 255);
img.rectangle(40, 40, 50, 50, r); // draw a rectangle
img.filledEllipse(80, 100, 30, 50, b); // fill a ellipse
var data = img.getData(gd.PNG); // save image as PNG data
In this example, we first use the create() method to create a 440x240 TRUECOLOR image, and use the colorAllocate() method to assign two colors, then use the rectangle() method to draw a rectangle, and draw a filled Ellipse, and finally use the getData() method to save the image as binary data in PNG format.
static function
create
Create a new image
1
2
3static Image gd.create(Integer width,
Integer height,
Integer color = gd.TRUECOLOR) async;
Call parameters:
- width: Integer, specify image width
- height: Integer, specify the image height
- color: Integer, specifies the image type, allowed values aregd.TRUECOLORorgd.PALETTE
Return results:
- Image, returns the successfully created image object
load
Decode image from format data
1static Image gd.load(Buffer data) async;
Call parameters:
- data:Buffer, given the decoded image data
Return results:
- Image, returns the successfully decoded image object
Decode image from stream object
1static Image gd.load(SeekableStream stm) async;
Call parameters:
- stm:SeekableStream, the stream object where the given image data is located
Return results:
- Image, returns the successfully decoded image object
Decode an image from the specified file
1static Image gd.load(String fname) async;
Call parameters:
- fname: String, specify the file name
Return results:
- Image, returns the successfully decoded image object
rgb
Generate combined colors from rgb color components
1
2
3static Integer gd.rgb(Integer red,
Integer green,
Integer blue);
Call parameters:
- red: Integer, red component, range is 0-255
- green: Integer, green component, range is 0-255
- blue: Integer, blue component, range is 0-255
Return results:
- Integer, returns the combined color
rgba
Generate combined colors from rgba color components
1
2
3
4static Integer gd.rgba(Integer red,
Integer green,
Integer blue,
Number alpha);
Call parameters:
- red: Integer, red component, range is 0-255
- green: Integer, green component, range is 0-255
- blue: Integer, blue component, range is 0-255
- alpha: Number, transparent component, range is 0.0-1.0
Return results:
- Integer, returns the combined color
hsl
Generate combined colors from hsl color components
1
2
3static Integer gd.hsl(Number hue,
Number saturation,
Number lightness);
Call parameters:
- hue: Number, hue component, range is 0-360
- saturation: Number, saturation component, range is 0.0-1.0
- lightness: Number, brightness component, range is 0.0-1.0
Return results:
- Integer, returns the combined color
hsla
Generate combined colors from hsla color components
1
2
3
4static Integer gd.hsla(Number hue,
Number saturation,
Number lightness,
Number alpha);
Call parameters:
- hue: Number, hue component, range is 0-360
- saturation: Number, saturation component, range is 0.0-1.0
- lightness: Number, brightness component, range is 0.0-1.0
- alpha: Number, transparent component, range is 0.0-1.0
Return results:
- Integer, returns the combined color
hsb
Generate combined colors from hsb color components
1
2
3static Integer gd.hsb(Number hue,
Number saturation,
Number brightness);
Call parameters:
- hue: Number, hue component, range is 0-360
- saturation: Number, saturation component, range is 0.0-1.0
- brightness: Number, brightness component, range is 0.0-1.0
Return results:
- Integer, returns the combined color
hsba
Generate combined colors from hsba color components
1
2
3
4static Integer gd.hsba(Number hue,
Number saturation,
Number brightness,
Number alpha);
Call parameters:
- hue: Number, hue component, range is 0-360
- saturation: Number, saturation component, range is 0.0-1.0
- brightness: Number, brightness component, range is 0.0-1.0
- alpha: Number, transparent component, range is 0.0-1.0
Return results:
- Integer, returns the combined color
color
Generate combined colors from string
1static Integer gd.color(String color);
Call parameters:
- color: String, a string specifying the color, such as: "#ff0000", "ff0000", "#f00", "f00"
Return results:
- Integer, returns the combined color
constant
NONE
Image format constant, indicating that the current image source is unknown
1const gd.NONE = 0;
JPEG
Image format constant, indicating that the current image source is jpeg format data
1const gd.JPEG = 1;
GIF
Image format constant, indicating that the current image source is gif format data
1const gd.GIF = 2;
PNG
Image format constant, indicating that the current image source is png format data
1const gd.PNG = 3;
TIFF
Image format constant, indicating that the current image source is tiff format data
1const gd.TIFF = 4;
BMP
Image format constant, indicating that the current image source is bmp format data
1const gd.BMP = 5;
WEBP
Image format constant, indicating that the current image source is webp format data
1const gd.WEBP = 6;
TRUECOLOR
Image type constant, indicating that the current image is a true color image
1const gd.TRUECOLOR = 0;
PALETTE
Image type constant, indicating that the current image is a palette image
1const gd.PALETTE = 1;
ARC
Sector drawing style, draws an arc connecting the start and end points
1const gd.ARC = 0;
CHORD
Sector drawing style, draw a straight line connecting the origin, start and end points
1const gd.CHORD = 1;
NOFILL
Sector drawing style, draws a sector without filling
1const gd.NOFILL = 2;
EDGED
Sector drawing style, draw an arc connecting the starting point and the end point and a straight line connecting the origin
1const gd.EDGED = 4;
HORIZONTAL
Mirroring direction, horizontal mirroring processing
1const gd.HORIZONTAL = 1;
VERTICAL
Mirror direction, vertical mirror processing
1const gd.VERTICAL = 2;
BOTH
Mirroring direction, both horizontal and vertical mirroring processing
1const gd.BOTH = 3;
LEFT
Rotation direction, rotate left
1const gd.LEFT = 1;
RIGHT
Rotation direction, rotate right
1const gd.RIGHT = 2;
MEAN_REMOVAL
Filter type: Use average removal method to achieve contour effect
1const gd.MEAN_REMOVAL = 0;
EDGEDETECT
Filter type: Use edge detection to highlight the edges of the image
1const gd.EDGEDETECT = 1;
EMBOSS
Filter type: Emboss image
1const gd.EMBOSS = 2;
SELECTIVE_BLUR
Filter Type: Blur Image
1const gd.SELECTIVE_BLUR = 3;
GAUSSIAN_BLUR
Filter type: Blur image with Gaussian algorithm
1const gd.GAUSSIAN_BLUR = 4;
NEGATE
Filter type: Invert all colors in the image
1const gd.NEGATE = 5;
GRAYSCALE
Filter type: Convert image to grayscale
1const gd.GRAYSCALE = 6;
SMOOTH
Filter type: Make the image smoother, use arg1 to set the smoothness level
1const gd.SMOOTH = 7;
BRIGHTNESS
Filter type: Change the brightness of the image, use arg1 to set the brightness level, the value range is -255~255
1const gd.BRIGHTNESS = 8;
CONTRAST
Filter type: Change the contrast of the image, use arg1 to set the contrast level, the value range is 0~100
1const gd.CONTRAST = 9;
COLORIZE
Filter type: Change the hue of the image. Use arg1, arg2, and arg3 to specify red, blue, and green respectively. The range of each color is 0~255. arg4 is transparency, and the value returned is 0~127.
1const gd.COLORIZE = 10;