nodejs处理图片的中间件node-images详解


Posted in NodeJs onMay 08, 2017

Cross-platform image decoder(png/jpeg/gif) and encoder(png/jpeg) for Node.js

node.js轻量级跨平台图像编解码库

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 功能特性

  1. Lightweight:no need to install any image processing library.
  2. 轻量级:无需安装任何图像处理库。
  3. Cross-platform: Released a compiled .node file on windows, just download and start.
  4. 跨平台:Windows下发布了编译好的.node文件,下载就能用。
  5. Easy-to-use: Provide jQuery-like chaining API.Simple and reliable!
  6. 方便用:jQuery风格的API,简单可依赖。

Installation 安装

$ npm install images

API 接口

node-images provide jQuery-like Chaining API,You can start the chain like this:

node-images 提供了类似jQuery的链式调用API,您可以这样开始:

/* Load and decode image from file */
/* 从指定文件加载并解码图像 */
images(file)

/* Create a new transparent image */
/* 创建一个指定宽高的透明图像 */
images(width, height)

/* Load and decode image from a buffer */
/* 从Buffer数据中解码图像 */
images(buffer[, start[, end]])

/* Copy from another image */
/* 从另一个图像中复制区域来创建图像 */
images(image[, x, y, width, height])

images(file)

Load and decode image from file从指定文件加载并解码图像

images(width, height)

Create a new transparent image创建一个指定宽高的透明图像

images(buffer[, start[, end]])

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 图像

.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

注意:该操作将会切断调用链

See:.save(file[, type[, config]]) 参考:.save(file[, type[, config]])

.save(file[, type[, config]])

eg:images("input.png").encode("output.jpg", {operation:50}) 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获取或设置图像宽度

.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的阈值(当新增内存使用超过该阈值时,执行垃圾回收)

images.getUsedMemory()

Get used memory (in bytes)得到图像处理库占用的内存大小(单位为字节)

images.gc()

Forced call garbage collection 强制调用V8的垃圾回收机制

https://github.com/zhangyuanwei/node-images

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

NodeJs 相关文章推荐
nodejs npm install全局安装和本地安装的区别
Jun 05 NodeJs
基于NodeJS的前后端分离的思考与实践(二)模版探索
Sep 26 NodeJs
详解Nodejs的timers模块
Dec 22 NodeJs
windows 下安装nodejs 环境变量设置
Feb 02 NodeJs
实例分析nodejs模块xml2js解析xml过程中遇到的坑
Mar 18 NodeJs
nodejs连接mysql数据库简单封装示例-mysql模块
Apr 10 NodeJs
nodejs实现解析xml字符串为对象的方法示例
Mar 14 NodeJs
nodeJS模块简单用法示例
Apr 21 NodeJs
nodejs 简单实现动态html的方法
May 12 NodeJs
NodeJS服务器实现gzip压缩的示例代码
Oct 12 NodeJs
nodejs使用Sequelize框架操作数据库的实现
Oct 21 NodeJs
分享五个Node.js开发的优秀实践 
Apr 07 NodeJs
使用nodejs爬取前程无忧前端技能排行
May 06 #NodeJs
win系统下nodejs环境安装配置
May 04 #NodeJs
Nodejs--post的公式详解
Apr 29 #NodeJs
NodeJs的fs读写删除移动监听
Apr 28 #NodeJs
NodeJs安装npm包一直失败的解决方法
Apr 28 #NodeJs
NodeJs模拟登陆正方教务
Apr 28 #NodeJs
用Nodejs搭建服务器访问html、css、JS等静态资源文件
Apr 28 #NodeJs
You might like
非常好的php目录导航文件代码
2006/10/09 PHP
坏狼的PHP学习教程之第2天
2008/06/15 PHP
php模拟post行为代码总结(POST方式不是绝对安全)
2012/02/22 PHP
php字符编码转换之gb2312转为utf8
2013/10/28 PHP
php编写的简单页面跳转功能实现代码
2013/11/27 PHP
php+mysql实现简单的增删改查功能
2015/07/13 PHP
WordPress主题制作中自定义头部的相关PHP函数解析
2016/01/08 PHP
composer.lock文件的作用
2016/02/03 PHP
Zend Framework教程之Zend_Config_Ini用法分析
2016/03/23 PHP
jQuery代码优化 事件委托篇
2011/11/01 Javascript
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
jquery图片切换实例分析
2015/04/15 Javascript
javascript实现俄罗斯方块游戏的思路和方法
2015/04/27 Javascript
js实现非常简单的焦点图切换特效实例
2015/05/07 Javascript
html+javascript+bootstrap实现层级多选框全层全选和多选功能
2017/03/09 Javascript
jquery 手势密码插件
2017/03/17 Javascript
前端跨域的几种解决方式总结(推荐)
2017/08/16 Javascript
vue组件中使用iframe元素的示例代码
2017/12/13 Javascript
深入浅析angular和vue还有jquery的区别
2018/08/13 jQuery
vue router 跳转后回到顶部的实例
2018/08/31 Javascript
js实现移动端tab切换时下划线滑动效果
2019/09/08 Javascript
uni-app从安装到卸载的入门教程
2020/05/15 Javascript
jquery实现抽奖功能
2020/10/22 jQuery
Python序列循环移位的3种方法推荐
2018/04/09 Python
python实现视频读取和转化图片
2019/12/10 Python
利用django model save方法对未更改的字段依然进行了保存
2020/03/28 Python
Python openpyxl 插入折线图实例
2020/04/17 Python
C语言中break与continue的区别
2012/07/12 面试题
公司办公室岗位职责
2014/03/19 职场文书
初中毕业典礼演讲稿
2014/09/09 职场文书
党员年终个人总结
2015/02/14 职场文书
2015年小学生自我评价范文
2015/03/03 职场文书
幼儿园小班开学寄语
2015/05/27 职场文书
新年祝酒词大全
2015/08/11 职场文书
2016年秋季趣味运动会开幕词
2016/03/04 职场文书
MySQL 数据类型详情
2021/11/11 MySQL