node-images 進行圖片壓縮

前置條件:先安裝images

 

 npm install images

 

編寫代碼

思路: 從指定文件夾遍歷圖片,執行壓縮,壓縮完成後放到指定文件夾中,並保持圖片名無變化。

var images = require("images");
var fs = require("fs");

var path = "1012";
var outpath = "compress/";  


function compress(path){
    fs.readdir(path, function(err, files){     
        if(err){
            console.log('error:\n' + err);
            return;
        }

        files.forEach(function(file){

            fs.stat(path + '/' + file, function(err, stat){
                if(err){console.log(err); return;}
                if(stat.isDirectory()){                 
                    // 如果是文件夾遍歷
                    compress(path + '/' + file);
                }else{
                   
                     //遍歷圖片
                    console.log('文件名:' + path + '/' + file);
                    var name = path + '/' + file;
                    var outName =  outpath+file

               images(name) .save(outName, {             
                                quality : 82                    //保存圖片到文件,圖片質量爲50
                            });

                }               
            });

        });

    });
}

compress(path)

  

測試執行: node img.js

 

 

 

結果 

經測試,在不改變圖片寬高情況下,手機拍照原圖進行壓縮,壓縮圖片大小是原圖的4分之1。

 

附註:api接口

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 fileconfig 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 garbage collection
強制調用V8的垃圾回收機制




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章