nodejs處理圖片的中間件node-images詳解
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 //保存圖片到文件,圖片質(zhì)量為50 });
Features 功能特性
- Lightweight:no need to install any image processing library.
- 輕量級:無需安裝任何圖像處理庫。
- Cross-platform: Released a compiled .node file on windows, just download and start.
- 跨平臺:Windows下發(fā)布了編譯好的.node文件,下載就能用。
- Easy-to-use: Provide jQuery-like chaining API.Simple and reliable!
- 方便用:jQuery風(fēng)格的API,簡單可依賴。
Installation 安裝
$ npm install images
API 接口
node-images provide jQuery-like Chaining API,You can start the chain like this:
node-images 提供了類似jQuery的鏈?zhǔn)秸{(diào)用API,您可以這樣開始:
/* Load and decode image from file */ /* 從指定文件加載并解碼圖像 */ images(file) /* Create a new transparent image */ /* 創(chuàng)建一個(gè)指定寬高的透明圖像 */ images(width, height) /* Load and decode image from a buffer */ /* 從Buffer數(shù)據(jù)中解碼圖像 */ images(buffer[, start[, end]]) /* Copy from another image */ /* 從另一個(gè)圖像中復(fù)制區(qū)域來創(chuàng)建圖像 */ images(image[, x, y, width, height])
images(file)
Load and decode image from file從指定文件加載并解碼圖像
images(width, height)
Create a new transparent image創(chuàng)建一個(gè)指定寬高的透明圖像
images(buffer[, start[, end]])
Load and decode image from a buffer從Buffer數(shù)據(jù)中解碼圖像
images(image[, x, y, width, height])
Copy from another image從另一個(gè)圖像中復(fù)制區(qū)域來創(chuàng)建圖像
.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 )在當(dāng)前圖像( x , y )上繪制 image 圖像
.encode(type[, config])
eg:images("input.png").encode("jpg", {operation:50}) Encode image to buffer, config is image setting.
以指定格式編碼當(dāng)前圖像到Buffer,config為圖片設(shè)置,目前支持設(shè)置JPG圖像質(zhì)量
Return buffer
返回填充好的Buffer
Note:The operation will cut off the chain
注意:該操作將會切斷調(diào)用鏈
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 }
編碼并保存當(dāng)前圖像到 file ,如果type未指定,則根據(jù) file 自動(dòng)判斷文件類型,config為圖片設(shè)置,目前支持設(shè)置JPG圖像質(zhì)量
.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獲取或者設(shè)置圖像寬高,如果height未指定,則根據(jù)當(dāng)前寬高等比縮放
.resize(width[, height])
Set the size of the image,if the height is not specified, then scaling based on the current width and height
設(shè)置圖像寬高,如果height未指定,則根據(jù)當(dāng)前寬高等比縮放, 默認(rèn)采用 bicubic 算法。
.width([width])
Get width for the image or set width of the image獲取或設(shè)置圖像寬度
.height([height])
Get height for the image or set height of the image獲取或設(shè)置圖像高度
images.setLimit(width, height)
Set the limit size of each image 設(shè)置庫處理圖片的大小限制,設(shè)置后對所有新的操作生效(如果超限則拋出異常)
images.setGCThreshold(value)
Set the garbage collection threshold 設(shè)置圖像處理庫自動(dòng)gc的閾值(當(dāng)新增內(nèi)存使用超過該閾值時(shí),執(zhí)行垃圾回收)
images.getUsedMemory()
Get used memory (in bytes)得到圖像處理庫占用的內(nèi)存大小(單位為字節(jié))
images.gc()
Forced call garbage collection 強(qiáng)制調(diào)用V8的垃圾回收機(jī)制
https://github.com/zhangyuanwei/node-images
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
NodeJs crypto加密制作token的實(shí)現(xiàn)代碼
這篇文章主要介紹了NodeJs crypto加密制作token的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Node.js實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了Node.js實(shí)現(xiàn)發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11npm?install的--save和--save-dev使用說明(推薦)
這篇文章主要介紹了npm?install的--save和--save-dev使用說明,文中給大家提到了各個(gè)命令的區(qū)別及各種安裝參數(shù)的區(qū)別,需要的朋友可以參考下2022-08-08node.js模擬實(shí)現(xiàn)自動(dòng)發(fā)送郵件驗(yàn)證碼
這篇文章主要為大家介紹了node.js模擬實(shí)現(xiàn)自動(dòng)發(fā)送郵件驗(yàn)證碼的實(shí)例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04用nodejs的實(shí)現(xiàn)原理和搭建服務(wù)器(動(dòng)態(tài))
下面小編就為大家?guī)硪黄胣odejs的實(shí)現(xiàn)原理和搭建服務(wù)器(動(dòng)態(tài))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08Node.js查找當(dāng)前目錄下文件夾實(shí)例代碼
本篇文章主要介紹了Node.js查找當(dāng)前目錄下文件夾實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03