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获取本机内网和外网ip地址的实现代码
Jun 01 NodeJs
Nodejs实现的一个静态服务器实例
Dec 06 NodeJs
NodeJS与HTML5相结合实现拖拽多个文件上传到服务器的实现方法
Jul 26 NodeJs
Nodejs读取文件时相对路径的正确写法(使用fs模块)
Apr 27 NodeJs
nodejs 图片预览和上传的示例代码
Sep 30 NodeJs
修改Nodejs内置的npm默认配置路径方法
May 13 NodeJs
详解nodejs 开发企业微信第三方应用入门教程
Mar 12 NodeJs
详解nodejs http请求相关总结
Mar 31 NodeJs
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
May 30 NodeJs
Nodejs技巧之Exceljs表格操作用法示例
Nov 06 NodeJs
nodejs+koa2 实现模仿springMVC框架
Oct 21 NodeJs
windows如何把已安装的nodejs高版本降级为低版本(图文教程)
Dec 14 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简单预防sql注入的方法
2016/09/27 PHP
PHP读MYSQL中文乱码的快速解决方法
2016/10/01 PHP
PHP基于DOMDocument解析和生成xml的方法分析
2017/07/17 PHP
Laravel 加载第三方类库的方法
2018/04/20 PHP
php微信公众号开发之简答题
2018/10/20 PHP
PHP实现随机发放扑克牌
2020/04/21 PHP
页面载入结束自动调用js函数示例
2013/09/23 Javascript
详解jquery uploadify 上传文件
2013/11/09 Javascript
js 与 php 通过json数据进行通讯示例
2014/03/26 Javascript
JsRender实用入门教程
2014/10/31 Javascript
js的各种排序算法实现(总结)
2016/07/23 Javascript
JS如何设置cookie有效期为当天24点并弹出欢迎登陆界面
2016/08/04 Javascript
Javascript中JSON数据分组优化实践及JS操作JSON总结
2017/12/22 Javascript
vue2中使用sass并配置全局的sass样式变量的方法
2018/09/04 Javascript
在element-ui的el-tree组件中用render函数生成el-button的实例代码
2018/11/05 Javascript
vue组件中watch props根据v-if动态判断并挂载DOM的问题
2019/05/12 Javascript
Layui 动态禁止select下拉的例子
2019/09/03 Javascript
微信小程序表单验证WxValidate的使用
2019/11/27 Javascript
python 中文乱码问题深入分析
2011/03/13 Python
Python使用scrapy采集时伪装成HTTP/1.1的方法
2015/04/08 Python
Python文件读写常见用法总结
2019/02/22 Python
Python Django基础二之URL路由系统
2019/07/18 Python
关于django 1.10 CSRF验证失败的解决方法
2019/08/31 Python
ansible动态Inventory主机清单配置遇到的坑
2020/01/19 Python
Python动态导入模块和反射机制详解
2020/02/18 Python
pygame实现飞机大战
2020/03/11 Python
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
2020/12/09 Python
Miller Harris官网:英国小众香水品牌
2020/09/24 全球购物
人事部主管岗位职责
2013/12/26 职场文书
生产车间主管岗位职责
2013/12/28 职场文书
毕业生自荐信如何写
2014/03/24 职场文书
感恩节活动策划方案
2014/05/16 职场文书
联片教研活动总结
2014/07/01 职场文书
军训后的感想
2015/08/07 职场文书
Golang使用Panic与Recover进行错误捕获
2022/03/22 Golang
Win10防火墙白名单怎么设置?Win10添加防火墙白名单方法
2022/04/06 数码科技