mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	options for basic image quality control
This commit is contained in:
		
							parent
							
								
									9ea8209d4e
								
							
						
					
					
						commit
						c9e4261930
					
				| @ -4,6 +4,7 @@ const repository = require('./repository'); | |||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const protectedSessionService = require('./protected_session'); | const protectedSessionService = require('./protected_session'); | ||||||
| const noteService = require('./notes'); | const noteService = require('./notes'); | ||||||
|  | const optionService = require('./options'); | ||||||
| const imagemin = require('imagemin'); | const imagemin = require('imagemin'); | ||||||
| const imageminMozJpeg = require('imagemin-mozjpeg'); | const imageminMozJpeg = require('imagemin-mozjpeg'); | ||||||
| const imageminPngQuant = require('imagemin-pngquant'); | const imageminPngQuant = require('imagemin-pngquant'); | ||||||
| @ -67,20 +68,16 @@ async function shrinkImage(buffer, originalName) { | |||||||
|     return finalImageBuffer; |     return finalImageBuffer; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const MAX_SIZE = 1000; |  | ||||||
| const MAX_BYTE_SIZE = 200000; // images should have under 100 KBs
 |  | ||||||
| 
 |  | ||||||
| async function resize(buffer) { | async function resize(buffer) { | ||||||
|  |     const imageMaxWidthHeight = await optionService.getOptionInt('imageMaxWidthHeight'); | ||||||
|  | 
 | ||||||
|     const image = await jimp.read(buffer); |     const image = await jimp.read(buffer); | ||||||
| 
 | 
 | ||||||
|     if (image.bitmap.width > image.bitmap.height && image.bitmap.width > MAX_SIZE) { |     if (image.bitmap.width > image.bitmap.height && image.bitmap.width > imageMaxWidthHeight) { | ||||||
|         image.resize(MAX_SIZE, jimp.AUTO); |         image.resize(imageMaxWidthHeight, jimp.AUTO); | ||||||
|     } |     } | ||||||
|     else if (image.bitmap.height > MAX_SIZE) { |     else if (image.bitmap.height > imageMaxWidthHeight) { | ||||||
|         image.resize(jimp.AUTO, MAX_SIZE); |         image.resize(jimp.AUTO, imageMaxWidthHeight); | ||||||
|     } |  | ||||||
|     else if (buffer.byteLength <= MAX_BYTE_SIZE) { |  | ||||||
|         return buffer; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // we do resizing with max quality which will be trimmed during optimization step next
 |     // 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, { |     return await imagemin.buffer(buffer, { | ||||||
|         plugins: [ |         plugins: [ | ||||||
|             imageminMozJpeg({ |             imageminMozJpeg({ | ||||||
|                 quality: 50 |                 quality: await optionService.getOptionInt('imageJpegQuality') | ||||||
|             }), |             }), | ||||||
|             imageminPngQuant({ |             imageminPngQuant({ | ||||||
|                 quality: [0, 0.7] |                 quality: [0, 0.7] | ||||||
|  | |||||||
| @ -10,6 +10,18 @@ async function getOption(name) { | |||||||
|     return option.value; |     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) { | async function setOption(name, value) { | ||||||
|     const option = await require('./repository').getOption(name); |     const option = await require('./repository').getOption(name); | ||||||
| 
 | 
 | ||||||
| @ -51,6 +63,7 @@ async function getOptionsMap(allowedOptions) { | |||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|     getOption, |     getOption, | ||||||
|  |     getOptionInt, | ||||||
|     setOption, |     setOption, | ||||||
|     createOption, |     createOption, | ||||||
|     getOptions, |     getOptions, | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ const passwordEncryptionService = require('./password_encryption'); | |||||||
| const myScryptService = require('./my_scrypt'); | const myScryptService = require('./my_scrypt'); | ||||||
| const appInfo = require('./app_info'); | const appInfo = require('./app_info'); | ||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
|  | const log = require('./log'); | ||||||
| const dateUtils = require('./date_utils'); | const dateUtils = require('./date_utils'); | ||||||
| 
 | 
 | ||||||
| async function initDocumentOptions() { | async function initDocumentOptions() { | ||||||
| @ -75,7 +76,9 @@ const defaultOptions = [ | |||||||
|     { name: 'similarNotesWidget', value: '{"enabled":true,"expanded":true,"position":600}', isSynced: false }, |     { name: 'similarNotesWidget', value: '{"enabled":true,"expanded":true,"position":600}', isSynced: false }, | ||||||
|     { name: 'spellCheckEnabled', value: 'true', isSynced: false }, |     { name: 'spellCheckEnabled', value: 'true', isSynced: false }, | ||||||
|     { name: 'spellCheckLanguageCode', value: 'en-US', 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() { | async function initStartupOptions() { | ||||||
| @ -84,6 +87,8 @@ async function initStartupOptions() { | |||||||
|     for (const {name, value, isSynced} of defaultOptions) { |     for (const {name, value, isSynced} of defaultOptions) { | ||||||
|         if (!(name in optionsMap)) { |         if (!(name in optionsMap)) { | ||||||
|             await optionService.createOption(name, value, isSynced); |             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(); |     const db = await createConnection(); | ||||||
|     sql.setDbConnection(db); |     sql.setDbConnection(db); | ||||||
| 
 | 
 | ||||||
|     await require('./options_init').initStartupOptions(); |  | ||||||
| 
 |  | ||||||
|     resolve(); |     resolve(); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| @ -76,6 +74,8 @@ async function initDbConnection() { | |||||||
|             await migrationService.migrate(); |             await migrationService.migrate(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         await require('./options_init').initStartupOptions(); | ||||||
|  | 
 | ||||||
|         log.info("DB ready."); |         log.info("DB ready."); | ||||||
|         dbReadyResolve(); |         dbReadyResolve(); | ||||||
|     }); |     }); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam