Photoshop Resize while maintaining aspect ratio

Photoshop’s inbuilt batch manipulation tools leave a lot to be desired, especially when dealing with a large quantity of images that are both portrait and landscape.

This script recursively goes through folders, finds images, opens, trims background, resizes to 1100×1100 in longest axis, increases canvas size to 1200×1200 to give border, saves to web in Jpeg quality:10

//</p>
// Wulf Solter - 2013-10-27
// Trim, resize to 1100x1100, canvas to 1200 and center.

// Uncomment the next line to show the debugger
//$.level = 1; debugger;

// Save current dialog preferences
var startDisplayDialogs = app.displayDialogs;

// Don't display dialogs
app.displayDialogs = DialogModes.NO;

// Constants
var fHeight = 1100;
var fWidth = 1100;
var opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.JPEG;
opts.quality = 10;
//var FILE_TYPE = ".jpg"; // The type of files that this script works on -- you can change
var SEARCH_MASK = "*"; //+ FILE_TYPE; // Image file filter to find only those files

// Set of exception strings for error handling
var X_NOINPUT = "noInput";
var X_BADDOC = "badDoc";
var X_WERROR = "writeError";
var X_CERROR = "closeError";

try {
var inputFolder = Folder.selectDialog("Select a folder to process");
if (inputFolder == null) throw X_NOINPUT;
processSubs(inputFolder);
} catch (exception) {
// Show degbug message and then quit
alert(exception);
} finally {
// Reset app preferences
app.displayDialogs = startDisplayDialogs;
}

//=================================== FUNCTIONS =========================================
function processSubs(theFolder) {
var myFileList = theFolder.getFiles();
//$.writeln(myFileList);
// Open each file in turn
for (var i = 0; i &lt; myFileList.length; i++) {
// Only want to open non-hidden files (and no folders)
if ((myFileList[i] instanceof File) &amp;&amp; (myFileList[i].hidden == false)) {
// Open the file in Photoshop
stirlingize(myFileList[i]);
}
if ((myFileList[i] instanceof Folder)) {
// subfolder so recurse
processSubs(myFileList[i]);
}
}
}

function stirlingize(filename) {
var docRef = open(filename);
if (docRef == null) throw X_BADDOC;

//Trim the document with
// top=true, left=true, bottom=true, right=true
app.activeDocument.trim(TrimType.TOPLEFT, true, true, true, true)

// Resize on portrait/landscape
if (app.activeDocument.height &gt; app.activeDocument.width) {
app.activeDocument.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
} else {
app.activeDocument.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

// resize Canvas to 1200px square
app.activeDocument.resizeCanvas("1200 px", "1200 px");

// Save the file with options set in constants above!
jpgFile = new File( filename + "-resized.jpg" )
app.activeDocument.exportDocument(jpgFile, ExportType.SAVEFORWEB, opts);

// and close this document
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

function GetSubFolders(theFolder) {
var myFileList = theFolder.getFiles();
for (var i = 0; i &lt; myFileList.length; i++) {
var myFile = myFileList[i];
if (myFile instanceof Folder){
GetSubFolders(myFile);
} else if (myFile instanceof File &amp;&amp; myFile.name.match(/\.indd$/i)) {
myFiles.push(myFile);
}
}
}
//--------------------------------------------------------------------------------------------------------------

&nbsp;
<p dir="ltr">

Save in /Applications/<Photoshop App>/Presets/Scripts/<anyname>.jsx

Reboot Photoshop and then you’ll find the script in your File menu under Scripts.

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

*