Skip to main content
Participant
June 10, 2014
Question

Resizing layer from script causes it to clip the pixels outside the viewing canvas

  • June 10, 2014
  • 2 replies
  • 421 views

The script uses a pre-created psd file, imports it into the program(ps) and draws data from user input variables

to populate pre-made text layers within the psd file, then it merges all visible layers save for the background and transforms that to

fit within the bounds of the desired area. The problem is, sometimes the string is too long and ends up

being larger than the canvas. when a function is then called to resize the layer to fit within the bounds of the document, the

unvisible pixels are deleted. I would also love for you guys to tell me what i'm doing wrong when transforming(resizing) the layer,

because its supposed to become 5.5" x 1.25" and its not doing that 😕😕

Thank you!

function resizeLayer(){ 

if(!documents.length) return; 

var startRulerUnits = app.preferences.rulerUnits; 

app.preferences.rulerUnits = Units.PIXELS; 

var doc = activeDocument; 

var res= doc.resolution; 

var LB = activeDocument.activeLayer.bounds; 

var Width= LB[2].value - LB[0].value; 

var onePix = 100/Width; 

var newSize = onePix * 1100; 

var newHSize = onePix * 250; 

doc.activeLayer.resize( newSize ,  newHSize, AnchorPosition.MIDDLECENTER);  

app.preferences.rulerUnits = startRulerUnits; 

}

________________________________________________

function PopulatePlate(){

    editplate();

        var 1NameLayer = activeDocument.artLayers.getByName("1");

    if(1NameLayer.kind == LayerKind.TEXT) companyLayer.textItem.contents = 1Name;

      var 2NameLayer = activeDocument.artLayers.getByName("2");

    if(2NameLayer.kind == LayerKind.TEXT) pubLayer.textItem.contents = "Featured in:"+  2Name;

     var 3NameLayer = activeDocument.artLayers.getByName("3");

    if(3NameLayer.kind == LayerKind.TEXT) dateLayer.textItem.contents = 3Name;

    selectLayer("Background");

    app.activeDocument.activeLayer.visible = false

    selectLayer("1");

    app.activeDocument.mergeVisibleLayers();

    selectLayer("Background");

     app.activeDocument.activeLayer.visible = true

     var textLayer = activeDocument.artLayers.getByName("1");

     selectLayer("1");

     resizeLayer();

    app.activeDocument.flatten();

    if (TrimColorV === "s" ||TrimColorV === "b" ||TrimColorV === "as"){app.doAction('DesaturatePlate','Layouts0.3')}

    app.activeDocument.save();

    //app.activeDocument.close();

    app.activeDocument = app.documents[0]

    app.activeDocument.mergeVisibleLayers() ;

     }

This topic has been closed for replies.

2 replies

Participant
June 11, 2014

I'm trying to resize a layer to 5.5" by 1.25", this cant be done with percentage because the source will not always be the same size.
As for the pixels getting cut off, i think the problem is that when i merge the 3 layers it clips out the invisible pixels. anyone know how to keep invisble pixels in when one merges layers in a script?

matias.kiviniemi
Legend
June 12, 2014

One comment is that you should try setting units as inches so you could just .resize( 5.5, 2.5, AnchorPosition.MIDDLECENTER) (disclaimer that I've never actually tried that). Your current formula of *1100, *250 can't work in every case but depends on some assumption on current size. Another comment is that have you thought of aspect ratios? If it's not always 5.5:2.5 that will skew the text that might look funny. You might have to add aspect ratio logic, i.e. stretch horizontally, calculate scale and apply same scale vertically.

On disappearing pixels, try merging the other way (i.e. move other on top of the other and merge that). Photoshop treats the other layer as original that remains and new layer bounds could be those. Or try creating an empty layer and merge both to that. You can also try things in Photoshop, i.e. do you get same behaviour.

matias.kiviniemi
Legend
June 11, 2014

Not 100% sure what you're trying to achieve, i.e. is the problem that size does not become what you want or that pixels disappear. The scaling formula is a bit strange, taking width and applying scale to it both width and height (based on some assumptions on original size dimensions). Disappearing pixels could be because of the document.flatten() after resize. By default resizing/translating a layer does not lose data..

Maybe if you clarify a bit?