如何使用imagemin压缩图像和busboy(How to use imagemin to compress images and busboy)

编程入门 行业动态 更新时间:2024-10-26 01:28:46
如何使用imagemin压缩图像和busboy(How to use imagemin to compress images and busboy)

我想使用下面的库来压缩图像https://github.com/imagemin/imagemin

问题是当用户使用表单上传时,如何将表单中的文件详细信息插入到映像min插件中? 例如,如果文件表单字段是调用example-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?

I want to use the following library to compress images https://github.com/imagemin/imagemin

The problem is when the user uploads using a form, how do I plug in the file details from the form to the image min plugin? Like for example, if the file form field is call example-image, how do I plug that file form field to image min plugin so that it can compress the images?

I tried:

req is from the 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)); } });

But the problem is I rather have a buffer of the file that I can upload to S3. I don't want to come the files to a build/images folder. I want to get a buffer for the file, compress it, and upload that buffer to s3. How can I use image min to get a buffer of the file upload via html form and upload that to s3?

最满意答案

输出参数的文档显示它是可选的(尽管可以肯定,函数声明没有,这可能会令人困惑)。

产量

类型:字符串

将目标文件夹设置为文件的写入位置。 如果没有指定目的地,则不会写入任何文件。

因此,您可以选择不将文件写入存储,只需使用内存中的输出:

imagemin([file.path], { plugins: [ imageminMozjpeg(), imageminPngquant({quality: '65-80'}) ] }).then(files => { // upload file to S3 });

The documentation for the output parameter shows that it is optional (though admittedly, the function declaration did not, which might be confusing).

output

Type: string

Set the destination folder to where your files will be written. If no destination is specified no files will be written.

Therefore, you can opt out of writing the files to storage and just use the output in memory:

imagemin([file.path], { plugins: [ imageminMozjpeg(), imageminPngquant({quality: '65-80'}) ] }).then(files => { // upload file to S3 });

更多推荐

本文发布于:2023-08-06 20:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   imagemin   images   busboy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!