首頁 > web前端 > js教程 > 主體

nodejs中設定圖片的中間件實例教程

零下一度
發布: 2017-05-09 10:08:43
原創
1165 人瀏覽過

這篇文章主要介紹了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          //保存图片到文件,图片质量为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的垃圾回收機制

github.com/zhangyuanwei/node-images


【相關推薦】

##1. 

免費js在線影片教學

2. 

JavaScript中文參考手冊

#3. 

php.cn獨孤九賤(3)-JavaScript影片教學

#

以上是nodejs中設定圖片的中間件實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!