Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
options for basic image quality control
zadamc9e426193020
diff --git a/src/services/image.js b/src/services/image.jsindex 8523c0fde8..41fc126f0e 100644--- a/src/services/image.js+++ b/src/services/image.js@@ -4,6 +4,7 @@ const repository = require('./repository');const log = require('./log');const protectedSessionService = require('./protected_session');const noteService = require('./notes');+const optionService = require('./options');const imagemin = require('imagemin');const imageminMozJpeg = require('imagemin-mozjpeg');const imageminPngQuant = require('imagemin-pngquant');@@ -67,20 +68,16 @@ async function shrinkImage(buffer, originalName) {return finalImageBuffer;}-const MAX_SIZE = 1000;-const MAX_BYTE_SIZE = 200000; // images should have under 100 KBs-async function resize(buffer) {+ const imageMaxWidthHeight = await optionService.getOptionInt('imageMaxWidthHeight');+const image = await jimp.read(buffer);- 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);+ if (image.bitmap.width > image.bitmap.height && image.bitmap.width > imageMaxWidthHeight) {+ image.resize(imageMaxWidthHeight, jimp.AUTO);}- else if (buffer.byteLength <= MAX_BYTE_SIZE) {- return buffer;+ else if (image.bitmap.height > imageMaxWidthHeight) {+ image.resize(jimp.AUTO, imageMaxWidthHeight);}// we do resizing with max quality which will be trimmed during optimization step next@@ -96,7 +93,7 @@ async function optimize(buffer) {return await imagemin.buffer(buffer, {plugins: [imageminMozJpeg({- quality: 50+ quality: await optionService.getOptionInt('imageJpegQuality')}),imageminPngQuant({quality: [0, 0.7]diff --git a/src/services/options.js b/src/services/options.jsindex 2811053883..31ec697de5 100644--- a/src/services/options.js+++ b/src/services/options.js@@ -10,6 +10,18 @@ async function getOption(name) {return option.value;}+async function getOptionInt(name) {+ const val = await getOption(name);++ const intVal = parseInt(val);++ if (isNaN(intVal)) {+ throw new Error(`Could not parse "${val}" into integer for option "${name}"`);+ }++ return intVal;+}+async function setOption(name, value) {const option = await require('./repository').getOption(name);@@ -51,6 +63,7 @@ async function getOptionsMap(allowedOptions) {module.exports = {getOption,+ getOptionInt,setOption,createOption,getOptions,diff --git a/src/services/options_init.js b/src/services/options_init.jsindex 0f30d14657..63d2fc1026 100644--- a/src/services/options_init.js+++ b/src/services/options_init.js@@ -3,6 +3,7 @@ const passwordEncryptionService = require('./password_encryption');const myScryptService = require('./my_scrypt');const appInfo = require('./app_info');const utils = require('./utils');+const log = require('./log');const dateUtils = require('./date_utils');async function initDocumentOptions() {@@ -75,7 +76,9 @@ const defaultOptions = [{ name: 'similarNotesWidget', value: '{"enabled":true,"expanded":true,"position":600}', isSynced: false },{ name: 'spellCheckEnabled', value: 'true', isSynced: false },{ name: 'spellCheckLanguageCode', value: 'en-US', isSynced: false },- { name: 'hideTabRowForOneTab', value: 'false', isSynced: false }+ { name: 'hideTabRowForOneTab', value: 'false', isSynced: false },+ { name: 'imageMaxWidthHeight', value: '1200', isSynced: true },+ { name: 'imageJpegQuality', value: '75', isSynced: true }];async function initStartupOptions() {@@ -84,6 +87,8 @@ async function initStartupOptions() {for (const {name, value, isSynced} of defaultOptions) {if (!(name in optionsMap)) {await optionService.createOption(name, value, isSynced);++ log.info(`Created missing option "${name}" with default value "${value}"`);}}}diff --git a/src/services/sql_init.js b/src/services/sql_init.jsindex 51cb91d9f6..45f8272a1f 100644--- a/src/services/sql_init.js+++ b/src/services/sql_init.js@@ -20,8 +20,6 @@ const dbConnection = new Promise(async (resolve, reject) => {const db = await createConnection();sql.setDbConnection(db);- await require('./options_init').initStartupOptions();-resolve();});@@ -76,6 +74,8 @@ async function initDbConnection() {await migrationService.migrate();}+ await require('./options_init').initStartupOptions();+log.info("DB ready.");dbReadyResolve();});