Skip to main content
Participating Frequently
September 29, 2024
解決済み

How can I resize a layer (or text) to a percentage of the canvas?

  • September 29, 2024
  • 返信数 1.
  • 638 ビュー

Using actions (or methods that can be recorded as actions):

 

How can I resize a layer (or text) to a known percentage of the canvas?

 

If I can get a layer to be any specific percentage of the canvas (i.e. 100% width of the canvas) then I can use the percentage sizing in transforms to get it to any size I want, all relative to the canvas size (I can easily align it left and transform it to width 20% for example). The problem is I can't figure out any way to get a starting point that's a known size relative to the canvas.

このトピックへの返信は締め切られました。
解決に役立った回答 Stephen Marsh

There might be a creative way with an action, but a script is what first comes to mind. Scripts can be recorded into an action as a step.

 

Try the following for just the document width:

 

var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

docWidthScaler();

app.activeDocument.resolution = originalRulerUnits;

function docWidthScaler() {
    var theLayer = app.activeDocument.activeLayer;
    app.activeDocument.activeLayer = theLayer;
    var scale = Math.min(app.activeDocument.width / (theLayer.bounds[2] - theLayer.bounds[0]),
        app.activeDocument.width / (theLayer.bounds[3] - theLayer.bounds[1]));
    // Scale to N% of canvas
    theLayer.resize(scale * 50, scale * 50);
    // Centre on canvas
    theLayer.translate(app.activeDocument.width / 2 - (theLayer.bounds[0] + theLayer.bounds[2]) / 2,
        app.activeDocument.height / 2 - (theLayer.bounds[1] + theLayer.bounds[3]) / 2);
}

 

Or conditionally based on Portrait or Landscape document orientation:

 

var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Conditional resize layer by portrait or landscape orientation
if (app.activeDocument.height > app.activeDocument.width) {
    scaleP();
} else {
    scaleL();
}

app.activeDocument.resolution = originalRulerUnits;

function scaleP() {
    var theLayer = app.activeDocument.activeLayer;
    app.activeDocument.activeLayer = theLayer;
    var scale = Math.min(app.activeDocument.width / (theLayer.bounds[2] - theLayer.bounds[0]),
        app.activeDocument.width / (theLayer.bounds[3] - theLayer.bounds[1]));
    // Scale to N% of canvas
    theLayer.resize(scale * 50, scale * 50);
    // Centre on canvas
    theLayer.translate(app.activeDocument.width / 2 - (theLayer.bounds[0] + theLayer.bounds[2]) / 2,
        app.activeDocument.height / 2 - (theLayer.bounds[1] + theLayer.bounds[3]) / 2);
}

function scaleL() {
    var theLayer = app.activeDocument.activeLayer;
    app.activeDocument.activeLayer = theLayer;
    var scale = Math.min(app.activeDocument.width / (theLayer.bounds[2] - theLayer.bounds[0]),
        app.activeDocument.height / (theLayer.bounds[3] - theLayer.bounds[1]));
    // Scale to N% of canvas
    theLayer.resize(scale * 50, scale * 50);
    // Centre the text layer on canvas
    theLayer.translate(app.activeDocument.width / 2 - (theLayer.bounds[0] + theLayer.bounds[2]) / 2,
        app.activeDocument.height / 2 - (theLayer.bounds[1] + theLayer.bounds[3]) / 2);
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

返信数 1

Stephen Marsh
Community Expert
Stephen MarshCommunity Expert解決!
Community Expert
September 30, 2024

There might be a creative way with an action, but a script is what first comes to mind. Scripts can be recorded into an action as a step.

 

Try the following for just the document width:

 

var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

docWidthScaler();

app.activeDocument.resolution = originalRulerUnits;

function docWidthScaler() {
    var theLayer = app.activeDocument.activeLayer;
    app.activeDocument.activeLayer = theLayer;
    var scale = Math.min(app.activeDocument.width / (theLayer.bounds[2] - theLayer.bounds[0]),
        app.activeDocument.width / (theLayer.bounds[3] - theLayer.bounds[1]));
    // Scale to N% of canvas
    theLayer.resize(scale * 50, scale * 50);
    // Centre on canvas
    theLayer.translate(app.activeDocument.width / 2 - (theLayer.bounds[0] + theLayer.bounds[2]) / 2,
        app.activeDocument.height / 2 - (theLayer.bounds[1] + theLayer.bounds[3]) / 2);
}

 

Or conditionally based on Portrait or Landscape document orientation:

 

var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Conditional resize layer by portrait or landscape orientation
if (app.activeDocument.height > app.activeDocument.width) {
    scaleP();
} else {
    scaleL();
}

app.activeDocument.resolution = originalRulerUnits;

function scaleP() {
    var theLayer = app.activeDocument.activeLayer;
    app.activeDocument.activeLayer = theLayer;
    var scale = Math.min(app.activeDocument.width / (theLayer.bounds[2] - theLayer.bounds[0]),
        app.activeDocument.width / (theLayer.bounds[3] - theLayer.bounds[1]));
    // Scale to N% of canvas
    theLayer.resize(scale * 50, scale * 50);
    // Centre on canvas
    theLayer.translate(app.activeDocument.width / 2 - (theLayer.bounds[0] + theLayer.bounds[2]) / 2,
        app.activeDocument.height / 2 - (theLayer.bounds[1] + theLayer.bounds[3]) / 2);
}

function scaleL() {
    var theLayer = app.activeDocument.activeLayer;
    app.activeDocument.activeLayer = theLayer;
    var scale = Math.min(app.activeDocument.width / (theLayer.bounds[2] - theLayer.bounds[0]),
        app.activeDocument.height / (theLayer.bounds[3] - theLayer.bounds[1]));
    // Scale to N% of canvas
    theLayer.resize(scale * 50, scale * 50);
    // Centre the text layer on canvas
    theLayer.translate(app.activeDocument.width / 2 - (theLayer.bounds[0] + theLayer.bounds[2]) / 2,
        app.activeDocument.height / 2 - (theLayer.bounds[1] + theLayer.bounds[3]) / 2);
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

nedmartin作成者
Participating Frequently
September 30, 2024

Thanks, I guess I should just script it all. I was hoping there was some clever way to do it (because I'm lazy and have already got the actions, just trying to make them work on other-sized images as well)

At the moment my action is basically:

  • Type text (that's the correct size for only one size of image - I want to make this step so it's percentage based off the size of the canvas)
  • Layer via copy (3 times, so there's 4 copies)
  • Select first layer
    • Select all
    • Align layer top left corner
    • Move in and down a specific amount (I guess this needs to be percentage-based too, but I think the action can handle that already by using percent instead of pixels)
  • Select second layer
    • Select all
    • Align  layer top right corner
    • Move in and down a specific amount
  • Etc... so there's one in each corner
  • Make them all a group
  • Add transparency channels of all 4 to layer mask