Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revisionBoth sides next revision | ||
graphic:javascript:photoshop [2023/03/05 02:58] – [Doc operation code] ying | graphic:javascript:photoshop [2023/03/05 02:59] – [Doc operation code] ying | ||
---|---|---|---|
Line 287: | Line 287: | ||
</ | </ | ||
* resize doc <code javascript> | * resize doc <code javascript> | ||
+ | * resize to A4 size ratio <code javascript> | ||
+ | /* | ||
+ | reisize to fit A4 | ||
+ | by ying n chatGPT | ||
+ | 2023.03.05 | ||
+ | */ | ||
+ | // Get the active document | ||
+ | var doc = app.activeDocument; | ||
+ | app.backgroundColor.rgb.red = 255; | ||
+ | app.backgroundColor.rgb.green = 255; | ||
+ | app.backgroundColor.rgb.blue = 255; | ||
+ | // Define the A4 size in pixels (assuming 300 DPI) | ||
+ | var a4Width = 2480; // 8.27 inches * 300 pixels per inch | ||
+ | var a4Height = 3508; // 11.69 inches * 300 pixels per inch | ||
+ | var a4Ratio = a4Width / a4Height; | ||
+ | |||
+ | // Get the current canvas size | ||
+ | var currentWidth = doc.width.value; | ||
+ | var currentHeight = doc.height.value; | ||
+ | var currentRatio = currentWidth / currentHeight; | ||
+ | |||
+ | // Calculate the new canvas size | ||
+ | var newWidth = currentWidth; | ||
+ | var newHeight = currentHeight; | ||
+ | if (currentRatio > a4Ratio) { | ||
+ | // Extend the height to match the A4 ratio | ||
+ | newHeight = Math.round(newWidth / a4Ratio); | ||
+ | } else { | ||
+ | // Extend the width to match the A4 ratio | ||
+ | newWidth = Math.round(newHeight * a4Ratio); | ||
+ | } | ||
+ | |||
+ | // Only resize the canvas if the new size is larger than the current size | ||
+ | if (newWidth > currentWidth || newHeight > currentHeight) { | ||
+ | // Calculate the position of the current content | ||
+ | var x = Math.round((newWidth - currentWidth) / 2); | ||
+ | var y = Math.round((newHeight - currentHeight) / 2); | ||
+ | |||
+ | // Resize the canvas | ||
+ | doc.resizeCanvas(newWidth, | ||
+ | } | ||
+ | |||
+ | </ | ||
* file and folder <code javascript> | * file and folder <code javascript> | ||
var samplesFolder = Folder(app.path + "/ | var samplesFolder = Folder(app.path + "/ |