Skip to main content
mauricior6328708
Inspiring
November 29, 2016
Question

How to resize an image inside a selection!

  • November 29, 2016
  • 2 replies
  • 3006 views

Good afternoon guys, everyone salutes you!

It would be possible for a script to resize an image within a selection based on height so that the image does not distort, thus maintaining the same aspect ratio. Is that I created an action to exchange two images inside the document, however there is not cool when they are images of different sizes and with this script, I can solve this problem. Thank you!

This topic has been closed for replies.

2 replies

Inspiring
November 30, 2016

Here's a script I wrote a while back to deal with various ways of scaling to fit a selection.

app.preferences.rulerUnits = Units.PIXELS

app.preferences.typeUnits = TypeUnits.PIXELS

    var scriptName = "NinjaScript FitSelection";

    var scriptVersion = "0.1";

function boundsToSize (inBounds)

{

    return [inBounds[2] - inBounds[0], inBounds[3] - inBounds[1]];

}

function boundsToPivot (inBounds)

{

    return [(inBounds[2] + inBounds[0])/2, (inBounds[3] + inBounds[1])/2]

}

function fitScale()

{

    activeDocument.activeLayer.resize(sizeSL[0]/sizeAL[0]* 100, sizeSL[1]/sizeAL[1]* 100)

    activeDocument.activeLayer.translate (pivotSL[0] - pivotAL[0] ,   pivotSL[1] - pivotAL[1])

}

function fitWidth()

{

    activeDocument.activeLayer.resize(sizeSL[0]/sizeAL[0]* 100, sizeSL[0]/sizeAL[0]* 100)

    activeDocument.activeLayer.translate (pivotSL[0] - pivotAL[0] ,   pivotSL[1] - pivotAL[1])

}

function fitHeight()

{

    activeDocument.activeLayer.resize(sizeSL[1]/sizeAL[1]* 100, sizeSL[1]/sizeAL[1]* 100)

    activeDocument.activeLayer.translate (pivotSL[0] - pivotAL[0] ,   pivotSL[1] - pivotAL[1])

}

function fitInside()

{

    var inSize = Math.min(sizeSL[0]/sizeAL[0]* 100, sizeSL[1]/sizeAL[1]* 100);

   

   

    activeDocument.activeLayer.resize(inSize, inSize)

    activeDocument.activeLayer.translate (pivotSL[0] - pivotAL[0] ,   pivotSL[1] - pivotAL[1])

}

activeDocument.suspendHistory("NS_FitSelection", "main();")

function main()

{

    try

    {    activeDocument.selection.bounds}

    catch(e)

    { alert ("Error, no selection made"); return;}

sizeAL = boundsToSize(activeDocument.activeLayer.bounds);

sizeSL = boundsToSize(activeDocument.selection.bounds);

pivotAL = boundsToPivot(activeDocument.activeLayer.bounds);

pivotSL = boundsToPivot(activeDocument.selection.bounds);

   

    dialogFitSelection();

}

function dialogFitSelection()

{

    var thisDialog = new Window("dialog", scriptName + " v" + scriptVersion);

    thisDialog.orientation = 'column';

    thisDialog.alignChildren = 'center';

    thisDialog.alignment = 'center';

   

    var groupOptions = thisDialog.add("group");

        var radioFitInside = groupOptions.add("radiobutton", undefined, "Fit Inside");

        radioFitInside.value = true;

        var radioFitScale = groupOptions.add("radiobutton", undefined, "Fit Stretch");

        var radioFitHeight = groupOptions.add("radiobutton", undefined, "Fit Height");

        var radioFitWidth = groupOptions.add("radiobutton", undefined, "Fit Width");

   

     var groupButtons = thisDialog.add("group");

        myOkButton = groupButtons.add("button", undefined, "Ok",{name: "ok"});

        myOkButton.onClick = function()

        {

            if (radioFitInside.value) fitInside();

            if (radioFitScale.value) fitScale();

            if (radioFitWidth.value) fitWidth();       

            if (radioFitHeight.value) fitHeight();

            thisDialog.close();

        }

        myCancelButton = groupButtons.add("button", undefined, "Cancel",{name: "cancel"});

        myCancelButton.onClick = function()

        {

            thisDialog.close();

        }

   

   

        thisDialog.center();

        var result = thisDialog.show();  

}

JJMack
Community Expert
Community Expert
November 30, 2016

There is a nasty bug in Photoshop scripting that I reported to Adobe. Adobe acknowledged they can reproduce the problem but Adobe does not fix it. Every version of Photoshop I have tested has the bug.  This bug bite the script you posted.  If you suspend history state recording. Have an active selection and resize/move a layer Photoshop will back up a one history state prior to when you suspended history state recording.  Your script also seems to have other problem. It does not seem to resize smart object layers correctly. Here is the nasty bug report on file.

Photoshop: Script bug resize layer back document up one history state | Photoshop Family Customer Community

Here what I see running your script the last thing in the history state stack before running your script is Fill.  I filled the selection with gray in a layer under the image layer targeted the image layer and ran your script and used fit inside radio button.  Note the gray fill was removed. The image was aligned to the selection however it was not resized to fit within the selection. Your script hit the bug twicd Two previous steps are lost.

JJMack
mauricior6328708
Inspiring
December 1, 2016

Great script EnsilZah! It's very close to what I really need. You could delete the dialog box and execute option 3 "Fit Height" directly. That's all I need. Thank you!

mauricior6328708
Inspiring
November 29, 2016

Remembering that the script is to resize in the selection based on its height, respecting the same proportion, only that!!

JJMack
Community Expert
Community Expert
November 30, 2016

If you have a selection you can fit an image to that area in at least two way.  One way is to resize the image to fit with in the selection bounds the other was is to insure the resize image fills the selection bounds. In ant case the resize image needs to be masked by the selection for a selection  can have any shape, have feathering and  ever have none contiguous areas like the second selection you show number 3.  I prefer to fill the selected area bounds as shown in this video http://www.mouseprints.net/old/dpr/Populate911_720p.mp4

JJMack