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打包工具整合到鼠标右键的方法
May 11 NodeJs
NodeJS使用jQuery选择器操作DOM
Feb 13 NodeJs
详解nodejs中exports和module.exports的区别
Feb 17 NodeJs
nodejs爬虫遇到的乱码问题汇总
Apr 07 NodeJs
详解使用nodeJs安装Vue-cli
May 17 NodeJs
nodejs制作爬虫实现批量下载图片
May 19 NodeJs
nodejs mysql 实现分页的方法
Jun 06 NodeJs
Windows下使用Nodejs运行js的方法
Sep 02 NodeJs
Mac 安装 nodejs方法(图文详细步骤)
Oct 30 NodeJs
nodejs取得当前执行路径的方法
May 13 NodeJs
nodejs同步调用获取mysql数据时遇到的大坑
Mar 02 NodeJs
nodejs各种姿势断点调试的方法
Jun 18 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验证手机号码(支持归属地查询及编码为UTF8)
2013/02/01 PHP
PHP 常用的header头部定义汇总
2015/06/19 PHP
PHP自动生成表单代码分享
2015/06/19 PHP
PHP正则匹配操作简单示例【preg_match_all应用】
2017/07/10 PHP
jQuery中add实现同时选择两个id对象
2010/10/22 Javascript
从零开始学习jQuery (六) jquery中的AJAX使用
2011/02/23 Javascript
广泛收集的jQuery拖放插件集合
2012/04/09 Javascript
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
javascript数组详解
2014/10/22 Javascript
百度地图自定义控件分享
2015/03/04 Javascript
js实现从右向左缓缓浮出网页浮动层广告的方法
2015/05/09 Javascript
jQuery实现美观的多级动画效果菜单代码
2015/09/06 Javascript
jQuery Easyui Datagrid实现单行的上移下移及保存移动的结果
2016/08/15 Javascript
轻松搞定js表单验证
2016/10/13 Javascript
微信小程序 合法域名校验出错详解及解决办法
2017/03/09 Javascript
详解Angular2表单-模板驱动的表单(Template-Driven Forms)
2017/08/04 Javascript
使用vue-cli+webpack搭建vue开发环境的方法
2017/12/22 Javascript
node.js中TCP Socket多进程间的消息推送示例详解
2018/07/10 Javascript
vue axios请求频繁时取消上一次请求的方法
2018/11/10 Javascript
使用watch在微信小程序中实现全局状态共享
2019/06/03 Javascript
微信小程序学习总结(三)条件、模板、文件引用实例分析
2020/06/04 Javascript
[01:55]2014DOTA2国际邀请赛快报:国土生病 紧急去医院治疗
2014/07/10 DOTA
[53:13]DOTA2-DPC中国联赛 正赛 DLG vs PHOENIX BO3 第三场 1月18日
2021/03/11 DOTA
Python打包可执行文件的方法详解
2016/09/19 Python
Python 40行代码实现人脸识别功能
2017/04/02 Python
简单分析python的类变量、实例变量
2019/08/23 Python
Python提取PDF内容的方法(文本、图像、线条等)
2019/09/25 Python
Django继承自带user表并重写的例子
2019/11/18 Python
Python 格式化打印json数据方法(展开状态)
2020/02/27 Python
Python如何将字符串转换为日期
2020/07/31 Python
经济与贸易专业应届生求职信
2013/11/19 职场文书
项目工作说明书
2014/07/29 职场文书
2016年秋季新学期致辞
2015/07/30 职场文书
工程移交协议书
2016/03/24 职场文书
用Python编写简单的gRPC服务的详细过程
2021/07/04 Python
java版 简单三子棋游戏
2022/05/04 Java/Android