Skip to main content
Participating Frequently
January 4, 2022
Question

How to lock canvas resizing

  • January 4, 2022
  • 5 replies
  • 2490 views

How do we lock the resize of canvas so that if I enter a value in the width bos the height box should automatically be populated and vice versa.

See that attached image. In this case if I enter 700 in the width box, the height should be automatically populated with 700. Is it possible? If yes how to do it?

This topic has been closed for replies.

5 replies

Stephen Marsh
Community Expert
Community Expert
January 5, 2022

The following script provides an interactive square crop, based on the selection size entered in pixels.

 

Only two user interactions are required:

 

1) Entering a desired square selection size in pixel values (an integer)

 

2) Don't resize the transform selection edges, just move the entire selection to where the crop should be performed, then press the enter/return key or press the "tick" in the options bar for "Commit transform", then the script will continue to automatically crop the image.

 

/*
Interactive square canvas crop.jsx
Stephen Marsh, v1.0 - 5th January 2022
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-lock-canvas-resizing/m-p/12634283
*/

#target photoshop

function main() {

    var savedRuler = app.preferences.rulerUnits;

    app.preferences.rulerUnits = Units.PIXELS;

    var inputSelection;
    while (isNaN(inputSelection = prompt("Enter a square selection size (pixels):", "")));

    if (inputSelection === null) {
        alert('Script cancelled!');
        return;
    }

    while (inputSelection.length === 0) {
        inputSelection = prompt("Blank value not accepted! Enter a square selection size (pixels):", "");
    }

    var finalSelection = parseInt(inputSelection);

    app.activeDocument.selection.select( [ [0,0], [0,finalSelection], [finalSelection,finalSelection], [finalSelection,0] ] );

    centerSelectionHack();

    interactiveTransformSelection();

    cropToSelection();

    app.preferences.rulerUnits = savedRuler;

}

app.activeDocument.suspendHistory("Square canvas crop", "main()");


// Functions

function interactiveTransformSelection() {
    var idtransform = stringIDToTypeID("transform");
    var desc2058 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref311 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idselection = stringIDToTypeID("selection");
    ref311.putProperty(idchannel, idselection);
    desc2058.putReference(idnull, ref311);
    var idfreeTransformCenterState = stringIDToTypeID("freeTransformCenterState");
    var idquadCenterState = stringIDToTypeID("quadCenterState");
    var idQCSAverage = stringIDToTypeID("QCSAverage");
    desc2058.putEnumerated(idfreeTransformCenterState, idquadCenterState, idQCSAverage);
    var idoffset = stringIDToTypeID("offset");
    var desc2059 = new ActionDescriptor();
    var idhorizontal = stringIDToTypeID("horizontal");
    var idpixelsUnit = stringIDToTypeID("pixelsUnit");
    desc2059.putUnitDouble(idhorizontal, idpixelsUnit, 0);
    var idvertical = stringIDToTypeID("vertical");
    var idpixelsUnit = stringIDToTypeID("pixelsUnit");
    desc2059.putUnitDouble(idvertical, idpixelsUnit, 0);
    var idoffset = stringIDToTypeID("offset");
    desc2058.putObject(idoffset, idoffset, desc2059);
    var idinterfaceIconFrameDimmed = stringIDToTypeID("interfaceIconFrameDimmed");
    var idinterpolationType = stringIDToTypeID("interpolationType");
    var idnearestNeighbor = stringIDToTypeID("nearestNeighbor");
    desc2058.putEnumerated(idinterfaceIconFrameDimmed, idinterpolationType, idnearestNeighbor);
    executeAction(idtransform, desc2058, DialogModes.ALL);
}

function centerSelectionHack() {
    
    /* It gets the job done, but surely there is a more elegant way...
    I couldn't get selection.translate() to work as intended */

    // Create solid colour layer from active selection
    var idmake = stringIDToTypeID("make");
    var desc2180 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref365 = new ActionReference();
    var idcontentLayer = stringIDToTypeID("contentLayer");
    ref365.putClass(idcontentLayer);
    desc2180.putReference(idnull, ref365);
    var idusing = stringIDToTypeID("using");
    var desc2181 = new ActionDescriptor();
    var idtype = stringIDToTypeID("type");
    var desc2182 = new ActionDescriptor();
    var idcolor = stringIDToTypeID("color");
    var desc2183 = new ActionDescriptor();
    var idred = stringIDToTypeID("red");
    desc2183.putDouble(idred, 255.000000);
    var idgrain = stringIDToTypeID("grain");
    desc2183.putDouble(idgrain, 255.000000);
    var idblue = stringIDToTypeID("blue");
    desc2183.putDouble(idblue, 255.000000);
    var idRGBColor = stringIDToTypeID("RGBColor");
    desc2182.putObject(idcolor, idRGBColor, desc2183);
    var idsolidColorLayer = stringIDToTypeID("solidColorLayer");
    desc2181.putObject(idtype, idsolidColorLayer, desc2182);
    var idcontentLayer = stringIDToTypeID("contentLayer");
    desc2180.putObject(idusing, idcontentLayer, desc2181);
    executeAction(idmake, desc2180, DialogModes.NO);
    // Fill opacity to 0%
    app.activeDocument.activeLayer.fillOpacity = 0;
    // Centre active layer on canvas
    app.activeDocument.activeLayer.translate(app.activeDocument.width / 2 - (app.activeDocument.activeLayer.bounds[0] + app.activeDocument.activeLayer.bounds[2]) / 2,
    app.activeDocument.height / 2 - (app.activeDocument.activeLayer.bounds[1] + app.activeDocument.activeLayer.bounds[3]) / 2);
    // Set selection to mask channel
    var idset = stringIDToTypeID("set");
    var desc2554 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref429 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idselection = stringIDToTypeID("selection");
    ref429.putProperty(idchannel, idselection);
    desc2554.putReference(idnull, ref429);
    var idto = stringIDToTypeID("to");
    var ref430 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idchannel = stringIDToTypeID("channel");
    var idmask = stringIDToTypeID("mask");
    ref430.putEnumerated(idchannel, idchannel, idmask);
    desc2554.putReference(idto, ref430);
    executeAction(idset, desc2554, DialogModes.NO);
    // Remove temp solid colour layer
    app.activeDocument.activeLayer.remove();
}

function cropToSelection() {
    var idcrop = stringIDToTypeID("crop");
    executeAction(idcrop, undefined, DialogModes.NO);
    // Deselect
    app.activeDocument.selection.deselect();
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not word-processor)
  3. Paste the code in
  4. Save the text file as .txt
  5. Rename the text .txt to .jsx
  6. Install or browse to the .jsx file to run

 

If these simple instructions are too abbreviated, you may need to read on...

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Stephen Marsh
Community Expert
Community Expert
January 4, 2022

Here is a script to resize the canvas height using the width value from middle centre:

 

/*
Square canvas resize.jsx
Stephen Marsh, v1.0 - 5th January 2022
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-lock-canvas-resizing/m-p/12634283
*/

#target photoshop

function main() {

    var savedRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    var doc = app.activeDocument;

    while (isNaN(squareCanvasSize = prompt("Square canvas size from middle center (px):", "")));

    if (squareCanvasSize === null) {
        //alert('Script cancelled!');
        return
    }

    while (squareCanvasSize.length === 0) {
        squareCanvasSize = prompt("Blank value not accepted! Square canvas size from middle center (px):", "");
    }

    var squareInteger = parseInt(squareCanvasSize);

    doc.resizeCanvas(squareInteger, squareInteger, AnchorPosition.MIDDLECENTER);
    app.preferences.rulerUnits = savedRuler;
}

app.activeDocument.suspendHistory("Square canvas resize", "main()");

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Participating Frequently
January 4, 2022

How am I suppose to run it? There was no Scripts folder inside Presets so I created it and copied scrip there. It should be showing under File -> Script? Or how to run it now?

Stephen Marsh
Community Expert
Community Expert
January 4, 2022

Full instructions were provided in the link under the script code. Apart from repeating them I don't have anything further to add or say differently.

 

EDIT:

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not word-processor)
  3. Paste the code in
  4. Save the text file as .txt
  5. Rename the text .txt to .jsx
  6. Install or browse to the .jsx file to run

 

If these simple instructions are too abbreviated, you may need to read on...

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Mohammad.Harb
Community Expert
Community Expert
January 4, 2022

You can post a feature request in below link 

https://www.adobe.com/products/wishform.html

Chris 486
Community Expert
Community Expert
January 4, 2022

Hi Mike!

 

I'm not aware of a function in that window pane to make both inputs the same when you type in one. By chance, are you cropping the images from the same anchor point and the same size each time? If so you could turn that step into an recorded action and make it a one button click rather than opening up the menu each time. There may be a way to do a similar option through the crop tool and actions as well.

Participating Frequently
January 4, 2022

Unfortunately it's all different image sizes so actions will not help. 

Stephen Marsh
Community Expert
Community Expert
January 4, 2022

Is the anchor point always set to centre?

 

Is there a mathematical rule or formula that could be automated?

Stephen Marsh
Community Expert
Community Expert
January 4, 2022

That is what the link icon does in Image > Image Size. 

 

Canvas size crops if a lesser value is entered, the width and height fields are independent by design.

 

This could be scripted if you really wish to crop content from 800px to 700px square rather than resize.

Participating Frequently
January 4, 2022

Yes, I'm aware of image resize. I'm asking about canvas resize. I'm cropping images in bulk and I need to adjust to square for which I've to enter values for width and height in duplicate. If there is a way for one value to be automatically locked to other value then it will save few keystrokes per image.

jane-e
Community Expert
Community Expert
January 4, 2022

@MikeLondon22 

 

Just an idea; will it work for you to change the setting to Relative and type a number there?

 

Jane