Skip to main content
Participating Frequently
October 3, 2024
Open for Voting

Enhance Resizing Workflow with Fit-to-Canvas Option

  • October 3, 2024
  • 12 replies
  • 2009 views

When inserting a larger image that exceeds the canvas area, pressing Ctrl+T (for resizing) should have the default position set to the top-left corner (or remember the previous position).

In the resize function, there should be an additional button to automatically scale the entire image to fit the canvas size, instead of just having rotate and flip options. This would be a much more useful feature than rotate and flip.

The current resizing options are limiting. A "Fit to Canvas" feature would be much more practical and accessible for everyday use than rotate and flip.

This function would streamline workflows and save time for users who frequently need to resize images. Many users, especially those working with large or multiple images, face this challenge. It would benefit a broad user base, not just a niche group.

12 replies

Stephen Marsh
Community Expert
Community Expert
October 3, 2024

A scripted option, obviously not as convenient as the transform toolbar but better than nothing for now:

 

 

/*
Resize Layer to Canvas scriptUI GUI.jsx
v1.0 - 3rd October 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/enhance-resizing-workflow-with-fit-to-canvas-option/idc-p/14895563
Inspiration from:
https://forums.adobe.com/message/5413957
https://forums.adobe.com/thread/1968642
https://forums.adobe.com/message/8022190
https://gist.github.com/jawinn/ab4df1c33d0743e41fd3
https://gist.githubusercontent.com/jawinn/ab4df1c33d0743e41fd3/raw/72ff297ae42ed029c86be7644bd021ef46971070/Fit%2520Layer%2520To%2520Canvas.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/trying-to-do-text-to-fit-the-width-of-the-canvas-dimensions/m-p/11589399
*/

#target photoshop

// Check if there's an active document
if (app.documents.length > 0) {

    var win = new Window("dialog", "Resize Layer to Canvas (v1.0)");

    // Dropdown for resize options
    var resizeGroup = win.add("group");
    resizeGroup.add("statictext", undefined, "Resize Option:");
    var resizeDropdown = resizeGroup.add("dropdownlist", undefined, [
        "Maintain Aspect Ratio",
        "Fill Canvas (may crop)",
        "Fit to Canvas (may distort)"
    ]);
    resizeDropdown.selection = 0; // Default to maintain aspect ratio

    // OK and Cancel buttons, right-aligned
    var buttonGroup = win.add("group");
    buttonGroup.alignment = "right"; // Right-align the button group
    var cancelButton = buttonGroup.add("button", undefined, "Cancel");
    var okButton = buttonGroup.add("button", undefined, "OK");

    okButton.onClick = function () {
        win.close();
        fitLayerToCanvas(resizeDropdown.selection.index);
    }

    cancelButton.onClick = function () {
        win.close();
    }

    win.show();

    function fitLayerToCanvas(resizeOption) {
        if (app.documents.length > 0) {
            app.activeDocument.suspendHistory('Fit Layer to Canvas', 'FitLayerToCanvas(' + resizeOption + ')');
        }
    }

    function FitLayerToCanvas(resizeOption) {
        var doc = app.activeDocument;
        var layer = doc.activeLayer;

        // do nothing if layer is background or locked
        if (layer.isBackgroundLayer || layer.allLocked || layer.pixelsLocked ||
            layer.positionLocked || layer.transparentPixelsLocked) return;

        // do nothing if layer is not normal artLayer or Smart Object
        if (layer.kind != LayerKind.NORMAL && layer.kind != LayerKind.SMARTOBJECT) return;

        // store the ruler
        var defaultRulerUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var width = doc.width.as('px');
        var height = doc.height.as('px');
        var bounds = layer.bounds;
        var layerWidth = bounds[2].as('px') - bounds[0].as('px');
        var layerHeight = bounds[3].as('px') - bounds[1].as('px');

        // Calculate new dimensions
        var newWidth, newHeight;
        var layerRatio = layerWidth / layerHeight;
        var canvasRatio = width / height;

        switch (resizeOption) {
            case 0: // Maintain Aspect Ratio
                if (layerRatio > canvasRatio) {
                    newWidth = width;
                    newHeight = newWidth / layerRatio;
                } else {
                    newHeight = height;
                    newWidth = newHeight * layerRatio;
                }
                break;
            case 1: // Fill Canvas (may crop)
                var scale = Math.max(width / layerWidth, height / layerHeight);
                newWidth = layerWidth * scale;
                newHeight = layerHeight * scale;
                break;
            case 2: // Fit to Canvas (may distort)
                newWidth = width;
                newHeight = height;
                break;
        }

        // Resize the layer
        var resizePercentX = newWidth / layerWidth * 100;
        var resizePercentY = newHeight / layerHeight * 100;
        layer.resize(resizePercentX, resizePercentY, AnchorPosition.TOPLEFT);

        // Always center the layer
        var newX = (width - newWidth) / 2;
        var newY = (height - newHeight) / 2;

        // Move the layer to the new position
        layer.translate(new UnitValue(newX - layer.bounds[0].as('px'), 'px'), new UnitValue(newY - layer.bounds[1].as('px'), 'px'));

        // restore the ruler
        app.preferences.rulerUnits = defaultRulerUnits;
    }

} else {
    alert("A document must be open to run this script!");
}

 

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

 

 

Stephen Marsh
Community Expert
Community Expert
October 3, 2024

@Balázs28016806xdji 

 

How would you propose different aspect ratios and proportions be handled when fitting the oversized layer content to the canvas?

 

If the layer was landscape orientation and the canvas was portrait, would the resize fit to the canvas width, leaving blank space at the top and bottom (letterboxing)? Or would the landscape content be sized to the height of the portrait canvas, with the wider sides "cropping" or falling off to the sides of the canvas? And vice/versa for portrait layers on a landscape canvas.