【问题标题】:Transform photo with imagemin before uploading to s3 using multer-s3-transform在使用 multer-s3-transform 上传到 s3 之前使用 imagemin 转换照片
【发布时间】:2019-10-10 15:09:37
【问题描述】:

我尝试使用 multer-s3-transform 将我的照片上传到 s3,但在此之前我需要使用 imagemin 将图像转换为更小的文件大小。我可以使用 sharp 来做到这一点,但仍然想尝试使用 imagemin 来专门设置质量。

这就是我使用Sharp所做的。我需要找到一种使用 imagemin 的方法

const multerS3Obj = multerS3({
    s3 : s3,
    bucket : config.amazon.s3.bucketName,
    acl : "public-read",
    contentType : multerS3.AUTO_CONTENT_TYPE,
    metadata : function(req, file, cb) {
        const metadataObj = Object.assign({}, req.body);

        metadataObj.content_type = file.mimetype;
        metadataObj.filename = file.originalname;

        cb(null, metadataObj);
    },
    shouldTransform: function(req, file, cb) {
        cb(null, /^image/i.test(file.mimetype));
    },
    transforms: [
        {
          key: function(req, file, cb) {
            const refType = req.params.refType,
                refId = req.params.refId,
                subfolder = `uploads/${refType}/${refId}/`;
                cb(null, subfolder + file.originalname);
          },
          transform: function(req, file, cb) {
            cb(null, sharp().resize(null,null));
          }
        }
    ]
});

这是我使用 imagemin 所做的,但它不起作用

const buff = async (image, path) => {
    const files = await imagemin([image], path, {
        plugins : [
            imageminJpegtran(),
            imageminPngquant({quality: '65-80'})
        ]
    })
}

这是第一个代码中的转换部分

transform: async function(req, file, cb) {
    const files = await buff(file.originalname, 'location')
    cb(null, sharp().resize(null,null));
}

我不断收到错误:uncaughtException: dest.on is not a function

【问题讨论】:

    标签: javascript node.js sharp multer-s3 imagemin


    【解决方案1】:

    我能够解决它。而不是使用imagemin设置图像质量发现sharp支持设置图像质量

    let quality = ''
    if (file.mimetype === 'image/jpeg') {
        quality = sharp().jpeg({quality: 50})
    } else if (file.mimetype === 'image/png') {
        quality = sharp().png({quality: 50})
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 2017-05-22
      • 2020-06-29
      • 2017-03-22
      • 2019-10-26
      • 1970-01-01
      • 2016-01-20
      • 2021-02-07
      • 2018-08-29
      相关资源
      最近更新 更多