mirror of
https://github.com/zadam/trilium.git
synced 2025-01-01 12:52:17 +08:00
compressing PNGs with pngquant
This commit is contained in:
parent
d9f29cbf27
commit
91cf090820
1 changed files with 10 additions and 3 deletions
|
@ -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"
|
||||
})
|
||||
]
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue