• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Jul 07, 2014 Jul 07, 2014

Copy link to clipboard

Copied

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.

  1. var maintainAspectRatio;// set to true to keep aspect ratio 
  2. if(app.documents.length>0){ 
  3.     app.activeDocument.suspendHistory ('Fit Layer to Canvas', 'FitLayerToCanvas('+maintainAspectRatio+')'); 
  4. function FitLayerToCanvas( keepAspect ){// keepAspect:Boolean - optional. Default to false 
  5.     var doc = app.activeDocument; 
  6.     var layer = doc.activeLayer; 
  7.     // do nothing if layer is background or locked 
  8.     if(layer.isBackgroundLayer || layer.allLocked || layer.pixelsLocked 
  9.                             || layer.positionLocked || layer.transparentPixelsLocked ) return; 
  10.     // do nothing if layer is not normal artLayer or Smart Object 
  11.     if( layer.kind != LayerKind.NORMAL && layer.kind != LayerKind.SMARTOBJECT) return; 
  12.     // store the ruler 
  13.     var defaultRulerUnits = app.preferences.rulerUnits; 
  14.     app.preferences.rulerUnits = Units.PIXELS; 
  15.      
  16.     var width = doc.width.as('px'); 
  17.     var height =doc.height.as('px'); 
  18.     var bounds = app.activeDocument.activeLayer.bounds; 
  19.     var layerWidth = bounds[2].as('px')-bounds[0].as('px'); 
  20.     var layerHeight = bounds[3].as('px')-bounds[1].as('px'); 
  21.          
  22.     // move the layer so top left corner matches canvas top left corner 
  23.     layer.translate(new UnitValue(0-layer.bounds[0].as('px'),'px'), new UnitValue(0-layer.bounds[1].as('px'),'px')); 
  24.     if( !keepAspect ){ 
  25.         // scale the layer to match canvas 
  26.         layer.resize( (width/layerWidth)*100,(height/layerHeight)*100,AnchorPosition.TOPLEFT); 
  27.     }else{ 
  28.         var layerRatio = layerWidth / layerHeight; 
  29.         var newWidth = width; 
  30.         var newHeight = ((1.0 * width) / layerRatio); 
  31.         if (newHeight >= height) { 
  32.             newWidth = layerRatio * height; 
  33.             newHeight = height; 
  34.         } 
  35.         var resizePercent = newWidth/layerWidth*100; 
  36.         app.activeDocument.activeLayer.resize(resizePercent,resizePercent,AnchorPosition.TOPLEFT); 
  37.     } 
  38.     // restore the ruler 
  39.     app.preferences.rulerUnits = defaultRulerUnits; 
TOPICS
Actions and scripting

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Jul 07, 2014 Jul 07, 2014

Hum Im not sure Im getting you here… Have you looked at Image Processor…?

Why are you NOT just using Fit Image and canvas size in your actions…?

These are all built-in to Photoshop.

If you wanted to do all 3 sizes in the 1 fly-bye then use script to process…

If you need extra file naming conventions then script would probably be best…

All of the above should have NO trouble handling your 3k files…

Votes

Translate

Translate
Adobe
Guru ,
Jul 07, 2014 Jul 07, 2014

Copy link to clipboard

Copied

Hum Im not sure Im getting you here… Have you looked at Image Processor…?

Why are you NOT just using Fit Image and canvas size in your actions…?

These are all built-in to Photoshop.

If you wanted to do all 3 sizes in the 1 fly-bye then use script to process…

If you need extra file naming conventions then script would probably be best…

All of the above should have NO trouble handling your 3k files…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 07, 2014 Jul 07, 2014

Copy link to clipboard

Copied

LATEST

Apparently, I just like to do things the difficult way. Everything I found searching made it sound like you needed to write a script to accomplish what I wanted to do. Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines