compressing PNGs with pngquant

This commit is contained in:
azivner 2018-01-06 13:53:02 -05:00
parent d9f29cbf27
commit 91cf090820

View file

@ -8,6 +8,7 @@ const utils = require('../../services/utils');
const multer = require('multer')();
const imagemin = require('imagemin');
const imageminMozJpeg = require('imagemin-mozjpeg');
const imageminPngQuant = require('imagemin-pngquant');
const jimp = require('jimp');
router.get('/:imageId/:filename', auth.checkApiAuth, async (req, res, next) => {
@ -54,17 +55,20 @@ router.post('/upload', auth.checkApiAuth, multer.single('upload'), async (req, r
});
const MAX_SIZE = 1000;
const MAX_BYTE_SIZE = 200000; // images should have under 100 KBs
async function resize(buffer) {
const image = await jimp.read(buffer);
console.log("Size: ", buffer.byteLength);
if (image.bitmap.width > image.bitmap.height && image.bitmap.width > MAX_SIZE) {
image.resize(MAX_SIZE, jimp.AUTO);
}
else if (image.bitmap.height > MAX_SIZE) {
image.resize(jimp.AUTO, MAX_SIZE);
}
else {
else if (buffer.byteLength <= MAX_BYTE_SIZE) {
return buffer;
}
@ -85,8 +89,11 @@ async function resize(buffer) {
async function optimize(buffer) {
return await imagemin.buffer(buffer, {
plugins: [
imageminMozJpeg({
quality: 50
// imageminMozJpeg({
// quality: 50
// }),
imageminPngQuant({
quality: "0-70"
})
]
});