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获取本机内网和外网ip地址的实现代码
Jun 01 NodeJs
我的NodeJs学习小结(一)
Jul 06 NodeJs
NodeJS学习笔记之FS文件模块
Jan 13 NodeJs
浅析nodejs实现Websocket的数据接收与发送
Nov 19 NodeJs
NodeJS的Promise的用法解析
May 05 NodeJs
初识NodeJS服务端开发入门(Express+MySQL)
Apr 07 NodeJs
浅析 NodeJs 的几种文件路径
Jun 07 NodeJs
详解Nodejs 通过 fs.createWriteStream 保存文件
Oct 10 NodeJs
NodeJS简单实现WebSocket功能示例
Feb 10 NodeJs
nodejs实现范围请求的实现代码
Oct 12 NodeJs
nodejs实现日志读取、日志查找及日志刷新的方法分析
May 20 NodeJs
搭建一个nodejs脚手架的方法步骤
Jun 28 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设计模式 Delegation(委托模式)
2011/06/26 PHP
PHP调用VC编写的COM组件实例
2014/03/29 PHP
排序算法之PHP版快速排序、冒泡排序
2014/04/09 PHP
php版微信公众号接口实现发红包的方法
2016/10/14 PHP
PHP实现针对日期,月数,天数,周数,小时,分,秒等的加减运算示例【基于strtotime】
2017/04/19 PHP
两个SUBMIT按钮,如何区分处理
2006/08/22 Javascript
js 阻止子元素响应父元素的onmouseout事件具体实现
2013/12/23 Javascript
15个jquery常用方法、小技巧分享
2015/01/13 Javascript
JS实现网页背景颜色与select框中颜色同时变化的方法
2015/02/27 Javascript
Backbone.js的一些使用技巧
2015/07/01 Javascript
angularjs创建弹出框实现拖动效果
2020/08/25 Javascript
jQuery简单操作cookie的插件实例
2016/01/13 Javascript
使用jQuery实现Web页面换肤功能的要点解析
2016/05/12 Javascript
微信小程序 教程之WXSS
2016/10/18 Javascript
Vue.js数据绑定之data属性
2017/07/07 Javascript
bootstrap多层模态框滚动条消失的问题
2017/07/21 Javascript
老生常谈JavaScript面向对象基础与this指向问题
2017/10/16 Javascript
深入研究React中setState源码
2017/11/17 Javascript
JavaScript设计模式之建造者模式实例教程
2018/07/02 Javascript
vue父子组件的通信方法(实例详解)
2019/11/10 Javascript
使用vuex存储用户信息到localStorage的实例
2019/11/11 Javascript
前端vue如何使用高德地图
2020/11/05 Javascript
基于JavaScript实现轮播图效果
2021/01/02 Javascript
[56:24]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#1Liquid VS MVP.Phx第二局
2016/03/04 DOTA
python3实现全角和半角字符转换的方法示例
2017/09/21 Python
Python学习之Django的管理界面代码示例
2018/02/10 Python
Python装饰器知识点补充
2018/05/28 Python
利用python生成照片墙的示例代码
2020/04/09 Python
环境科学专业个人求职信
2013/09/26 职场文书
通用自荐信范文
2014/03/14 职场文书
工会趣味活动方案
2014/08/18 职场文书
铁路安全反思材料
2014/12/24 职场文书
社区三八妇女节活动总结
2015/02/06 职场文书
小学中队长竞选稿
2015/11/20 职场文书
驾驶员安全责任协议书
2016/03/22 职场文书
OpenCV全景图像拼接的实现示例
2021/06/05 Python