如果或者,则将Photoshop javascript的图层缩放到画布,然后居中

我一直试图确定一个脚本以用于数百个图像。这个论坛非常有用,我正尝试修改JJMack的画布脚本。

我正在尝试使脚本执行此操作...

1-如果该图层比画布宽且高,请不要更改它。

2-如果该图层的宽度小于画布,但不如画布高,则按比例缩放以适合画布宽度

3-如果该层比画布宽但不比画布高,则按比例缩放它以适合画布高度

4-然后,如果图层已被点2或3缩放,则将其居中放置在画布上。

这种脚本可以工作,但是我发现如果该层既不那么宽又不那么高,它似乎会缩放该层两次-使其变得比所需的更大。

我想知道是否有人会帮助我找出解决方案?

非常感谢,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,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 " ),DialogModes.NO );
  }catch(e){}
}}  




beichenx 回答:如果或者,则将Photoshop javascript的图层缩放到画布,然后居中

(这是重新格式化的代码;由于原始的JS IMO太密而无法实际检查我的重新格式化是否弄乱了某些东西,因此我这次很犹豫地编辑问题。)

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,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) {}
}

一些琐碎的重构之后:

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,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 "),DialogModes.NO);
  } catch (e) {}
}
本文链接:https://www.f2er.com/3058196.html

大家都在问