简体   繁体   中英

Photoshop javascript scale layer to canvas if or, then centre

I've been trying to pin down a script to use across a few hundred images. This forum has been so helpful and I'm attempting to amend JJMack's fit to canvas script.

I'm trying to make the script do this...

1- IF the layer is wider AND taller than the canvas, don't change it.

2- IF the layer is less wide BUT taller than the canvas, scale it proportionally to fit the canvas width

3- IF the layer is wider BUT less tall than the canvas, scale it proportionally to fit the canvas height

4- THEN, if the layer has been scaled by points 2 or 3, centre it to the canvas.

This script kind of works but I've found that if the layer is both less wide and less tall, it seems to scale the layer twice - making it bigger than it needs to be.

I wonder if anyone would be kind enough to help me figure out a solution?

Many thanks, J

var SHeight = app.activeDocument.height.as('px');  
var bounds = app.activeDocument.activeLayer.bounds;  
var LWidth = bounds[2].as('px')-bounds[0].as('px');  
var LHeight = bounds[3].as('px')-bounds[1].as('px'); 
var userResampleMethod = app.preferences.interpolation;  // Save interpolation settings  
app.preferences.interpolation = ResampleMethod.BICUBIC; // resample interpolation bicubic  
// ------------------------------------------------------------------------------------------
// If layer width or height less than canvas, scale to fit 1% larger
// ------------------------------------------------------------------------------------------
if (LWidth<SWidth) { // Smart Object layer Aspect Ratio less the Canvas area Aspect Ratio   
   var percentageChange = ((SWidth/LWidth)*101);  // Resize to canvas area width slightly larger  
   activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);  
   app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
      app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
    app.activeDocument.selection.selectAll();
align('AdCH'); align('AdCV');
app.activeDocument.selection.deselect();
// -----------------------------------------
// Align Layers to selection
// -----------------------------------------
function align(method) {
  var desc = new ActionDescriptor();
  var ref = new ActionReference();
  ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
  desc.putReference( charIDToTypeID( "null" ), ref );
  desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
  try{
  executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
  }catch(e){}
  }}  
   if (LHeight<SHeight) { // Smart Object layer Aspect Ratio less the Canvas area Aspect Ratio   
   var percentageChange = ((SHeight/LHeight)*101);  // Resize to canvas area width slightly larger  
   activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);  
   app.preferences.interpolation = userResampleMethod; // Reset interpolation setting  
    app.activeDocument.selection.selectAll();
align('AdCH'); align('AdCV');
app.activeDocument.selection.deselect();
// -----------------------------------------
// Align Layers to selection
// -----------------------------------------
function align(method) {
  var desc = new ActionDescriptor();
  var ref = new ActionReference();
  ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
  desc.putReference( charIDToTypeID( "null" ), ref );
  desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
  try{
  executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
  }catch(e){}
}}  




(This is the reformatted code; I was hesitant to edit the question this time since the original JS, IMO, is too dense to actually check if my reformatting messed something up.)

var SHeight = app.activeDocument.height.as('px');
var bounds  = app.activeDocument.activeLayer.bounds;
var LWidth  = bounds[2].as('px') - bounds[0].as('px');
var LHeight = bounds[3].as('px') - bounds[1].as('px');

var userResampleMethod = app.preferences.interpolation;            // Save interpolation settings

app.preferences.interpolation = ResampleMethod.BICUBIC; // resample interpolation bicubic

// If layer width or height less than canvas, scale to fit 1% larger
if (LWidth < SWidth) { // Smart Object layer Aspect Ratio less the Canvas area Aspect Ratio
  var percentageChange = ((SWidth / LWidth) * 101); // Resize to canvas area width slightly larger

  activeDocument
    .activeLayer
    .resize(percentageChange, percentageChange, AnchorPosition.MIDDLECENTER);

  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting
  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting

  app
    .activeDocument
    .selection
    .selectAll();

  align('AdCH');
  align('AdCV');

  app
    .activeDocument
    .selection
    .deselect();
}

if (LHeight < SHeight) { // Smart Object layer Aspect Ratio less the Canvas area Aspect Ratio
  var percentageChange = ((SHeight / LHeight) * 101); // Resize to canvas area width slightly larger

  activeDocument
    .activeLayer
    .resize(percentageChange, percentageChange, AnchorPosition.MIDDLECENTER);

  app.preferences.interpolation = userResampleMethod; // Reset interpolation setting

  app
    .activeDocument
    .selection
    .selectAll();

  align('AdCH');
  align('AdCV');

  app
    .activeDocument
    .selection
    .deselect();
}

function align(method) {
  var desc = new ActionDescriptor();
  var ref  = new ActionReference();
  ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
  desc.putReference(charIDToTypeID("null"), ref);
  desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
  try {
    executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
  } catch (e) {}
}

After some trivial refactoring:

var SHeight = app.activeDocument.height.as('px');
var bounds  = app.activeDocument.activeLayer.bounds;
var LWidth  = bounds[2].as('px') - bounds[0].as('px');
var LHeight = bounds[3].as('px') - bounds[1].as('px');

var userResampleMethod = app.preferences.interpolation;

app.preferences.interpolation = ResampleMethod.BICUBIC;

if (LWidth < SWidth) {
  var percentageChange = ((SWidth / LWidth) * 101);
  resize(percentageChange)
}

if (LHeight < SHeight) {
  var percentageChange = ((SHeight / LHeight) * 101);
  resize(percentageChange)
}

function resize(percentageChange) {
  activeDocument.activeLayer.resize(percentageChange, percentageChange, AnchorPosition.MIDDLECENTER);
  app.preferences.interpolation = userResampleMethod;
  app.activeDocument.selection.selectAll();
  align('AdCH');
  align('AdCV');
  app.activeDocument.selection.deselect();
}

function align(method) {
  var desc = new ActionDescriptor();
  var ref  = new ActionReference();
  ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
  desc.putReference(charIDToTypeID("null"), ref);
  desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
  try {
    executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
  } catch (e) {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM