2 Replies Latest reply on Jul 7, 2014 4:08 PM by PushpinPictures

    How do I automatically scale a layer to fit a canvas while maintaining the original aspect ratio?

    PushpinPictures

      I'm currently tasked with reformatting 3,000 product images (thumbnail, normal, and large sizes for each) for a new online store. To do that, I'm trying to create a Photoshop (CS6) action that can automate the process as much as possible because I have a hard deadline and not a lot of time to get it all done. Where I'm running into issues is scaling the images automatically once I've used File-->Place. My canvas sizes are all square (670px X 670px, 250px X 250px, and 125px X 125px), but the product images I'm placing on the canvases are almost always rectangular with the height greater than the width at about a 2:3 ratio. I need to scale them so that the image is touching the top and bottom edges of the canvas and the width is adjusted accordingly with the image centered horizontally.

       

      I found the program below on another thread, but it's not working exactly like I need it to. It mentions "maintain aspect ratio," but when I run it, the image I'm trying to place ends up getting stretched to fill the entire canvas rather than the width adjusting to the height once the height has reached its maximum. I have no experience with JavaScript, so I'm having a difficult time adjusting the code to meet my needs. Any help would be greatly appreciated since I am a writer who is WAY out of his comfort zone.

       

      var maintainAspectRatio;// set to true to keep aspect ratio  
      if(app.documents.length>0){  
          app.activeDocument.suspendHistory ('Fit Layer to Canvas', 'FitLayerToCanvas('+maintainAspectRatio+')');  
      }  
      function FitLayerToCanvas( keepAspect ){// keepAspect:Boolean - optional. Default to false  
          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 = app.activeDocument.activeLayer.bounds;  
          var layerWidth = bounds[2].as('px')-bounds[0].as('px');  
          var layerHeight = bounds[3].as('px')-bounds[1].as('px');  
                
          // move the layer so top left corner matches canvas top left corner  
          layer.translate(new UnitValue(0-layer.bounds[0].as('px'),'px'), new UnitValue(0-layer.bounds[1].as('px'),'px'));  
          if( !keepAspect ){  
              // scale the layer to match canvas  
              layer.resize( (width/layerWidth)*100,(height/layerHeight)*100,AnchorPosition.TOPLEFT);  
          }else{  
              var layerRatio = layerWidth / layerHeight;  
              var newWidth = width;  
              var newHeight = ((1.0 * width) / layerRatio);  
              if (newHeight >= height) {  
                  newWidth = layerRatio * height;  
                  newHeight = height;  
              }  
              var resizePercent = newWidth/layerWidth*100;  
              app.activeDocument.activeLayer.resize(resizePercent,resizePercent,AnchorPosition.TOPLEFT);  
          }  
          // restore the ruler  
          app.preferences.rulerUnits = defaultRulerUnits;  
      }