Skip to main content
Participant
November 29, 2023
Question

Fit image to canvas keeping proportion but filling all the canvas sides

  • November 29, 2023
  • 1 reply
  • 251 views

Hi!

 

I am using a nice script from this forum to fit layer to canvas, but I need to make a change I can't get my head around. 

 

We have a grid that we load to a document, and we want it to be stretched to all sides of the document, but keeping the proportions of the grid (I don't mind loosing part of the grid outside the document). Probably it is an easy change to the code, but I am having a hard time figuring it out.

 

This is the code we are using, from another thread in the forum.

 

// FIT LAYER TO CANVAS
var maintainAspectRatio = true; // set to true to keep aspect ratio, false to stretch/distort to fit
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;
}
 
Thanks!
This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
November 29, 2023

Does this one do what you want?

 

// https://forums.adobe.com/thread/1968642
// https://forums.adobe.com/message/8022190#8022190
#target photoshop    
var oldPref = app.preferences.rulerUnits    
app.preferences.rulerUnits = Units.PIXELS;    
var doc = activeDocument;   
var iLayer = doc.activeLayer;    
doc.activeLayer = iLayer;    
var scale = Math.max(doc.width/(iLayer.bounds[2]-iLayer.bounds[0]),doc.height/(iLayer.bounds[3]-iLayer.bounds[1])); // Optionally change Math.max to Math.min to fit canvas short side
iLayer.resize (scale*100,scale*100);    
iLayer.translate(doc.width/2-(iLayer.bounds[0]+iLayer.bounds[2])/2,doc.height/2-(iLayer.bounds[1]+iLayer.bounds[3])/2);
app.preferences.rulerUnits = oldPref;
Participant
November 29, 2023

Yes! That is great 🙂

 

And thank you, Stephen, for helping me on this, and all the other times I found the solution on your comments in the forum. Have a lovely day!