-
-
Save jawinn/ab4df1c33d0743e41fd3 to your computer and use it in GitHub Desktop.
// FIT LAYER TO CANVAS | |
// via https://forums.adobe.com/message/5413957#5413957 | |
var maintainAspectRatio;// set to true to keep aspect ratio | |
if(app.documents.length>0){ | |
app.activeDocument.suspendHistory ('Fit Layer to Canvas', 'FitLayerToCanvas('+maintainAspectRatio+')'); | |
} | |
function FitLayerToCanvas( keepAspect ){// keepAspect:Boolean - optional. Default to false | |
var doc = app.activeDocument; | |
var layer = doc.activeLayer; | |
// do nothing if layer is background or locked | |
if(layer.isBackgroundLayer || layer.allLocked || layer.pixelsLocked | |
|| layer.positionLocked || layer.transparentPixelsLocked ) return; | |
// do nothing if layer is not normal artLayer or Smart Object | |
if( layer.kind != LayerKind.NORMAL && layer.kind != LayerKind.SMARTOBJECT) return; | |
// store the ruler | |
var defaultRulerUnits = app.preferences.rulerUnits; | |
app.preferences.rulerUnits = Units.PIXELS; | |
var width = doc.width.as('px'); | |
var height =doc.height.as('px'); | |
var bounds = app.activeDocument.activeLayer.bounds; | |
var layerWidth = bounds[2].as('px')-bounds[0].as('px'); | |
var layerHeight = bounds[3].as('px')-bounds[1].as('px'); | |
// move the layer so top left corner matches canvas top left corner | |
layer.translate(new UnitValue(0-layer.bounds[0].as('px'),'px'), new UnitValue(0-layer.bounds[1].as('px'),'px')); | |
if( !keepAspect ){ | |
// scale the layer to match canvas | |
layer.resize( (width/layerWidth)*100,(height/layerHeight)*100,AnchorPosition.TOPLEFT); | |
}else{ | |
var layerRatio = layerWidth / layerHeight; | |
var newWidth = width; | |
var newHeight = ((1.0 * width) / layerRatio); | |
if (newHeight >= height) { | |
newWidth = layerRatio * height; | |
newHeight = height; | |
} | |
var resizePercent = newWidth/layerWidth*100; | |
app.activeDocument.activeLayer.resize(resizePercent,resizePercent,AnchorPosition.TOPLEFT); | |
} | |
// restore the ruler | |
app.preferences.rulerUnits = defaultRulerUnits; | |
} |
This is super helpful. I was wondering if it's possible to modify the script to resize a layer set (folder) or a single layer?
My scripting is pretty limited and my searching hasn't provided me a lot to work with.
Very useful, thanks
Thank you very much sir, you were very helpful
Thank you so much!
Thank you! This was super useful :)
Hey there. I like your code very much. It made my day. Thank you very very much. But I think there is a problem. If I mask any layer and then try to run this script, it is not working properly. Will you please take a look?
Thank you jawinn! Really useful script. But it doesn't fit layer proportionally. I fixed it:
// FIT LAYER TO CANVAS
// via https://forums.adobe.com/message/5413957#5413957
var maintainAspectRatio = true; // set to true to keep aspect ratio
if (app.documents.length > 0) {
app.activeDocument.suspendHistory('Fit Layer to Canvas', 'FitLayerToCanvas(' + maintainAspectRatio + ')');
}
function FitLayerToCanvas(keepAspect) {
// keepAspect: Boolean - optional. Default to false
var doc = app.activeDocument;
var layer = doc.activeLayer;
// do nothing if layer is background or locked
if (layer.isBackgroundLayer || layer.allLocked || layer.pixelsLocked || layer.positionLocked || layer.transparentPixelsLocked) return;
// do nothing if layer is not a normal artLayer or Smart Object
if (layer.kind != LayerKind.NORMAL && layer.kind != LayerKind.SMARTOBJECT) return;
// store the ruler
var defaultRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var width = doc.width.as('px');
var height = doc.height.as('px');
var bounds = app.activeDocument.activeLayer.bounds;
var layerWidth = bounds[2].as('px') - bounds[0].as('px');
var layerHeight = bounds[3].as('px') - bounds[1].as('px');
// move the layer so the top-left corner matches the canvas top-left corner
layer.translate(new UnitValue(0 - bounds[0].as('px'), 'px'), new UnitValue(0 - bounds[1].as('px'), 'px'));
if (keepAspect) {
var layerRatio = layerWidth / layerHeight;
var canvasRatio = width / height;
if (layerRatio > canvasRatio) {
var newWidth = width;
var newHeight = newWidth / layerRatio;
} else {
var newHeight = height;
var newWidth = newHeight * layerRatio;
}
var resizePercent = (newWidth / layerWidth) * 100;
app.activeDocument.activeLayer.resize(resizePercent, resizePercent, AnchorPosition.TOPLEFT);
} else {
// scale the layer to match the canvas
layer.resize((width / layerWidth) * 100, (height / layerHeight) * 100, AnchorPosition.TOPLEFT);
}
// restore the ruler
app.preferences.rulerUnits = defaultRulerUnits;
}
Thanks useful script