I was trying to use multer-s3-transform in uploading my photo to s3, but before that I need to transform the image to much lower file size using imagemin. I was able to do it using sharp but still want to try it using imagemin to specifically set the quality.
This is what I did using sharp. I need to find a way how to do it using 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));
}
}
]
});
This is what I did using imagemin but it didn't work
const buff = async (image, path) => {
const files = await imagemin([image], path, {
plugins : [
imageminJpegtran(),
imageminPngquant({quality: '65-80'})
]
})
}
this is the transform part in the first code
transform: async function(req, file, cb) {
const files = await buff(file.originalname, 'location')
cb(null, sharp().resize(null,null));
}
I keep getting error: uncaughtException: dest.on is not a function
I was able to solved it. instead of using imagemin to set the quality of the image found out that sharp supports setting of image quality
let quality = ''
if (file.mimetype === 'image/jpeg') {
quality = sharp().jpeg({quality: 50})
} else if (file.mimetype === 'image/png') {
quality = sharp().png({quality: 50})
}