From 2c4e832c160b2b8f8f2fcfa35436ab21a18dfaad Mon Sep 17 00:00:00 2001 From: Abdullah Alfaraj Date: Wed, 11 Jan 2023 00:39:39 +0300 Subject: [PATCH] get final image width and height from the selection area --- index.js | 7 +++++++ psapi.js | 2 +- selection.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 selection.js diff --git a/index.js b/index.js index e03058c..818fd77 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,13 @@ const dialog_box = require('./dialog_box') const html_manip = require('./html_manip') const export_png = require('./export_png') const viewer = require('./viewer') +const selection = require('./selection') + + +const eventHandler = (event, descriptor) => { + console.log("event got triggered!") + console.log(event, descriptor)} +require("photoshop").action.addNotificationListener(['all'], eventHandler); async function getUniqueDocumentId () { diff --git a/psapi.js b/psapi.js index c4324a2..d8d2bf3 100644 --- a/psapi.js +++ b/psapi.js @@ -829,7 +829,7 @@ async function createClippingMaskExe () { async function checkIfSelectionAreaIsActive() { - let isSelectionAreaValid = getSelectionInfoExe() + let isSelectionAreaValid = await getSelectionInfoExe() return isSelectionAreaValid } async function saveUniqueDocumentIdExe (new_id) { diff --git a/selection.js b/selection.js new file mode 100644 index 0000000..adbe743 --- /dev/null +++ b/selection.js @@ -0,0 +1,43 @@ + +function finalWidthHeight(selectionWidth,selectionHeight,minWidth,minHeight){ + // const minWidth = 512 + // const minHeight = 512 + + // const selectionWidth = 256 + // const selectionHeight = 1000 + + let finalWidth = 0 + let finalHeight = 0 + + if(selectionWidth <= selectionHeight){ + //do operation on the smaller dimension + const scaleRatio = selectionWidth / minWidth + + finalWidth = minWidth + finalHeight = selectionHeight / scaleRatio + }else{ + const scaleRatio = selectionHeight / minHeight + + finalHeight = minHeight + finalWidth = selectionWidth / scaleRatio +} + return [finalWidth,finalHeight] +} + +async function selectionToFinalWidthHeight(){ + const {getSelectionInfoExe} = require('./psapi') + try{ + + const selectionInfo = await getSelectionInfoExe() + const [finalWidth,finalHeight] = finalWidthHeight(selectionInfo.width,selectionInfo.height,512,512) + + return [parseInt(finalWidth),parseInt(finalHeight)] + }catch(e){ + console.warn("you need a rectangular selection",e) + } +} + +module.exports = { +finalWidthHeight, +selectionToFinalWidthHeight +} \ No newline at end of file