1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > node.js实现图片上传(包含缩略图)

node.js实现图片上传(包含缩略图)

时间:2020-08-21 10:23:04

相关推荐

node.js实现图片上传(包含缩略图)

图片上传

使用multiparty插件实现上传

安装multiparty

npm i --save multiparty

代码实现

const multiparty = require('multiparty');let form = new multiparty.Form({uploadDir: upload.path});

构造参数说明
encoding 设置接收数据编码,默认是utf-8maxFieldsSize 限制字段可以分配的内存量,默认2MmaxFields 限制在发出错误事件之前将要解析的字段数,默认1000maxFilesSize 限制总文件大小,默认无穷大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 Imageimport globimport osimport systry:im = Image.open(sys.argv[1])o_width = im.size[0]o_height = im.size[1]thumb_width = 400size = (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;

这里我推荐使用第二种方法

源码地址

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。