mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
compressing PNGs with pngquant
This commit is contained in:
parent
d9f29cbf27
commit
91cf090820
@ -8,6 +8,7 @@ const utils = require('../../services/utils');
|
|||||||
const multer = require('multer')();
|
const multer = require('multer')();
|
||||||
const imagemin = require('imagemin');
|
const imagemin = require('imagemin');
|
||||||
const imageminMozJpeg = require('imagemin-mozjpeg');
|
const imageminMozJpeg = require('imagemin-mozjpeg');
|
||||||
|
const imageminPngQuant = require('imagemin-pngquant');
|
||||||
const jimp = require('jimp');
|
const jimp = require('jimp');
|
||||||
|
|
||||||
router.get('/:imageId/:filename', auth.checkApiAuth, async (req, res, next) => {
|
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_SIZE = 1000;
|
||||||
|
const MAX_BYTE_SIZE = 200000; // images should have under 100 KBs
|
||||||
|
|
||||||
async function resize(buffer) {
|
async function resize(buffer) {
|
||||||
const image = await jimp.read(buffer);
|
const image = await jimp.read(buffer);
|
||||||
|
|
||||||
|
console.log("Size: ", buffer.byteLength);
|
||||||
|
|
||||||
if (image.bitmap.width > image.bitmap.height && image.bitmap.width > MAX_SIZE) {
|
if (image.bitmap.width > image.bitmap.height && image.bitmap.width > MAX_SIZE) {
|
||||||
image.resize(MAX_SIZE, jimp.AUTO);
|
image.resize(MAX_SIZE, jimp.AUTO);
|
||||||
}
|
}
|
||||||
else if (image.bitmap.height > MAX_SIZE) {
|
else if (image.bitmap.height > MAX_SIZE) {
|
||||||
image.resize(jimp.AUTO, MAX_SIZE);
|
image.resize(jimp.AUTO, MAX_SIZE);
|
||||||
}
|
}
|
||||||
else {
|
else if (buffer.byteLength <= MAX_BYTE_SIZE) {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,8 +89,11 @@ async function resize(buffer) {
|
|||||||
async function optimize(buffer) {
|
async function optimize(buffer) {
|
||||||
return await imagemin.buffer(buffer, {
|
return await imagemin.buffer(buffer, {
|
||||||
plugins: [
|
plugins: [
|
||||||
imageminMozJpeg({
|
// imageminMozJpeg({
|
||||||
quality: 50
|
// quality: 50
|
||||||
|
// }),
|
||||||
|
imageminPngQuant({
|
||||||
|
quality: "0-70"
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user