Skip to content

Instantly share code, notes, and snippets.

@lm913
Last active December 18, 2015 22:06
Show Gist options
  • Save lm913/dd799e095628ed30af0a to your computer and use it in GitHub Desktop.
Save lm913/dd799e095628ed30af0a to your computer and use it in GitHub Desktop.
Check the canvas against given size requirements and makes the necessary adjustments
// enable double clicking
#target photoshop
app.bringToFront();
var doc = activeDocument;
// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startDisplayDialogs = app.displayDialogs
var startBgColor = app.backgroundColor
// Set Adobe Photoshop CC 2015 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.displayDialogs = DialogModes.NO
app.backgroundColor.rgb.hexValue = "ffffff"
var startWidth = doc.width;
var startHeight = doc.height;
var startRes = doc.resolution;
var startProfile = doc.colorProfileName;
var endWidth = 300;
var endHeight = 200;
var endRes = 72;
var endProfile = "sRGB IEC61966-2.1"
var startRatio = startWidth / startHeight;
var endRatio = endWidth / endHeight;
doc.backgroundLayer.duplicate();
if (startRes != endRes){
doc.resizeImage(startWidth, startHeight, endRes, ResampleMethod.AUTOMATIC);
}
if (endRatio < startRatio){
if (endHeight > endWidth){
doc.resizeCanvas(null, (1 / endRatio) * startHeight, AnchorPosition.MIDDLECENTER);
}
} else {
doc.resizeCanvas(endRatio * startWidth, null, AnchorPosition.MIDDLECENTER);
}
app.activeDocument.convertProfile(endProfile, Intent.RELATIVECOLORIMETRIC, true, true );
// Reset the application preferences
app.preferences.rulerUnits = startRulerUnits
app.displayDialogs = startDisplayDialogs
app.backgroundColor = startBgColor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment