Skip to content

Instantly share code, notes, and snippets.

@mattupstate
Created August 22, 2012 00:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattupstate/3420867 to your computer and use it in GitHub Desktop.
Save mattupstate/3420867 to your computer and use it in GitHub Desktop.
Photoshop script to scale an image to fill a specified rectangle and preserve aspect ratio. Save this in <Photoshop Install Folder>/Presets/Scripts and restart Photoshop.
#target photoshop
main ();
function cloneRectangle(rect) {
return { x:rect.x, y:rect.y, width:rect.width, height:rect.height };
}
function rectangle(x, y, width, height) {
return { x:x, y:y, width:width, height:height };
}
function widthToHeight(size){
return size.width / size.height;
}
function heightToWidth(size) {
return size.height / size.width;
}
function scaleWidth(size, height) {
var scaled = cloneRectangle(size);
var ratio = widthToHeight( size );
scaled.width = height * ratio;
scaled.height = height;
return scaled;
}
function scaleHeight(size, width) {
var scaled = cloneRectangle(size);
var ratio = heightToWidth(size);
scaled.width = width;
scaled.height = width * ratio;
return scaled;
}
function scaleToFill(size, bounds) {
var scaled = scaleHeight(size, bounds.width);
if (scaled.height < bounds.height)
scaled = scaleWidth(size, bounds.height);
return scaled;
}
function main () {
if (app.documents.length < 1) {
alert ("No document open to resize.");
return;
}
var arHeight = 4;
var arWidth = 5;
var doc = app.activeDocument;
var docWidth = new UnitValue (doc.width, "px");
var docHeight = new UnitValue (doc.height, "px");
var dlg = new Window("dialog", "Form");
dlg.orientation = "row";
var labelGroup = dlg.add("group");
labelGroup.orientation = "column";
labelGroup.add("statictext", undefined, "Width:");
labelGroup.add("statictext", undefined, "Height:");
var inputGroup = dlg.add("group");
inputGroup.orientation = "column";
var widthInput = inputGroup.add("edittext", undefined, "");
widthInput.characters = 10;
widthInput.active = true;
var heightInput = inputGroup.add("edittext", undefined, "");
heightInput.characters = 10;
var buttonGroup = dlg.add("group");
buttonGroup.orientation = "column";
var okButton = buttonGroup.add("button", undefined, "OK");
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
okButton.onClick = function() {
var canvasWidth = new UnitValue(widthInput.text, "px");
var canvasHeight = new UnitValue(heightInput.text, "px");
var scaled = scaleToFill(rectangle(0, 0, docWidth, docHeight),
rectangle(0, 0, canvasWidth, canvasHeight));
doc.resizeImage(scaled.width, scaled.height);
dlg.hide();
};
dlg.show();
}
@RomanStone
Copy link

Modified To run For Each Open Tab:

#target photoshop

main ();

function cloneRectangle(rect) {
    return { x:rect.x, y:rect.y, width:rect.width, height:rect.height };
}

function rectangle(x, y, width, height) {
    return { x:x, y:y, width:width, height:height };
}

function widthToHeight(size){
    return size.width / size.height;
}

function heightToWidth(size) {
    return size.height / size.width;
}

function scaleWidth(size, height) {
    var scaled = cloneRectangle(size);
    var ratio = widthToHeight( size );
    scaled.width = height * ratio;
    scaled.height = height;
    return scaled;
}

function scaleHeight(size, width) {
    var scaled = cloneRectangle(size);
    var ratio = heightToWidth(size);
    scaled.width = width;
    scaled.height = width * ratio;
    return scaled;
}

function scaleToFill(size, bounds) {
    var scaled = scaleHeight(size, bounds.width);

    if (scaled.height < bounds.height)
        scaled = scaleWidth(size, bounds.height);

    return scaled;
}

function main () {
    if (app.documents.length < 1) {
        alert ("No document open to resize.");
        return;
    }

    var arHeight = 4;
    var arWidth = 5;
    var dlg;
    var labelGroup;
    var inputGroup;
    var widthInput;
    var heightInput;
    var buttonGroup;
    var okButton;
    var cancelButton;

    dlg = new Window("dialog", "Form");
    dlg.orientation = "row";

    labelGroup = dlg.add("group");
    labelGroup.orientation = "column";
    labelGroup.add("statictext", undefined, "Width:");
    labelGroup.add("statictext", undefined, "Height:");

    inputGroup = dlg.add("group");
    inputGroup.orientation = "column";

    widthInput = inputGroup.add("edittext", undefined, "");
    widthInput.characters = 10;
    widthInput.active = true;

    heightInput = inputGroup.add("edittext", undefined, "");
    heightInput.characters = 10;

    buttonGroup = dlg.add("group");
    buttonGroup.orientation = "column";
    okButton = buttonGroup.add("button", undefined, "OK");
    cancelButton = buttonGroup.add("button", undefined, "Cancel");

    okButton.onClick = function() {

        var i;
        var doc;
        var Cursor;
        var docWidth;
        var docHeight;
        var canvasWidth;
        var canvasHeight;
        var scaled;

        for (i = 0; i < documents.length; i++) {

            Cursor = app.documents[i];

            app.activeDocument = Cursor;

            doc = app.activeDocument;

            docWidth = new UnitValue (doc.width, "px");
            docHeight = new UnitValue (doc.height, "px");

            canvasWidth = new UnitValue(widthInput.text, "px");
            canvasHeight = new UnitValue(heightInput.text, "px");

            scaled = scaleToFill(rectangle(0, 0, docWidth, docHeight), rectangle(0, 0, canvasWidth, canvasHeight));

            doc.resizeImage(scaled.width, scaled.height);

        };

        dlg.hide();
    };

    dlg.show();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment