8 Replies Latest reply on Dec 1, 2016 4:05 AM by mauricior6328708

    How to resize an image inside a selection!

    mauricior6328708 Level 1

      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!

      img.jpg

        • 1. Re: How to resize an image inside a selection!
          mauricior6328708 Level 1

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

          • 2. Re: How to resize an image inside a selection!
            JJMack Most Valuable Participant

            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

            img.jpg

            • 3. Re: How to resize an image inside a selection!
              JJMack Most Valuable Participant

              mauricior6328708 wrote:

               

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

              It can not only be the height for there are two aspect ratios involved to fit an image to a rectangle area it will either be as exact fit resize or the resize will  be fitted to the width or height that would depend on the two aspect ratios evolved.  The Images aspect ratio and the selection area aspect ratio.

               

              As in your exanple 3 the left image is not fitted within the selection.   You can of course only take into account the selection height,  as shown in #3 without masking Resize side by side image like shown could wind up overlapping when both images have a wide aspect ratio. Both will exceed the selections width. If selected aras are near each other.

               

              As I wrote  I prefer to fill the selection area and mask the resized image to that area.

               

              Keep in mind images and selection area should have the same type of orientation.  Landscape into portrait area or Portrait into Landscape do not fair well the virtual center crop compositions most likely will not be acceptable.

              • 4. Re: How to resize an image inside a selection!
                mauricior6328708 Level 1

                Got it!

                I'm looking for another way using smart object ... Any news I come back and report the result. Thank you!

                • 5. Re: How to resize an image inside a selection!
                  JJMack Most Valuable Participant

                  I use placed smart object layer so the smart object rendered pixels are my image full size images pixels the Smart object image will resize well.  Using Smart object layer does not change the way one would resize images.

                   

                  Replacing the content of a smart object is a good way to create templates for Picture packages.  For you can have all smart object layers sharing a single object.  You replace the single placed image object and all image location contents change and the smart object replacement is resized for each area by the smart object layers associated transform that does not get replaced. The only  restriction is the replacement image need to have the same size as the original placed image.

                   

                   

                  Placing in image files create heavyweight Smart object layers. If I am doing something like laying out many image in a document I will paste in resized raster images layers.

                   

                  http://www.mouseprints.net/old/dpr/PasteImageRoll.html

                  • 6. Re: How to resize an image inside a selection!
                    EnsilZah_ Level 1

                    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();   
                    }
                    
                    
                    
                    
                    1 person found this helpful
                    • 7. Re: How to resize an image inside a selection!
                      JJMack Most Valuable Participant

                      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.

                      Capture.jpg

                      1 person found this helpful
                      • 8. Re: How to resize an image inside a selection!
                        mauricior6328708 Level 1

                        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!