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的前后端分离的思考与实践(四)安全问题解决方案
Sep 26 NodeJs
nodejs教程之入门
Nov 21 NodeJs
轻松创建nodejs服务器(9):实现非阻塞操作
Dec 18 NodeJs
nodejs中使用多线程编程的方法实例
Mar 24 NodeJs
nodejs利用http模块实现银行卡所属银行查询和骚扰电话验证示例
Dec 30 NodeJs
详解nodejs操作mongodb数据库封装DB类
Apr 10 NodeJs
详解nodejs异步I/O和事件循环
Jun 07 NodeJs
Nodejs实现多房间简易聊天室功能
Jun 20 NodeJs
修改Nodejs内置的npm默认配置路径方法
May 13 NodeJs
nodejs 十六进制字符串型数据与btye型数据相互转换
Jul 30 NodeJs
nodejs require js文件入口,在package.json中指定默认入口main方法
Oct 10 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
Chrome Web App开发小结
2014/09/04 PHP
PHP中基本HTTP认证技巧分析
2015/03/16 PHP
读jQuery之八 包装事件对象
2011/06/21 Javascript
Javascript算符的优先级介绍
2013/03/20 Javascript
JavaScript排序算法之希尔排序的2个实例
2014/04/04 Javascript
jquery淡化版banner异步图片文字效果切换图片特效
2014/04/08 Javascript
jQuery实现表格行上移下移和置顶的方法
2015/05/22 Javascript
jquery实现隐藏在左侧的弹性弹出菜单效果
2015/09/18 Javascript
jQuery四种选择器使用及示例
2016/06/05 Javascript
浅谈jQuery双事件多重加载的问题
2016/10/05 Javascript
vue项目中使用百度地图的方法
2018/06/08 Javascript
extract-text-webpack-plugin用法详解
2019/02/14 Javascript
jquery登录的异步验证操作示例
2019/05/09 jQuery
express中static中间件的具体使用方法
2019/10/17 Javascript
Vue实现 点击显示再点击隐藏效果(点击页面空白区域也隐藏效果)
2020/01/16 Javascript
Jquery cookie插件实现原理代码解析
2020/08/04 jQuery
vant 自定义 van-dropdown-item的用法
2020/08/05 Javascript
在vue中使用vant TreeSelect分类选择组件操作
2020/11/02 Javascript
[36:13]Mineski vs iG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[08:53]DOTA2-DPC中国联赛 正赛 PSG.LGD vs LBZS 选手采访
2021/03/11 DOTA
python list 合并连接字符串的方法
2013/03/09 Python
Python基于回溯法子集树模板解决最佳作业调度问题示例
2017/09/08 Python
python删除文本中行数标签的方法
2018/05/31 Python
Python 类的特殊成员解析
2018/06/20 Python
python selenium自动上传有赞单号的操作方法
2018/07/05 Python
Python3爬虫之urllib携带cookie爬取网页的方法
2018/12/28 Python
Python实现对特定列表进行从小到大排序操作示例
2019/02/11 Python
python和php哪个更适合写爬虫
2020/06/22 Python
乐高西班牙官方商店:LEGO Shop ES
2019/12/01 全球购物
澳大利亚窗帘商店:Curtain Wonderland
2019/12/01 全球购物
最新远光软件笔试题面试题内容
2013/11/08 面试题
单位门卫岗位职责
2013/12/20 职场文书
教师个人考察材料
2014/12/16 职场文书
管理失职检讨书
2015/05/05 职场文书
2015年建筑工程工作总结
2015/05/13 职场文书
利用Python读取微信朋友圈的多种方法总结
2021/08/23 Python