mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	options for basic image quality control
This commit is contained in:
		| @@ -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); | ||||
|     if (image.bitmap.width > image.bitmap.height && image.bitmap.width > imageMaxWidthHeight) { | ||||
|         image.resize(imageMaxWidthHeight, jimp.AUTO); | ||||
|     } | ||||
|     else if (image.bitmap.height > MAX_SIZE) { | ||||
|         image.resize(jimp.AUTO, MAX_SIZE); | ||||
|     } | ||||
|     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] | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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}"`); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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(); | ||||
|     }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user