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的前后端分离的思考与实践(六)Nginx + Node.js + Java 的软件栈部署实践
Sep 26 NodeJs
NodeJS学习笔记之Connect中间件模块(二)
Jan 27 NodeJs
使用nodejs开发cli项目实例
Jun 03 NodeJs
浅谈Nodejs观察者模式
Oct 13 NodeJs
Nodejs Stream 数据流使用手册
Apr 17 NodeJs
NodeJS连接MongoDB数据库时报错的快速解决方法
May 13 NodeJs
nodejs学习笔记之路由
Mar 27 NodeJs
nodejs 图解express+supervisor+ejs的用法(推荐)
Sep 08 NodeJs
nodejs连接mysql数据库及基本知识点详解
Mar 20 NodeJs
解决nodejs的npm命令无反应的问题
May 17 NodeJs
详解NodeJs开发微信公众号
May 25 NodeJs
Nodejs对postgresql基本操作的封装方法
Feb 20 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/12/06 PHP
适用于php-5.2 的 php.ini 中文版[金步国翻译]
2011/04/17 PHP
PHP url 加密解密函数代码
2011/08/26 PHP
php猜单词游戏
2015/09/29 PHP
php四种定界符详解
2017/02/16 PHP
PHP PDOStatement::bindColumn讲解
2019/01/30 PHP
禁止F5等快捷键的JS代码
2007/03/06 Javascript
js获取下拉列表框中的value和text的值示例代码
2014/01/11 Javascript
javascript制作的网页侧边弹出框思路及实现代码
2014/05/21 Javascript
用js一次改变多个input的readonly属性值的方法
2014/06/11 Javascript
JS+DIV+CSS实现的经典标签切换效果代码
2015/09/14 Javascript
BOM系列第三篇之定时器应用(时钟、倒计时、秒表和闹钟)
2016/08/17 Javascript
jquery二级目录选中当前页的css样式
2016/12/08 Javascript
Swiper 4.x 使用方法(移动端网站的内容触摸滑动)
2018/05/17 Javascript
基于vue展开收起动画的示例代码
2018/07/05 Javascript
vue 实现axios拦截、页面跳转和token 验证
2018/07/17 Javascript
vue devtools的安装与使用教程
2018/08/08 Javascript
解决vue axios的封装 请求状态的错误提示问题
2018/09/25 Javascript
JS实现的小火箭发射动画效果示例
2018/12/08 Javascript
Vue 数组和对象更新,但是页面没有刷新的解决方式
2019/11/09 Javascript
Vue 图片压缩并上传至服务器功能
2020/01/15 Javascript
ES6 async、await的基本使用方法示例
2020/06/06 Javascript
JS实现canvas简单小画板功能
2020/06/23 Javascript
基于jquery实现彩色投票进度条代码解析
2020/08/26 jQuery
python sqlobject(mysql)中文乱码解决方法
2008/11/14 Python
Python中的闭包实例详解
2014/08/29 Python
Python文件与文件夹常见基本操作总结
2016/09/19 Python
Python Flask框架扩展操作示例
2019/05/03 Python
pyqt弹出新对话框,以及关闭对话框获取数据的实例
2019/06/18 Python
python并发编程多进程 互斥锁原理解析
2019/08/20 Python
python实现计算器功能
2019/10/31 Python
Python中格式化字符串的四种实现
2020/05/26 Python
中学老师的自我评价
2013/11/07 职场文书
科学发展观演讲稿
2014/09/11 职场文书
教师国庆节演讲稿范文2014
2014/09/21 职场文书
Python如何利用正则表达式爬取网页信息及图片
2021/04/17 Python