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教程 安装express及配置app.js文件的详细步骤
May 11 NodeJs
Nodejs学习item【入门手上】
May 05 NodeJs
在windows上用nodejs搭建静态文件服务器的简单方法
Aug 11 NodeJs
Nodejs中解决cluster模块的多进程如何共享数据问题
Nov 10 NodeJs
深入nodejs中流(stream)的理解
Mar 27 NodeJs
NodeJS 实现手机短信验证模块阿里大于功能
Jun 19 NodeJs
Nodejs实现多房间简易聊天室功能
Jun 20 NodeJs
使用vs code开发Nodejs程序的使用方法
Sep 21 NodeJs
Nodejs处理异常操作示例
Dec 25 NodeJs
nodejs微信开发之接入指南
Mar 17 NodeJs
nodejs微信开发之自动回复的实现
Mar 17 NodeJs
Nodejs监听日志文件的变化的过程解析
Aug 04 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来自动调用不同服务器上的flash
2006/10/09 PHP
如何突破PHP程序员的技术瓶颈分析
2011/07/17 PHP
应用开发中涉及到的css和php笔记分享
2011/08/02 PHP
php实现计数器方法小结
2015/01/05 PHP
Yii框架操作cookie与session的方法实例详解
2019/09/04 PHP
使用TextRange获取输入框中光标的位置的代码
2007/03/08 Javascript
Javascript结合css实现网页换肤功能
2009/11/02 Javascript
Jquery图片延迟加载插件jquery.lazyload.js的使用方法
2014/05/21 Javascript
js实现鼠标滑过文字链接色彩变化的效果
2015/05/06 Javascript
用JavaScript判断CSS浏览器类型前缀的两种方法
2015/10/08 Javascript
微信小程序 获取javascript 里的数据
2017/08/17 Javascript
bootstrap confirmation按钮提示组件使用详解
2017/08/22 Javascript
原生JS实现的简单小钟表功能示例
2018/08/30 Javascript
vue webpack开发访问后台接口全局配置的方法
2018/09/18 Javascript
jquery简易手风琴插件的封装
2020/10/13 jQuery
简单的编程0基础下Python入门指引
2015/04/01 Python
Python标准库defaultdict模块使用示例
2015/04/28 Python
Python编程之gui程序实现简单文件浏览器代码
2017/12/08 Python
Flask框架通过Flask_login实现用户登录功能示例
2018/07/17 Python
django使用LDAP验证的方法示例
2018/12/10 Python
python matplotlib如何给图中的点加标签
2019/11/14 Python
Python3常用内置方法代码实例
2019/11/18 Python
python 串口读取+存储+输出处理实例
2019/12/26 Python
python3实现飞机大战
2020/11/29 Python
Booking.com英国官网:全球酒店在线预订网站
2018/04/21 全球购物
Bed Bath & Beyond加拿大官网:购买床上用品、浴巾、厨房电器等
2019/10/04 全球购物
酒店副总经理岗位职责范本
2014/02/04 职场文书
学生会宣传部部长竞选演讲稿
2014/04/25 职场文书
党员应该树立反腐倡廉的坚定意识思想汇报
2014/09/12 职场文书
关于十八大的演讲稿
2014/09/15 职场文书
给客户的检讨书
2014/12/21 职场文书
小学生成绩单评语
2014/12/31 职场文书
丽江古城导游词
2015/02/03 职场文书
活动经费申请报告
2015/05/15 职场文书
Nginx已编译的nginx-添加新模块
2021/04/01 Servers
win11系统中dhcp服务异常什么意思? Win11 DHCP服务异常修复方法
2022/04/08 数码科技