Skip to main content
Participant
Feb 19, 2025 7:51 AM UTC
Question

How to resize layer to a specific size and have other layers resized accordingly

Hi,

I am in a process of making product images for a client with rulers to provide scale, a bit like this (not my image, but similar use case)

All the images are same size and they consist two layers: top layer with a photo of the product (trimmed and w/ transparency) and a background layer with the rulers. However, now the client wants me to make all the products same size, meaning that the image size would vary. I don´t quite understand why they would want that, but whatever.

The problem is that I already have >1000 images so I would really like to automate this process. Is there a way how I could resize the top layer to specific pixel size and have background layer automatically resized relative to the top. Or in other words, resize the image to a size where the top layer would be X pixels wide.

4 replies

c.pfaffenbichler
Community Expert
Community Expert
Feb 19, 2025 8:41 AM UTC

Please provide a few sample files. 

What is »X pixels« exactly? 

Translate
coolhat73Author
Participant
Feb 19, 2025 9:14 AM UTC

Attached are mock samples. Let us say that I want to resize both of these these so that the top layer ("Layer 2") is 300 pixels wide

 

Translate
c.pfaffenbichler
Community Expert
Community Expert
Feb 19, 2025 9:58 AM UTC

Thank you.

»product1.psd« seems to feature faulty masking, though. 

 

A bit sloppy but you can try 

// 2025, use it at your own risk;
if (app.documents.length > 0) {
var targetWidth = 300;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// the file;
var myDocument = app.activeDocument;
var theLayers = collectLayers (myDocument, []);
for (var m = 0; m < theLayers.length; m++) {
theLayers[m].visible = true;
var theLayer = smartify2010(theLayers[m]);
};
var theWidth = myDocument.activeLayer.bounds[2] - myDocument.activeLayer.bounds[0];
var theScale = targetWidth/theWidth*100;
// resize;
myDocument.resizeImage (new UnitValue (theScale, "%"), new UnitValue (theScale, "%"), undefined, ResampleMethod.BICUBICAUTOMATIC);
// reset;
app.preferences.rulerUnits = originalRulerUnits;
};
////// function collect all layers //////
function collectLayers (theParent, allLayers) {
if (!allLayers) {var allLayers = new Array} 
else {};
for (var m = theParent.layers.length - 1; m >= 0;m--) {
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
allLayers.push(smartify2010(theLayer))
}
else {
allLayers = (collectLayers(theLayer, allLayers))
// this line includes the layer groups;
allLayers.push(theLayer);
}
};
return allLayers
};
////// function to smartify if not //////
function smartify2010 (theLayer) {
// make layers smart objects if they are not already;
app.activeDocument.activeLayer = theLayer;
// process pixel-layers and groups;
if (theLayer.kind == "LayerKind.GRADIENTFILL" || theLayer.kind == "LayerKind.LAYER3D" || theLayer.kind == "LayerKind.NORMAL" || 
theLayer.kind == "LayerKind.PATTERNFILL" || theLayer.kind == "LayerKind.SOLIDFILL" || 
theLayer.kind == "LayerKind.TEXT" || theLayer.kind == "LayerKind.VIDEO" || theLayer.typename == "LayerSet") {
var id557 = charIDToTypeID( "slct" );
var desc108 = new ActionDescriptor();
var id558 = charIDToTypeID( "null" );
var ref77 = new ActionReference();
var id559 = charIDToTypeID( "Mn  " );
var id560 = charIDToTypeID( "MnIt" );
var id561 = stringIDToTypeID( "newPlacedLayer" );
ref77.putEnumerated( id559, id560, id561 );
desc108.putReference( id558, ref77 );
executeAction( id557, desc108, DialogModes.NO )
return app.activeDocument.activeLayer
}
if (theLayer.kind == LayerKind.SMARTOBJECT || theLayer.kind == "LayerKind.VIDEO") {return theLayer};
};
Translate