1. 程式人生 > >node.js實現圖片上傳(包含縮略圖)

node.js實現圖片上傳(包含縮略圖)

http close path return new tde log img thumb

圖片上傳

使用multiparty插件實現上傳

安裝multiparty

npm i --save multiparty

代碼實現

const multiparty = require(‘multiparty‘);
let form = new multiparty.Form({uploadDir: upload.path});
構造參數說明
  • encoding 設置接收數據編碼,默認是utf-8
  • maxFieldsSize 限制字段可以分配的內存量,默認2M
  • maxFields 限制在發出錯誤事件之前將要解析的字段數,默認1000
  • maxFilesSize 限制總文件大小,默認無窮大
  • autoFields 啟用字段事件並禁用字段的部分事件。如果添加字段偵聽器,則自動將其設置為true。
  • autoFiles 啟用文件事件並禁用文件的部分事件。如果添加了一個文件偵聽器,則自動將其設置為true。
  • uploadDir 文件上傳的目錄

==如果回調提供,autofields和autofiles被設置為true,所有字段和文件的收集和傳遞給回調,不再需要聽任何形式的事件。==

事件說明
  • part 請求文件數據時觸發,回調函數是一個實現可讀流的實例對象
    • headers:頭部文件
    • name:字段名稱
    • filename:文件名稱
    • byteFffset:主體數據的字節偏移量
    • byteCount:數據總的字節長度
  • aborted 在請求中止時觸發

  • close 在請求結束之後觸發

  • file 接收到文件的參數
    • name:字段名稱
    • file:存儲著文件信息的對象
    • fieldName:字段名稱
    • originalFilename:文件名稱
    • path:寫到磁盤上文件的具體路徑
    • headers:存儲著頭部信息
    • size:文件具體大小
  • field 獲取請求的具體數據。回調函數兩個參數
    • name:字段名
    • value:字段值

==註意使用part事件時,如果同時監聽fields和files事,part事件會獲取不到數據。==

更多說明

增加事件監聽後

    let upload = uploadPath();
    console.dir(upload);
    let form = new multiparty.Form({
        uploadDir: upload.path
    });
    form.on(‘error‘, function (err) {
        console.log(‘Error parsing form: ‘ + err.stack);
    });
    form.on(‘file‘, (name, file) => {
        console.log(file);
        res.json(file);
    });
    form.on(‘close‘, function () {
        console.log(‘Upload completed!‘);
    });
    form.parse(req);

圖片處理

一般來說上傳圖片都會進行簡單的處理,例如無損畫質壓縮,縮略圖生成等

1、用 resize-img 進行縮略圖制作

安裝組件

npm install --save resize-img

代碼實現
    const resizeImg = require(‘resize-img‘);
    
    resizeImg(fs.readFileSync(file_path), {width: 800}).then(buf => {
        fs.writeFileSync(file_path, buf);
    });

2、使用python圖片處理庫PIL

為什麽使用python?

CPU密集型任務是Node.js的軟肋,當服務器同時執行多個圖片處理時(特別是比較大的圖片時),會出現BUG,所以我們可以選用python圖片處理庫PIL

PIL安裝
pip install pillow
python實現
from PIL import Image
import glob
import os
import sys

try:
    im = Image.open(sys.argv[1])
    o_width = im.size[0]
    o_height = im.size[1]
    thumb_width = 400
    size = (thumb_width, o_height * thumb_width / o_width)
    im.thumbnail(size)
    im.save(sys.argv[2],im.format)
    print(sys.argv[2])
except IOError:
     print("cannot create thumbnail for", sys.argv[1])
node調用pyhton
const path = require(‘path‘);
const exec = require(‘child_process‘).exec;

let baseDir = path.resolve(__dirname, ‘..‘);

let thumb_pic = (arg1, arg2) => {
    return new Promise((resolve, reject) => {
        let py_path = baseDir + "/public/py/";
        exec(`python ${py_path}thumb_pic.py  ${arg1} ${arg2}`, function (error, stdout, stderr) {
            if (stdout.length > 0) {
                console.log(‘you offer args:‘, stdout);
                resolve(true);
            } else {
                console.log(‘you don\‘t offer args‘);
                resolve(false);
            }
            if (error) {
                console.info(‘stderr : ‘ + stderr);
                reject(stderr);
            }
        });
    });
};

module.exports.thumb_pic = thumb_pic;

這裏我推薦使用第二種方法
源碼地址

node.js實現圖片上傳(包含縮略圖)