Resize layer to fit canvas size
-
Byteripper
Resize layer to fit canvas size
Could you help me with a script that resize layer and constrain proportions to fit canvas size, according to any size of the canvas?
-
myranalis
Resize layer to fit canvas size
This is my standard function to scale and center one layer to fit another - or in your case pTargetLayer is the document itself.
Code: Select allfunction ScaleToFit(pLyr, pTargetLayer) {
try {
var cw;
var ch;
var targetx;
var targety;
if (pTargetLayer.typename == "ArtLayer") {
cw =pTargetLayer.bounds[2].as('px') - pTargetLayer.bounds[0].as('px');
ch = pTargetLayer.bounds[3].as('px') - pTargetLayer.bounds[1].as('px');
targetx = pTargetLayer.bounds[0].as('px') + cw/2;
targety = pTargetLayer.bounds[1].as('px') + ch/2;
} else if (pTargetLayer.typename == "Document") {
cw =pTargetLayer.width.as('px');
ch = pTargetLayer.height.as('px');
targetx = cw/2;
targety = ch/2;
} else {
//invalid type
return;
}
var lw = pLyr.bounds[2].as('px') - pLyr.bounds[0].as('px');
var lh = pLyr.bounds[3].as('px') - pLyr.bounds[1].as('px');
var rWidth = cw/lw;
var rHeight = ch/lh;
if (rWidth != 1 && rHeight != 1) {
if (rWidth > rHeight) {
//resize up by rwidth as a percent
pLyr.resize(rWidth * 100, rWidth * 100);
} else {
//resize up by rheight as a percent
var t = rHeight * 100;
pLyr.resize(rHeight * 100, rHeight * 100);
}
}
lw = pLyr.bounds[2].as('px') - pLyr.bounds[0].as('px');
lh = pLyr.bounds[3].as('px') - pLyr.bounds[1].as('px');
var currx = pLyr.bounds[0].as('px') + lw/2;
var curry = pLyr.bounds[1].as('px') + lh/2;
pLyr.translate(targetx - currx, targety - curry);
} catch (e) {
//ignore
}
}
Code: Select allfunction ScaleToFit(pLyr, pTargetLayer) {
try {
var cw;
var ch;
var targetx;
var targety;
if (pTargetLayer.typename == "ArtLayer") {
cw =pTargetLayer.bounds[2].as('px') - pTargetLayer.bounds[0].as('px');
ch = pTargetLayer.bounds[3].as('px') - pTargetLayer.bounds[1].as('px');
targetx = pTargetLayer.bounds[0].as('px') + cw/2;
targety = pTargetLayer.bounds[1].as('px') + ch/2;
} else if (pTargetLayer.typename == "Document") {
cw =pTargetLayer.width.as('px');
ch = pTargetLayer.height.as('px');
targetx = cw/2;
targety = ch/2;
} else {
//invalid type
return;
}
var lw = pLyr.bounds[2].as('px') - pLyr.bounds[0].as('px');
var lh = pLyr.bounds[3].as('px') - pLyr.bounds[1].as('px');
var rWidth = cw/lw;
var rHeight = ch/lh;
if (rWidth != 1 && rHeight != 1) {
if (rWidth > rHeight) {
//resize up by rwidth as a percent
pLyr.resize(rWidth * 100, rWidth * 100);
} else {
//resize up by rheight as a percent
var t = rHeight * 100;
pLyr.resize(rHeight * 100, rHeight * 100);
}
}
lw = pLyr.bounds[2].as('px') - pLyr.bounds[0].as('px');
lh = pLyr.bounds[3].as('px') - pLyr.bounds[1].as('px');
var currx = pLyr.bounds[0].as('px') + lw/2;
var curry = pLyr.bounds[1].as('px') + lh/2;
pLyr.translate(targetx - currx, targety - curry);
} catch (e) {
//ignore
}
}
-
Byteripper
Resize layer to fit canvas size
Maybe I'm doing it wrong... I have the layer trimed and when i run the script it doesn't do anything.
-
myranalis
Resize layer to fit canvas size
Well first thing - the above was meant to be just a function you can adapt to your needs - not a complete script. It doesn't do anything if you just run it in Photoshop as is. You need to at least add the following as the very last line:
Code: Select allScaleToFit(app.activeDocument.activeLayer, app.activeDocument);
Then run it after you've selected your shoe layer.
Second, I doubt that function will work in your case because I rarely have to accommodate layer masks in the layers I am moving around. I am pretty sure that you'll have to apply the layer mask before that function will work.
Code: Select allScaleToFit(app.activeDocument.activeLayer, app.activeDocument);
Then run it after you've selected your shoe layer.
Second, I doubt that function will work in your case because I rarely have to accommodate layer masks in the layers I am moving around. I am pretty sure that you'll have to apply the layer mask before that function will work.