21 Replies Latest reply: May 16, 2014 11:09 AM by spdorsey6969 RSS

    Resize canvas as a percentage of a layer's width?

    spdorsey6969 Community Member

      Hello

       

        Is there a way to resize a canvas so that it is exactly 120% the area of a selection? Or perhaps 120% of a layer's width?

       

        We have lots of .psd files that have large margins. I need to reduce them so that they have 10% of the content's height as margins on all sides, but each document is a different width and height. So I need to select the contents of the document, increase that selected area by 20% vertical (somehow), and then change the document's width/height so that it has that 10% margin all the way around.

       

      Thanks

        • 1. Re: Resize canvas as a percentage of a layer's width?
          gener7 Community Member

          Yes you can do this. See if checking "Relative" works for you.

           

          Screen Shot 2014-05-12 at 2.57.22 PM.png

          • 2. Re: Resize canvas as a percentage of a layer's width?
            spdorsey6969 Community Member

            That's pretty close, but it calculates the top and bottom margins according to the height of the content. I need the top & bottom margins to be the same as the left & right margins.

            • 3. Re: Resize canvas as a percentage of a layer's width?
              JJMack Community Member

              A document can have many layer and each layer can have a different size then the canvas except a background layer will always be canvas size because the background layer if there is one will be the bottom layer and will be canvas size for Photoshop background layer does not support any transparency. If a documents canvas size is change the background layer size will also change to match no other type of layer will see any size change only it position over the canvas will change.

               

              A script could be written to implement re-size a documents canvas size using any rule you can dream up. 

              • 4. Re: Resize canvas as a percentage of a layer's width?
                spdorsey6969 Community Member

                  On a 600 x 891 document, I would need it to be changed to 720 x 1101.

                 

                  For width, 720 = 600 x 120%, or 600 + 120px = 720px.

                 

                  For height, 1101 = 891 + 120. I'm using 120px to add to the height because the height needs to match the width margin.

                 

                  The margin needs to be equal all the way around the canvas, and it needs to match 10% of the width of the content.

                 

                  It's odd, I know. But this is what the client is asking for. I have hundreds of these to do, so I'd LOVE to automate it with an action.

                • 5. Re: Resize canvas as a percentage of a layer's width?
                  gener7 Community Member

                  Yes, it's going by the height and width. So the trick is to reduce the percentage value for the height.

                   

                  Let's look at aspect ratio 4H:3W for example.  H will be 7.5% in order for the border to be even.

                   

                  If you are  working with a mixed bag of documents with different ratios, you will have to take time out to calculate your H values if you want them the same as your W values.

                   

                  I hope that gets you closer. I don't have a script off the top of my head that would makes this go faster, but I hope I got you in the right direction.

                  • 6. Re: Resize canvas as a percentage of a layer's width?
                    spdorsey6969 Community Member

                    This is a very good start, thank you very much for your time. I think the only answer may be a manual calculation.

                    • 7. Re: Resize canvas as a percentage of a layer's width?
                      JJMack Community Member

                      Not with an action it would need logic it would need to be automated with a script.

                       

                      A script could be written to implement re-size a documents canvas size using any rule you can dream up.  120% time width pixels  = z pixels.   z-width = pixels to be added to width  you want the same added to height so the new canvas size would be width and height a relative +Z-width pixels.  Its that simple in a script. However you kneed to know Photoshop scripting.  Scripting is not easy like action they are programs like Fit Image, Image Processor, Contact Sheet II etc. Actions can use scripts.

                       

                      WM900x600.jpgCapture.jpg

                      Capture.jpg
                      Download

                      • 8. Re: Resize canvas as a percentage of a layer's width?
                        spdorsey6969 Community Member

                        You are correct. A script could be built that stores the width and height in variables and uses them to calculate the new canvas size. but I do not know javascript, so that's not much of an option.

                        • 9. Re: Resize canvas as a percentage of a layer's width?
                          gener7 Community Member

                          Maybe this artcle can help you out if you would like to try a ready made script.

                           

                          Creating a uniform border as a percentage of image size. How?

                          • 10. Re: Resize canvas as a percentage of a layer's width?
                            spdorsey6969 Community Member

                            I am going to scrutinize this article. I believe the answer is buried somewhere in this code.

                             

                            I'll have to have a co-worker who is more fluent in JS take a look at it. Perhaps we can decipher it together. I'll try to post the results.

                             

                            Thank you so much!

                            • 11. Re: Resize canvas as a percentage of a layer's width?
                              gener7 Community Member

                              Indeed. This code added text layers in addition to a uniform border, so grabbing the snippet of code for your work should get you there.

                               

                              I'd be interested how it turns out for you.

                               

                              Gene

                              • 12. Re: Resize canvas as a percentage of a layer's width?
                                JJMack Community Member

                                I can not type so typing is a daunting task for me.  I do not know javascript but do have a programming background and don't fear  hacking at scripts.  Scripting can indeed be an giant task therefore I create simple scripts to be used within actions to add a little logic.  A simple little thing like rotate portrait images 90 degree to landscape.   Simple scripts like that can be useful in an action.   That script can be expanded on to make it a run twice script.  Where the first time run it makes some notation in the document's meta data and rotates portraitist The second time run the script remove the notation and rotates back a document it rotated on the first run.  I mention this because both those script are in my crafting action package. I posted. some script in it are large and complex other like rotate portrait a extremely simple. I tried to write these scripts to be easy to read and have commented them well. Rotate portrait has most of what you need as far as code goes.  Show how to set unites to work in pixels and how to get width and height the do the compare and rotate the portraits clean up by restoring the users units setting.

                                 

                                /*

                                <javascriptresource>

                                <about>$$$/JavaScripts/RotatePortrait/About=JJMack's Rotate Portrait.^r^rCopyright 2009 Mouseprints.^r^rScript utility for action.^rNOTE:Rotate Portrait to Landscape!</about>

                                <category>JJMack's Action Utility</category>

                                </javascriptresource>

                                */

                                Rotate();

                                 

                                function Rotate() {

                                 

                                // validate that a document is open

                                if (documents.length < 1) {

                                  alert("No Open Document!");

                                  return;

                                  }

                                var orig_ruler_units = app.preferences.rulerUnits;

                                app.preferences.rulerUnits = Units.PIXELS;

                                // Rotate portrait to landscape orientation

                                if (activeDocument.height > activeDocument.width) activeDocument.rotateCanvas(90);

                                 

                                // Reset units to original settings

                                app.preferences.rulerUnits = orig_ruler_units;

                                }

                                Note: Document width is canvas width  Layers can be many and they may have many different sizes. None need to have the same width as the canvas.  Larger layers then the canvas will be clipped by the canvas size. If you add canvas pixel that may have been clipped by the document canvas size may now show in the added canvas... Photoshop is quite complex.

                                 

                                A little scripting can go a long ways when used in actions....

                                • 13. Re: Resize canvas as a percentage of a layer's width?
                                  JJMack Community Member

                                  Even the more complex run twice script is not the much code,

                                   

                                  //////////////////////////////////////////////////////////////////////////////////

                                  //

                                  // Copyright 2002-2003. Adobe Systems, Incorporated. All rights reserved.

                                  // This scripts demonstrates how to rotate a layer 90 degrees clockwise.

                                  // Original file came from PSCS scripting\samples\javascript\RotateLayer.js

                                  //

                                  // Variation Copyright(c)Douglas Cody, 2004, All Rights Reserved.

                                  // http://www.clikphoto.com

                                  //

                                  // Updataed John J McAssey 2008 - 2009 http://mouseprints.net

                                  //

                                  // This script is designed to be used by a Photoshop Action twice

                                  // A good pratice to use when creating an actions that use this scipt is for the action

                                  // not to do a save or play some other action between its two useages of this Script.

                                  //

                                  // This script will look at the document orientation (portrait vs landscape)

                                  // On the first execution, if the document is a portrait, it will be rotated

                                  // to a horizontal.

                                  // On the second execution, a rotated document will be

                                  // restored to a vertical. This effectively toggles the orientation ONLY if

                                  // the original document started out as a portrait.

                                  //

                                  // NOTE: Meta-data Info Instructions field is modified to hold an interim state.

                                  //

                                  // Bug Fixes by JJMack, 2008, with the original code Square images were always

                                  // rotated -90 and marked "rotate back" with two executions you wound up with

                                  // an upside down image... The original code would also fail to rotate an image

                                  // back if the action added canvas and change the rotated images aspect ratio

                                  // to other then landscape. Again you wind up with a marked upside down image.

                                  // In addition units the compare could fail because the script did not set the

                                  // units to use for rulers.  

                                  //

                                  // Updated in 2009 JJMack to remove some restrictions try make it near bullet proof 

                                  // presserve any data that might have been in Files metada Info Instructions field

                                  // this also allows more one run twice scripts to be used on a document.

                                  //

                                  //////////////////////////////////////////////////////////////////////////////////

                                   

                                   

                                  /*

                                  <javascriptresource>

                                  <about>$$$/JavaScripts/orient/About=JJMack's Orient^r^rCopyright 2009 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^rFirst Run records orintation and rotate Protrait to Landscape^rSecond Run removes orintation recorded and rotates Portrats back.</about>

                                  <category>JJMack's Action Run Twice Utility</category>

                                  </javascriptresource>

                                  */

                                   

                                   

                                  if (app.documents.length > 0) {

                                          var orintation = '';

                                    if (app.activeDocument.info.instructions.indexOf("<orient>") == -1 ) { // No Footprint

                                    //alert("first")

                                   

                                   

                                    var orig_ruler_units = app.preferences.rulerUnits; // Save ruler units

                                    app.preferences.rulerUnits = Units.PIXELS; // Set ruler units to PIXELS

                                   

                                   

                                    // Add Foot Print to  metadata info instructions and rorate protrait documents

                                    // alert( " Width = " + app.activeDocument.width + " Height = " + app.activeDocument.height );

                                    if (app.activeDocument.width < app.activeDocument.height) { // portrait 

                                    app.activeDocument.rotateCanvas(-90.0);

                                    app.activeDocument.info.instructions = app.activeDocument.info.instructions += "<orient>portrait</orient>";

                                    }

                                    else { app.activeDocument.info.instructions += "<orient>landscape or square</orient>"; } // not portrait

                                    // Reset units to original settings

                                   

                                   

                                    app.preferences.rulerUnits = orig_ruler_units; // Restore ruler units

                                    }

                                    else {

                                    //alert("second")

                                    // Retreive saved orintation and rotate portrait back up

                                    orientOffset = app.activeDocument.info.instructions.indexOf("<orient>") + "<orient>".length;

                                    orientLength = app.activeDocument.info.instructions.indexOf("</orient>") -orientOffset;

                                    orintation = app.activeDocument.info.instructions.substr(orientOffset, orientLength);

                                    if ( orintation == "portrait" ) { app.activeDocument.rotateCanvas(90.0); }

                                   

                                   

                                    // Remove footprint from metadata info instructions

                                    before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexO f("<orient>"));

                                    afterOffset = app.activeDocument.info.instructions.indexOf("</orient>") + "</orient>".length;

                                    after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);

                                    app.activeDocument.info.instructions = before + after;

                                    }

                                  }

                                  else { alert("You must have at least one open document to run this script!"); }

                                  • 14. Re: Resize canvas as a percentage of a layer's width?
                                    spdorsey6969 Community Member

                                    I adjusted the script that was submitted by Paul Riggott. Thanks, Gener7, for linking to it. And thanks to all who replied. I appreciate your responses.

                                     

                                    My version of the code could probably be cleaner (I don't really understand how it works), but it seems to do the trick. Here it is:

                                     

                                     

                                     

                                     

                                     

                                    if (documents.length < 1) {

                                      alert("No Open Document!");

                                      }

                                     

                                      if(documents.length) app.activeDocument.suspendHistory('Add Border', 'main()');  

                                    function main(){ 

                                    var startRulerUnits = preferences.rulerUnits; 

                                    app.preferences.rulerUnits = Units.PIXELS; 

                                    var doc = activeDocument; 

                                    var tenPercent = (Math.max(doc.height,doc.width)/100) * 10; 

                                    var OffsetX = tenPercent; 

                                    var OffsetY = tenPercent/4;  

                                    doc.resizeCanvas((doc.width + (tenPercent*2)), (doc.height + (tenPercent*2)), AnchorPosition.MIDDLECENTER);  

                                    var LB = doc.activeLayer.bounds;  

                                    var LHeight = Math.abs(LB[3].value) - Math.abs(LB[1].value); 

                                    var percentage = ((tenPercent/LHeight)*50); 

                                    doc.activeLayer.resize(percentage,percentage,AnchorPosition.MIDDLECENTER); 

                                    LB = doc.activeLayer.bounds;  

                                    var X = (activeDocument.width - tenPercent) - LB[2].value; 

                                    var Y = (activeDocument.height - OffsetY) - LB[3]; 

                                    doc.activeLayer.translate(X,Y); 

                                    //doc.flatten(); 

                                    preferences.rulerUnits = startRulerUnits; 

                                    }

                                    • 15. Re: Resize canvas as a percentage of a layer's width?
                                      JJMack Community Member

                                      I get a script error running that script on a document with only a background layer. That script can also move a the active layer if it a layered document.  You should not be playing around with the documents active layer.  You should not be working on the document active layer you just want to change the canvas size..

                                       

                                      if (documents.length < 1) {

                                        alert("No Open Document!");

                                        }

                                       

                                        if(documents.length) app.activeDocument.suspendHistory('Add Border', 'main()'); 

                                      function main(){

                                      var startRulerUnits = preferences.rulerUnits;

                                      app.preferences.rulerUnits = Units.PIXELS;

                                      var doc = activeDocument;

                                      var tenPercent = (Math.max(doc.height,doc.width)/100) * 10;

                                      //var OffsetX = tenPercent;

                                      //var OffsetY = tenPercent/4; 

                                      doc.resizeCanvas((doc.width + (tenPercent*2)), (doc.height + (tenPercent*2)), AnchorPosition.MIDDLECENTER); 

                                      //var LB = doc.activeLayer.bounds; 

                                      //var LHeight = Math.abs(LB[3].value) - Math.abs(LB[1].value);

                                      //var percentage = ((tenPercent/LHeight)*50);

                                      //doc.activeLayer.resize(percentage,percentage,AnchorPosition.MIDDLECENTER);

                                      //LB = doc.activeLayer.bounds; 

                                      //var X = (activeDocument.width - tenPercent) - LB[2].value;

                                      //var Y = (activeDocument.height - OffsetY) - LB[3];

                                      //doc.activeLayer.translate(X,Y);

                                      //doc.flatten();

                                      preferences.rulerUnits = startRulerUnits;

                                      }

                                      • 16. Re: Resize canvas as a percentage of a layer's width?
                                        gener7 Community Member

                                        Thanks for the adjustment, JJ. I modified it for a 50% frame, and assigned a keyboard shortcut to it. I rarely use scripts,but it's good to try out a few.

                                        • 17. Re: Resize canvas as a percentage of a layer's width?
                                          JJMack Community Member

                                          Here is a cleaned up script.  This script will only work with version of Photoshop newer than CS2 for it uses the method app.activeDocument.suspendHistory(); to suspend Photoshop History states.  This method was added to Photoshop's scripting after CS2.  The script can be made to run with CS2 by just using main();.  It take time to learn how Photoshop works and how it works changes over time as updates are still being added.  Knowing how Photoshop works will enable you to record better actions and write script that work well.

                                          if (documents.length < 1) alert("No Open Document!");
                                          else app.activeDocument.suspendHistory('Add Border', 'main()');
                                          
                                          function main(){
                                            var startRulerUnits = app.preferences.rulerUnits;
                                            app.preferences.rulerUnits = Units.PIXELS;
                                            var borderSize = (Math.max(app.activeDocument.height,app.activeDocument.width)/100) * 10;
                                            app.activeDocument.resizeCanvas((app.activeDocument.width + (borderSize*2)), (app.activeDocument.height + (borderSize*2)), AnchorPosition.MIDDLECENTER); 
                                            app.preferences.rulerUnits = startRulerUnits;
                                          }
                                          
                                          

                                           

                                          Photoshop is a complex application and because of that how this script will works depends on the layers in the current document and Photoshop current background color.   Photoshop supports a special layer known as the Background layer.  A document is not required to have a Background layer.   Only the bottom layer in the layer stack can be a the "Background" layer for no layers can be below it.  The Photoshop's "Background" layer does support transparency so its always partially locked for all pixels in a background opacity is 100% and the layer is the same size as the documents canvas size.  If you change a document Canvas size and the document has a background layer that bckground layers size will also change.  The background can be both cropped and expanded.  When expanded the pixels added will be the current Photoshop background color.  So the added border may be any color or empty pixels. For documents are not required to have a background layer.  Other types of layer support transparency so their sizes will not be changed. Non background layers can be any size and have any aspect ratio and have some alignment to the documents canvas. The are anchored the the canvas and the canvas size acts like a clipping mask for the composite image.   When new canvas is added the alignment to the canvas may change this is controlled anchor position used during the resize canvas operation.  A border added to a document may not be empty or a solid color for the old canvas size may have been clipping pixels in some layers that are now no longer clipped and show in the composite.

                                          • 18. Re: Resize canvas as a percentage of a layer's width?
                                            gener7 Community Member

                                            I just got it, and replaced the old script. On both layered and non-layered files, it leaves "Add Border" in the History Panel for CC on my Mac.

                                             

                                            Nice job getting the code down to a mean and lean 10 lines.

                                             

                                            Thanks again!!

                                            • 19. Re: Resize canvas as a percentage of a layer's width?
                                              spdorsey6969 Community Member

                                              Thanks for the post, and for the script optimization. It's a work of art!

                                               

                                              The comps that this script will be used on will always have a background layer that is 100% white and that is made from a fill layer. By using a fill layer, the canvas can be resized infinitely and the background will always remain solid and fill the entire canvas.

                                               

                                              This script is invoked using an action. That action hides the background layer before running the script, and it crops the canvas to the size of the document's contents. When the script is applied, it leaves the document with a perfect margin all the way around that is based on exactly 10% of the content's longest edge (width or height).

                                              • 20. Re: Resize canvas as a percentage of a layer's width?
                                                JJMack Community Member

                                                You should un mark your append as correct for that script has errors....

                                                 

                                                The script will not always add white borders. If your document has a background layer the border added will always be a solid color whatever the current background color is. If you want the border to always be white you need to add a statement to set the current Photoshop background color to white in case it was changed.  The Script you started with flatten the document to insure there was a background layer and set the color to black for a black border.  Then it added a white text layer and resize that text layer into the border area and flattened again

                                                • 21. Re: Resize canvas as a percentage of a layer's width?
                                                  spdorsey6969 Community Member

                                                  I just fixed the correct answer thing. I had to go looking for it.