如何使用imagemin压缩图片和busboy
How to use imagemin to compress images and busboy
我想用下面的库来压缩图片
https://github.com/imagemin/imagemin
问题是当用户使用表单上传时,如何将表单中的文件详细信息插入图像 min 插件?例如,如果文件表单域调用 example-image,我如何将该文件表单域插入 image min 插件以便它可以压缩图像?
我试过:
req 来自 express/nodejs req
var filebus = new Busboy({ headers: req.headers }),
promises = [];
filebus.on('file', function(fieldname, file, filename, encoding, contentType) {
var fileBuffer = new Buffer(0),
s3ImgPath;
if (file) {
file.on('data', function (d) {
fileBuffer = Buffer.concat([fileBuffer, d]);
}).on('end', function () {
Imagemin.buffer(fileBuffer, {
plugins: [
imageminMozjpeg(),
imageminPngquant({quality: '80'})
]
}).then(function (data) {
console.log(data[0]);
if (s3ImgPath) {
promises.push(this.pushImageToS3(s3ImgPath, data[0].data, contentType));
}
});
}.bind(this));
}
});
但问题是我有一个可以上传到 S3 的文件缓冲区。我不想将文件放入 build/images 文件夹。我想为文件获取一个缓冲区,压缩它,然后将该缓冲区上传到 s3。如何使用 image min 获取通过 html 表单上传文件的缓冲区并将其上传到 s3?
输出参数的文档显示它是可选的(尽管不可否认,函数声明不是,这可能会造成混淆)。
output
Type: string
Set the destination folder to where your files will be written. If no
destination is specified no files will be written.
因此,您可以选择不将文件写入存储,而只使用内存中的输出:
imagemin([file.path], {
plugins: [
imageminMozjpeg(),
imageminPngquant({quality: '65-80'})
]
}).then(files => {
// upload file to S3
});
我想用下面的库来压缩图片 https://github.com/imagemin/imagemin
问题是当用户使用表单上传时,如何将表单中的文件详细信息插入图像 min 插件?例如,如果文件表单域调用 example-image,我如何将该文件表单域插入 image min 插件以便它可以压缩图像?
我试过:
req 来自 express/nodejs req
var filebus = new Busboy({ headers: req.headers }),
promises = [];
filebus.on('file', function(fieldname, file, filename, encoding, contentType) {
var fileBuffer = new Buffer(0),
s3ImgPath;
if (file) {
file.on('data', function (d) {
fileBuffer = Buffer.concat([fileBuffer, d]);
}).on('end', function () {
Imagemin.buffer(fileBuffer, {
plugins: [
imageminMozjpeg(),
imageminPngquant({quality: '80'})
]
}).then(function (data) {
console.log(data[0]);
if (s3ImgPath) {
promises.push(this.pushImageToS3(s3ImgPath, data[0].data, contentType));
}
});
}.bind(this));
}
});
但问题是我有一个可以上传到 S3 的文件缓冲区。我不想将文件放入 build/images 文件夹。我想为文件获取一个缓冲区,压缩它,然后将该缓冲区上传到 s3。如何使用 image min 获取通过 html 表单上传文件的缓冲区并将其上传到 s3?
输出参数的文档显示它是可选的(尽管不可否认,函数声明不是,这可能会造成混淆)。
output
Type: string
Set the destination folder to where your files will be written. If no destination is specified no files will be written.
因此,您可以选择不将文件写入存储,而只使用内存中的输出:
imagemin([file.path], {
plugins: [
imageminMozjpeg(),
imageminPngquant({quality: '65-80'})
]
}).then(files => {
// upload file to S3
});