在 AWS 上传之前使用 ImageMin 压缩图像

问题描述

在将图像上传到我的 S3 存储桶之前,我尝试使用 imagemin 对其进行压缩。为此,我有以下代码

let files = null;
if (contentType === 'image/png') {
    files = await imagemin([fileName],{
        plugins: [
            imageminpngquant({
                quality: [0.5,0.6],}),],});
} else if (contentType === 'image/jpeg') {
    files = await imagemin([fileName],{
        plugins: [imageminJpegtran()],});
}

const imageData = _.get(files,['0','data'],null);

// Setting up S3 upload parameters
const params = {
    Bucket: process.env.S3_BUCKET_NAME,Key: awsFileName,Body: imageData,ContentType: contentType,};

// Uploading files to the bucket
const data = await s3.upload(params).promise();

虽然这有效,但我注意到图像的文件大小与原始图像完全相同 - 这让我相信正在上传原始图像,而不是压缩图像

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)