Script to resize and resample image to longest side
Copy link to clipboard
Copied
Hi friends,
I have various portrait and landscape images that need to be resized to the longest side (150mm) while also resamping the image to preserve details.
File > Automate > Image Fit... works to constrain proportions but it does not resample the image size to preserve details, so I end up losing resolution.
Creating separate Actions to resize landscape and portrait to 150mm using Image Size is my current solution but I would like to simplify this so I can skip manually selecting portrait or landscape and batch my files faster.
Is there a script available that will help me batch my files to both resize any image and resample them?
Thanks for reading!
Explore related tutorials & articles
Copy link to clipboard
Copied
Here is something to take a look at, work on a copy of the original for safety. I hacked it from another script, you can ignore the suffix if you wish. The GUI can be removed and the resize value hard-coded into the script for automation. Just use this as a proof of concept for now:
if (app.documents.length !== 0) {
// ScriptUI version
// https://forums.adobe.com/message/11151550#11151550
win = new Window("dialog", "Flatten Dupe, Rename & Resize", undefined, {
closeButton: false
});
win.orientation = "column";
win.alignChildren = ["center", "top"];
win.spacing = 10;
win.margins = 16;
var statictext1 = win.add("statictext");
statictext1.text = "Enter filename suffix";
statictext1.alignment = ["left", "top"];
var edittext1 = win.add("edittext");
edittext1.text = ""; //Add text between the quotes for a preset suggestion
edittext1.preferredSize.width = 300;
edittext1.preferredSize.height = 25;
var statictext2 = win.add("statictext");
statictext2.text = "Enter longest edge in MM";
statictext2.alignment = ["left", "top"];
var edittext2 = win.add("edittext");
edittext2.text = ""; // Add a numerical value for a preset suggestion
edittext2.preferredSize.width = 300;
edittext2.preferredSize.height = 25;
baton10 = win.add("group");
var button1 = baton10.add("button", undefined, undefined, { name: "Cancel" });
button1.text = "Cancel";
button1.preferredSize.width = 90;
button1.justify = "center";
button1.alignment = ["left", "top"];
var button2 = baton10.add("button", undefined, undefined, { name: "Ok" });
button2.text = "OK";
button2.preferredSize.width = 90;
button2.justify = "center";
button2.alignment = ["right", "top"];
button2.onClick = function () {
TEST_A()
close = true;
win.close();
};
win.show();
// FUNCTION
function TEST_A() {
(function () {
// Dupe Original File with -Suffix.jsx
var origDoc = app.activeDocument;
var baseName = origDoc.name.replace(/\.[^\.]+$/, '');
var sep = String("-");
// Add scriptUI input for the suffix variable
var suffix = edittext1.text
// https://forums.adobe.com/message/11043796
if (suffix === null) { return } // Test if CANCEL returns null, then do nothing
if (suffix === "") { return } // Test if an empty string is returned, then do nothing
var dupeName = baseName + sep + suffix;
origDoc.duplicate((dupeName), true);
app.activeDocument.flatten();
// Optional Step Below: remove the // comment characters to enable saving the original file with or without changes
// origDoc.close(SaveOptions.DONOTSAVECHANGES); // (SaveOptions.SAVECHANGES)
})();
// https://forums.adobe.com/thread/1031437
// make document square with the document’s longer side’s length;
// 2012, use it at your own risk;
(function () {
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.MM;
// Add scriptUI input to the longest edge variable
var longestEdge = "" + edittext2.text;
// https://forums.adobe.com/message/11043796
if (longestEdge === null) { return } // Test if CANCEL returns null, then do nothing
if (longestEdge === "") { return } // Test if an empty string is returned, then do nothing
var resizeInput = longestEdge.replace(/[^\d.]/g, '') // Remove anything that is not a digit or decimal point
if (resizeInput === "") { return } // Test if an empty string is returned, then do nothing
// https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/
var resizeValue = parseInt(resizeInput, 10); // Assuming that parseInt is more appropriate than parseFloat
// get longer side
var theFactor = resizeValue / getTheLonger(Number(myDocument.width), Number(myDocument.height));
// resize
myDocument.resizeImage(myDocument.width * theFactor, myDocument.height * theFactor, myDocument.resolution, ResampleMethod.PRESERVEDETAILS, 0);
// reset ruler units to original
preferences.rulerUnits = originalRulerUnits;
} else { }
function getTheLonger(a, b) {
if (a >= b) {
var x = a
} else {
var x = b
}
return x;
}
})();
}
} else {
alert('You must have a document open!');
}
Copy link to clipboard
Copied
This cut-down version has no GUI and uses 150mm for the longest edge. Again, work on copies until you know that it is 100% working as expected.
#target photoshop
(function () {
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.MM;
var longestEdge = 150; // 150mm
// get longer side
var theFactor = longestEdge / getTheLonger(Number(myDocument.width), Number(myDocument.height));
// resize
myDocument.resizeImage(myDocument.width * theFactor, myDocument.height * theFactor, myDocument.resolution, ResampleMethod.PRESERVEDETAILS, 0);
// reset ruler units to original
preferences.rulerUnits = originalRulerUnits;
} else { }
function getTheLonger(a, b) {
if (a >= b) {
var x = a
} else {
var x = b
}
return x;
}
})();
Copy link to clipboard
Copied
Try »Insert Conditional« from the fly-out menu of the Actions Panel.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi Oomep_
the fit image script you get when going to File > Automate > Image Fit... works by applying bicubic resampling.
If you want, you can search on your computer for the Fit Image.jsx file.
On MacOS this is located here: /Applications/Adobe Photoshop CC 2019/Presets/Scripts/Fit Image.jsx
On line 135 you should see the following:
app.activeDocument.resizeImage(newWidth, newHeight, resolution, ResampleMethod.BICUBIC);
It suffices to change "ResampleMethod.BICUBIC" to "ResampleMethod.PRESERVEDETAILS", saving the file and running the action as you already have it.
Note that this only works on your computer, that is to say: every computer where you wan tthis to work needs to update/change their Fit Image.jsx file.
Alternatively, if you feel like a project, you could also modify the Fit Image script to include a dropdownlist with all the resampling methods... Bit more work though. 🙂
All the best!

