var images = require("images");
images("input.jpg") //加载图像文件
.size(400) //等比缩放图像到400像素宽
.draw(images("logo.png"), 10, 10) //在(10,10)处绘制Logo
.save("output.jpg", { //保存图片到文件,图片质量为50
quality : 50
});
Features 功能特性
- 轻量级:无需安装任何图像处理库。
- 跨平台:Windows下发布了编译好的.node文件,下载就能用。
- 方便用:jQuery风格的API,简单可依赖。
Installation 安装
`$ npm install images`
API 接口
node-images 提供了类似jQuery的链式调用API,您可以这样开始:
/* 从指定文件加载并解码图像 */
images(file)
/* 创建一个指定宽高的透明图像 */
images(width, height)
/* 从Buffer数据中解码图像 */
images(buffer[, start[, end]])
/* 从另一个图像中复制区域来创建图像 */
images(image[, x, y, width, height])
.fill(red, green, blue[, alpha])
以指定颜色填充图像
eg:images(200, 100).fill(0xff, 0x00, 0x00, 0.5)
.draw(image, x, y)
在当前图像( x , y )上绘制 image 图像
.encode(type[, config])
以指定格式编码当前图像到Buffer,config为图片设置,目前支持设置JPG图像质量
eg:images("input.png").encode("jpg", {operation:50})
返回填充好的Buffer
注意:该操作将会切断调用链
参考:.save(file[, type[, config]])
.save(file[, type[, config]])
编码并保存当前图像到 file ,如果type未指定,则根据 file 自动判断文件类型,config为图片设置,目前支持设置JPG图像质量
eg:images("input.png").save("output.jpg", {operation:50})
.size([width[, height]])
获取或者设置图像宽高,如果height未指定,则根据当前宽高等比缩放
.width([width])
获取或设置图像宽度
.height([height])
获取或设置图像高度
images.setLimit(width, height)
设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)。
win7 64 位安装失败… Microsoft Windows [版本 6.1.7601] 版权所有 © 2009 Microsoft Corporation。保留所有权利。
[email protected] install D:\imagestest\node_modules\images node install.js
events.js:72
throw er; // Unhandled ‘error’ event
^
Error: spawn ENOENT
at errnoException (child_process.js:975:11)
at Process.ChildProcess._handle.onexit (child_process.js:766:34)
npm ERR! [email protected] install: node install.js
npm ERR! cmd "/c" "node install.js"
failed with 8
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the images package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node install.js
npm ERR! You can get their info via:
npm ERR! npm owner ls images
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7601 npm ERR! command “C:\Program Files\nodejs\\node.exe” “C:\Program Files\nod ejs\node_modules\npm\bin\npm-cli.js” “install” “images” npm ERR! cwd D:\imagestest npm ERR! node -v v0.10.4 npm ERR! npm -v 1.2.18 npm ERR! code ELIFECYCLE npm ERR! npm ERR! Additional logging details can be found in: npm ERR! D:\imagestest\npm-debug.log npm ERR! not ok code 0
D:\imagestest>
在ubuntu, macosx上安装出错。缺啥支持包?
$ npm install images
npm http GET https://registry.npmjs.org/images
npm http 200 https://registry.npmjs.org/images
npm http GET https://registry.npmjs.org/images/-/images-1.1.3.tgz
npm http 200 https://registry.npmjs.org/images/-/images-1.1.3.tgz
> [email protected] install /Users/yliu0/dev/node_modules/images
> node install.js
CXX(target) Release/obj.target/images/src/Image.o
CXX(target) Release/obj.target/images/src/Png.o
../src/Png.cc:10:10: fatal error: 'png.h' file not found
#include <png.h>
^
1 error generated.
make: *** [Release/obj.target/images/src/Png.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
cenntos5.9报错
[email protected] install /opt/node_modules/images node install.js
gyp http GET http://nodejs.org/dist/v0.10.7/node-v0.10.7.tar.gz
gyp http 200 http://nodejs.org/dist/v0.10.7/node-v0.10.7.tar.gz
make: Entering directory /opt/node_modules/images/build' CXX(target) Release/obj.target/images/src/Image.o CXX(target) Release/obj.target/images/src/Png.o ../src/Png.cc:10:17: 警告:png.h:没有那个文件或目录 ../src/Png.cc:23: 错误:变量或字段 ‘read_from_memory’ 声明为 void ../src/Png.cc:23: 错误:‘png_structp’ 在此作用域中尚未声明 ../src/Png.cc:23: 错误:‘png_bytep’ 在此作用域中尚未声明 ../src/Png.cc:23: 错误:‘png_size_t’ 在此作用域中尚未声明 ../src/Png.cc:23: 错误:initializer 表达式列表被看作复合表达式 ../src/Png.cc:23: 错误:expected ‘,’ or ‘;’ before ‘{’ token make: *** [Release/obj.target/images/src/Png.o] 错误 1 make: Leaving directory
/opt/node_modules/images/build’
gyp ERR! build error
gyp ERR! stack Error: make
failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Linux 2.6.18-348.6.1.el5
gyp ERR! command “node” “/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js” “rebuild”
gyp ERR! cwd /opt/node_modules/images
gyp ERR! node -v v0.10.7
gyp ERR! node-gyp -v v0.9.5
gyp ERR! not ok
npm ERR! weird error 1
npm ERR! not ok code 0
非win32平台下依赖 giflib,libjpeg-turbo,libpng 这些库。
../src/Png.cc:10:17: 警告:png.h:没有那个文件或目录
看样子是缺少头文件,可能需要安装相应的 dev 版本
嗯,支持
var from = images("input.jpg");
images(from, 0, 0, 200, 100) // 从input.jpg复制200x100的矩形区域
.size(50) //等比缩放图像到50像素宽
.save("output.jpg",{ //保存到output.jpg,图片质量为50
quality : 50
});
嗯,是跨平台的, win32平台下直接发布了.node库文件无需编译,可以直接使用。 其他平台下需要从源码编译。依赖 giflib,libjpeg-turbo,libpng 这几个库,报错大都是因为环境配置问题。
npm install images npm http GET https://registry.npmjs.org/images npm http 304 https://registry.npmjs.org/images
[email protected] install /root/test/node_modules/images node install.js
make: Entering directory /root/test/node_modules/images/build' CXX(target) Release/obj.target/images/src/Image.o CXX(target) Release/obj.target/images/src/Png.o ../src/Png.cc: In function ‘ImageState decodePng(PixelArray*, ImageData*)’: ../src/Png.cc:94: 错误:‘png_const_bytep’在此作用域中尚未声明 make: *** [Release/obj.target/images/src/Png.o] 错误 1 make: Leaving directory
/root/test/node_modules/images/build’
gyp ERR! build error
gyp ERR! stack Error: make
failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/node/0.10.21/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Linux 2.6.32-358.6.2.el6.x86_64
gyp ERR! command “node” “/usr/local/node/0.10.21/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js” “rebuild”
gyp ERR! cwd /root/test/node_modules/images
gyp ERR! node -v v0.10.21
gyp ERR! node-gyp -v v0.10.10
gyp ERR! not ok
npm ERR! weird error 1
npm ERR! not ok code 0
请问Linux下如何编译?
F:\nodejs\letu>npm install npm http GET https://registry.npmjs.org/images npm http 304 https://registry.npmjs.org/images
[email protected] install F:\nodejs\letu\node_modules\images node install.js
events.js:72 throw er; // Unhandled ‘error’ event ^ Error: spawn ENOENT at errnoException (child_process.js:980:11) at Process.ChildProcess._handle.onexit (child_process.js:771:34) npm ERR! weird error 8 npm ERR! not ok code 0
windows平台安装报错,现在这个包还在维护吗?
E:\story>npm install images
npm http GET https://registry.npmjs.org/images
npm http 304 https://registry.npmjs.org/images
[email protected] install E:\story\node_modules\images
node install.js
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
npm ERR! [email protected] install: `node install.js
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the images package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node install.js
npm ERR! You can get their info via:
npm ERR! npm owner ls images
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7601
npm ERR! command “C:\Program Files\nodejs\\node.exe” “C:\Program Files\nod ejs\node_modules\npm\bin\npm-cli.js” “install” “images”
npm ERR! cwd E:\story
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! E:\story\npm-debug.log
npm ERR! not ok code 0
@wanglihui 不是基于 promise 的,这个库的所有方法基本都是同步的。如果要实现异步读取和保存文件,可以这样
//异步读取
fs.readFile("a.jpg", function(data){
var img = image(data);
// img.XXXX()
var saveData = img.encode("jpg", {operation:50});
//异步写入
fs.writeFile("b.jpg", saveData, function(){
//写入完成的回调
});
});
@stepjacky 在啊。 49 downloads in the last day 370 downloads in the last week 1,110 downloads in the last month