From d8c69c5acc22b4dcb704d1cdfb43893e902d016f Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 5 Jan 2023 10:53:57 +0530 Subject: [PATCH] add size parameter --- src/api/imageProcessor.ts | 6 ++-- src/services/imageProcessor.ts | 55 +++++++++++++++++++++++++--------- src/utils/ipcComms.ts | 9 ++++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/api/imageProcessor.ts b/src/api/imageProcessor.ts index ffeb0e60e..07389bd71 100644 --- a/src/api/imageProcessor.ts +++ b/src/api/imageProcessor.ts @@ -13,7 +13,8 @@ export async function convertHEIC(fileData: Uint8Array): Promise { export async function generateImageThumbnail( inputFile: File | ElectronFile, - maxDimension: number + maxDimension: number, + maxSize: number ): Promise { let inputFilePath = null; let createdTempInputFile = null; @@ -32,7 +33,8 @@ export async function generateImageThumbnail( const thumbnail = await ipcRenderer.invoke( 'generate-image-thumbnail', inputFilePath, - maxDimension + maxDimension, + maxSize ); return thumbnail; } finally { diff --git a/src/services/imageProcessor.ts b/src/services/imageProcessor.ts index 95f2b753f..198b2916b 100644 --- a/src/services/imageProcessor.ts +++ b/src/services/imageProcessor.ts @@ -18,6 +18,10 @@ const MAX_DIMENSION_PLACEHOLDER = 'MAX_DIMENSION'; const SAMPLE_SIZE_PLACEHOLDER = 'SAMPLE_SIZE'; const INPUT_PATH_PLACEHOLDER = 'INPUT'; const OUTPUT_PATH_PLACEHOLDER = 'OUTPUT'; +const QUALITY_PLACEHOLDER = 'QUALITY'; + +const MAX_QUALITY = 80; +const MIN_QUALITY = 50; const SIPS_HEIC_CONVERT_COMMAND_TEMPLATE = [ 'sips', @@ -56,6 +60,10 @@ const IMAGE_MAGICK_THUMBNAIL_GENERATE_COMMAND_TEMPLATE = [ INPUT_PATH_PLACEHOLDER, '-thumbnail', `${MAX_DIMENSION_PLACEHOLDER}x${MAX_DIMENSION_PLACEHOLDER}>`, + '-unsharp', + '0x.5', + '-quality', + QUALITY_PLACEHOLDER, OUTPUT_PATH_PLACEHOLDER, ]; @@ -153,22 +161,35 @@ function constructConvertCommand( export async function generateImageThumbnail( inputFilePath: string, - width: number + width: number, + maxSize: number ): Promise { let tempOutputFilePath: string; + let quality = MAX_QUALITY; try { tempOutputFilePath = await generateTempFilePath('thumb.jpeg'); + let thumbnail: Uint8Array; + do { + console.log( + 'generating thumbnail', + 'quality', + quality, + 'thumbnail', + thumbnail?.length + ); + await runThumbnailGenerationCommand( + inputFilePath, + tempOutputFilePath, + width, + quality + ); - await runThumbnailGenerationCommand( - inputFilePath, - tempOutputFilePath, - width - ); - - if (!existsSync(tempOutputFilePath)) { - throw new Error('output thumbnail file not found'); - } - const thumbnail = new Uint8Array(await readFile(tempOutputFilePath)); + if (!existsSync(tempOutputFilePath)) { + throw new Error('output thumbnail file not found'); + } + thumbnail = new Uint8Array(await readFile(tempOutputFilePath)); + quality -= 10; + } while (thumbnail.length > maxSize && quality > MIN_QUALITY); return thumbnail; } catch (e) { logErrorSentry(e, 'generate image thumbnail failed'); @@ -185,13 +206,15 @@ export async function generateImageThumbnail( async function runThumbnailGenerationCommand( inputFilePath: string, tempOutputFilePath: string, - maxDimension: number + maxDimension: number, + quality: number ) { const thumbnailGenerationCmd: string[] = constructThumbnailGenerationCommand( inputFilePath, tempOutputFilePath, - maxDimension + maxDimension, + quality ); const escapedCmd = shellescape(thumbnailGenerationCmd); log.info('running thumbnail generation command: ' + escapedCmd); @@ -200,7 +223,8 @@ async function runThumbnailGenerationCommand( function constructThumbnailGenerationCommand( inputFilePath: string, tempOutputFilePath: string, - maxDimension: number + maxDimension: number, + quality: number ) { let thumbnailGenerationCmd: string[]; if (isPlatform('mac')) { @@ -215,6 +239,9 @@ function constructThumbnailGenerationCommand( if (cmdPart === MAX_DIMENSION_PLACEHOLDER) { return maxDimension.toString(); } + if (cmdPart === QUALITY_PLACEHOLDER) { + return quality.toString(); + } return cmdPart; } ); diff --git a/src/utils/ipcComms.ts b/src/utils/ipcComms.ts index 56a9e565e..2bbe39452 100644 --- a/src/utils/ipcComms.ts +++ b/src/utils/ipcComms.ts @@ -150,7 +150,10 @@ export default function setupIpcComs( return deleteTempFile(tempFilePath); }); - ipcMain.handle('generate-image-thumbnail', (_, fileData, maxDimension) => { - return generateImageThumbnail(fileData, maxDimension); - }); + ipcMain.handle( + 'generate-image-thumbnail', + (_, fileData, maxDimension, maxSize) => { + return generateImageThumbnail(fileData, maxDimension, maxSize); + } + ); }