function images(obj) { var constructor; if (obj instanceof Buffer) { constructor = images.loadFromBuffer; } else if (obj instanceof WrappedImage) { constructor = images.copyFromImage; } else if (typeof(obj) == "string") { constructor = images.loadFromFile; } else { constructor = images.createImage; } return constructor.apply(images, slice.call(arguments, 0)); }
n/a
function WrappedImage(width, height) { if (!(this instanceof WrappedImage)) return new WrappedImage(width, height); if (gcThreshold && nextGCThreshold) { if (images.getUsedMemory() > nextGCThreshold) { images.gc(); nextGCThreshold = images.getUsedMemory() + gcThreshold; } } this._handle = new _Image(width, height); }
n/a
copyFromImage = function (src, x, y, width, height) { return WrappedImage().copyFromImage(src, x, y, width, height); }
...
loadFromBuffer: function(buffer, start, end) {
this._handle.loadFromBuffer(buffer, start, end);
},
copyFromImage: function(img, x, y, width, height) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.copyFromImage(img, x, y, width, height);
},
fill: function(red, green, blue, alpha) {
this._handle.fillColor(red, green, blue, alpha);
},
draw: function(img, x, y) {
if (img instanceof WrappedImage) {
img = img._handle;
...
createImage = function (width, height) { return WrappedImage(width, height); }
n/a
gc = function () { return _images.gc(); }
...
Set the garbage collection threshold
设置图像处理库自动gc的阈值(当*新增*内存使用超过该阈值时,执行垃圾回收)
### images.getUsedMemory()
Get used memory (in bytes)
得到图像处理库占用的内存大小(单位为字节)
### images.gc()
Forced call garbage collection
强制调用V8的垃圾回收机制
...
getUsedMemory = function () { return _images.usedMemory; }
...
Set the limit size of each image
设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)
### images.setGCThreshold(value)
Set the garbage collection threshold
设置图像处理库自动gc的阈值(当*新增*内存使用超过该阈值时,执行垃圾回收)
### images.getUsedMemory()
Get used memory (in bytes)
得到图像处理库占用的内存大小(单位为字节)
### images.gc()
Forced call garbage collection
强制调用V8的垃圾回收机制
...
loadFromBuffer = function (buffer, start, end) { return WrappedImage().loadFromBuffer(buffer, start, end); }
...
}
}
this._handle = new _Image(width, height);
}
prototype = {
loadFromBuffer: function(buffer, start, end) {
this._handle.loadFromBuffer(buffer, start, end);
},
copyFromImage: function(img, x, y, width, height) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.copyFromImage(img, x, y, width, height);
},
...
loadFromFile = function (file) { return images.loadFromBuffer(fs.readFileSync(file)); }
n/a
setGCThreshold = function (value) { gcThreshold = value; nextGCThreshold = value; }
...
Get height for the image or set height of the image
获取或设置图像高度
### images.setLimit(width, height)
Set the limit size of each image
设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)
### images.setGCThreshold(value)
Set the garbage collection threshold
设置图像处理库自动gc的阈值(当*新增*内存使用超过该阈值时,执行垃圾回收)
### images.getUsedMemory()
Get used memory (in bytes)
得到图像处理库占用的内存大小(单位为字节)
...
setLimit = function (maxWidth, maxHeight) { _images.maxHeight = maxHeight; _images.maxWidth = maxWidth; return images; }
...
Get width for the image or set width of the image
获取或设置图像宽度
### .height([height])
Get height for the image or set height of the image
获取或设置图像高度
### images.setLimit(width, height)
Set the limit size of each image
设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)
### images.setGCThreshold(value)
Set the garbage collection threshold
设置图像处理库自动gc的阈值(当*新增*内存使用超过该阈值时,执行垃圾回收)
...
function WrappedImage(width, height) { if (!(this instanceof WrappedImage)) return new WrappedImage(width, height); if (gcThreshold && nextGCThreshold) { if (images.getUsedMemory() > nextGCThreshold) { images.gc(); nextGCThreshold = images.getUsedMemory() + gcThreshold; } } this._handle = new _Image(width, height); }
n/a
copyFromImage = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
loadFromBuffer: function(buffer, start, end) {
this._handle.loadFromBuffer(buffer, start, end);
},
copyFromImage: function(img, x, y, width, height) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.copyFromImage(img, x, y, width, height);
},
fill: function(red, green, blue, alpha) {
this._handle.fillColor(red, green, blue, alpha);
},
draw: function(img, x, y) {
if (img instanceof WrappedImage) {
img = img._handle;
...
draw = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
``` javascript
var images = require("images");
images("input.jpg") //Load image from file
//加载图像文件
.size(400) //Geometric scaling the image to 400 pixels width
//等比缩放图像到400像素宽
.draw(images("logo.png"), 10, 10) //Drawn logo at coordinates (10,10)
//在(10,10)处绘制Logo
.save("output.jpg", { //Save the image to a file,whih quality 50
quality : 50 //保存图片到文件,图片质量为50
});
```
## Features 功能特性
...
drawImage = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
fill: function(red, green, blue, alpha) {
this._handle.fillColor(red, green, blue, alpha);
},
draw: function(img, x, y) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.drawImage(img, x, y);
},
encode: function(type, config) {
var configurator;
if (typeof(type) != "number") {
type = String(type).toLowerCase();
type = (FILE_TYPE_MAP["." + type] || FILE_TYPE_MAP[type]);
}
...
encode = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
Fill image with color
以指定颜色填充图像
### .draw(image, x, y)
Draw *image* on the current image position( *x* , *y* )
在当前图像( *x* , *y* )上绘制 *image* 图像
### .encode(type[, config])
eg:`images("input.png").encode("jpg", {operation:50})`
Encode image to buffer, *config* is image setting.
以指定格式编码当前图像到Buffer,config为图片设置,目前支持设置JPG图像质量
Return buffer
返回填充好的Buffer
**Note:The operation will cut off the chain**
**注意:该操作将会切断调用链**
...
fill = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
Load and decode image from a buffer
从Buffer数据中解码图像
### images(image[, x, y, width, height])
Copy from another image
从另一个图像中复制区域来创建图像
### .fill(red, green, blue[, alpha])
eg:`images(200, 100).fill(0xff, 0x00, 0x00, 0.5)`
Fill image with color
以指定颜色填充图像
### .draw(image, x, y)
Draw *image* on the current image position( *x* , *y* )
在当前图像( *x* , *y* )上绘制 *image* 图像
...
fillColor = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
copyFromImage: function(img, x, y, width, height) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.copyFromImage(img, x, y, width, height);
},
fill: function(red, green, blue, alpha) {
this._handle.fillColor(red, green, blue, alpha);
},
draw: function(img, x, y) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.drawImage(img, x, y);
},
...
height = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
Set the size of the image,if the height is not specified, then scaling based on the current width and height
设置图像宽高,如果height未指定,则根据当前宽高等比缩放, 默认采用 bicubic 算法。
### .width([width])
Get width for the image or set width of the image
获取或设置图像宽度
### .height([height])
Get height for the image or set height of the image
获取或设置图像高度
### images.setLimit(width, height)
Set the limit size of each image
设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)
...
loadFromBuffer = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
}
}
this._handle = new _Image(width, height);
}
prototype = {
loadFromBuffer: function(buffer, start, end) {
this._handle.loadFromBuffer(buffer, start, end);
},
copyFromImage: function(img, x, y, width, height) {
if (img instanceof WrappedImage) {
img = img._handle;
}
this._handle.copyFromImage(img, x, y, width, height);
},
...
resize = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
Encoding and save the current image to a *file*, if the *type* is not specified, *type* well be automatically determined according
to the *file*, *config* is image setting. eg: `{ operation:50 }`
编码并保存当前图像到 *file* ,如果type未指定,则根据 *file* 自动判断文件类型,config为图片设置,目前支持设置JPG图像质量
### .size([width[, height]])
Get size of the image or set the size of the image,if the height is not specified, then scaling based on the current width and height
获取或者设置图像宽高,如果height未指定,则根据当前宽高等比缩放
### .resize(width[, height])
Set the size of the image,if the height is not specified, then scaling based on the current width and height
设置图像宽高,如果height未指定,则根据当前宽高等比缩放, 默认采用 bicubic 算法。
### .width([width])
Get width for the image or set width of the image
获取或设置图像宽度
...
save = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
images("input.jpg") //Load image from file
//加载图像文件
.size(400) //Geometric scaling the image to 400 pixels width
//等比缩放图像到400像素宽
.draw(images("logo.png"), 10, 10) //Drawn logo at coordinates (10,10)
//在(10,10)处绘制Logo
.save("output.jpg", { //Save the image to a file,whih quality
50
quality : 50 //保存图片到文件,图片质量为50
});
```
## Features 功能特性
* Lightweight:no need to install any image processing library.
* 轻量级:无需安装任何图像处理库。
...
saveAsync = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
n/a
size = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
Node.js轻量级跨平台图像编解码库
``` javascript
var images = require("images");
images("input.jpg") //Load image from file
//加载图像文件
.size(400) //Geometric scaling the image to 400 pixels width
//等比缩放图像到400像素宽
.draw(images("logo.png"), 10, 10) //Drawn logo at coordinates (10,10)
//在(10,10)处绘制Logo
.save("output.jpg", { //Save the image to a file,whih quality 50
quality : 50 //保存图片到文件,图片质量为50
});
```
...
toBuffer = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
type = String(type).toLowerCase();
type = (FILE_TYPE_MAP["." + type] || FILE_TYPE_MAP[type]);
}
if (config != undefined) {
configurator = CONFIG_GENERATOR[type];
config = configurator && configurator(config);
}
return this._handle.toBuffer(type, config);
},
save: function(file, type, config) {
if (type && typeof(type) == "object") {
config = type;
type = undefined;
}
fs.writeFileSync(file, this.encode(type || path.extname(file), config));
...
width = function () { var ret = fn.apply(this, slice.call(arguments, 0)); return ret === undefined ? this : ret; }
...
Get size of the image or set the size of the image,if the height is not specified, then scaling based on the current width and height
获取或者设置图像宽高,如果height未指定,则根据当前宽高等比缩放
### .resize(width[, height])
Set the size of the image,if the height is not specified, then scaling based on the current width and height
设置图像宽高,如果height未指定,则根据当前宽高等比缩放, 默认采用 bicubic 算法。
### .width([width])
Get width for the image or set width of the image
获取或设置图像宽度
### .height([height])
Get height for the image or set height of the image
获取或设置图像高度
...
function Image() { [native code] }
n/a
function gc() { [native code] }
...
Set the garbage collection threshold
设置图像处理库自动gc的阈值(当*新增*内存使用超过该阈值时,执行垃圾回收)
### images.getUsedMemory()
Get used memory (in bytes)
得到图像处理库占用的内存大小(单位为字节)
### images.gc()
Forced call garbage collection
强制调用V8的垃圾回收机制
...