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 Post请求报socket hang up错误的解决办法
Sep 25 NodeJs
Nodejs中的this详解
Mar 26 NodeJs
NodeJS中的MongoDB快速入门详细教程
Nov 11 NodeJs
NodeJS测试框架mocha入门教程
Mar 28 NodeJs
nodejs个人博客开发第四步 数据模型
Apr 12 NodeJs
详解nodejs异步I/O和事件循环
Jun 07 NodeJs
nodejs简单读写excel内容的方法示例
Mar 16 NodeJs
详解NodeJs开发微信公众号
May 25 NodeJs
NodeJs项目中关闭ESLint的方法
Aug 09 NodeJs
nodejs检测因特网是否断开的解决方案
Apr 17 NodeJs
nodejs中实现修改用户路由功能
May 24 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
建立文件交换功能的脚本(一)
2006/10/09 PHP
php的sprintf函数的用法 控制浮点数格式
2014/02/14 PHP
PHP实现取得HTTP请求的原文
2014/08/18 PHP
浅谈php+phpStorm+xdebug配置方法
2015/09/17 PHP
PHP微信红包生成代码分享
2016/10/06 PHP
thinkPHP数据库增删改查操作方法实例详解
2016/12/06 PHP
PHP 实现从数据库导出到.csv文件方法
2017/07/06 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
jQuery下的动画处理总结
2013/10/10 Javascript
js改变鼠标的形状和样式的方法
2014/03/31 Javascript
借助JavaScript脚本判断浏览器Flash Player信息的方法
2014/07/09 Javascript
Python脚本后台运行的几种方式
2015/03/09 Javascript
javascript-解决mongoose数据查询的异步操作
2016/12/22 Javascript
移动端使用localResizeIMG4压缩图片
2017/04/22 Javascript
使用vue-infinite-scroll实现无限滚动效果
2018/06/22 Javascript
bootstrap 弹出框modal添加垂直方向滚轴效果
2018/07/09 Javascript
微信小程序按钮去除边框线分享页面功能
2018/08/27 Javascript
从零开始搭建vue移动端项目到上线的步骤
2018/10/15 Javascript
jQuery模拟html下拉多选框的原生实现方法示例
2019/05/30 jQuery
Python遍历目录的4种方法实例介绍
2015/04/13 Python
Python生成不重复随机值的方法
2015/05/11 Python
python基于BeautifulSoup实现抓取网页指定内容的方法
2015/07/09 Python
学习python类方法与对象方法
2016/03/15 Python
python中import学习备忘笔记
2017/01/24 Python
详解Pymongo常用查询方法总结
2021/01/29 Python
HTML5页面直接调用百度地图API获取当前位置直接导航目的地的实现代码
2018/03/02 HTML / CSS
CSS代码检查工具stylelint的使用方法详解
2021/03/27 HTML / CSS
人力资源管理专业应届生求职信
2013/09/28 职场文书
医生自荐信
2013/10/11 职场文书
电子专业自荐信
2014/07/01 职场文书
市场策划求职信
2014/08/07 职场文书
上课不认真检讨书
2014/09/17 职场文书
机关作风建设自查报告
2014/10/22 职场文书
开展党的群众路线教育实践活动情况汇报
2014/11/05 职场文书
同学会感言
2015/07/30 职场文书
90行Python代码开发个人云盘应用
2021/04/20 Python