Skip to main content
Inspiring
November 28, 2023
Question

Need a script that resizes layer to a specific size using the largest dimension [SOLVED]

  • November 28, 2023
  • 1 reply
  • 358 views

Using Actions doesn't help and I'm not familiar with scripts.

So basically I have to resize 300 layers to 3000px using the largest size (width or height, whatever is bigger).

 

I saw scripts to resize images (not layers), so that won't do the job. I saw other solutions for very particular scenarios, but not what I'm looking for either, and since I'm familiar with scripting, I won't be able to adjust it to my needs.

 

Any chance that someone can help me with this?

I would really appreciate it, as this would save me so much time for this very tedious task and I need to deliver this asap

 

EDIT: I found the script that does this, but it needs a subtle change as described by the script's author @Stephen Marsh (thank you for your contribution, Stephen).

Use the following script (the original script used "Math.max" and I changed it to "Math.min" to fit the largest dimension to the canvas):

 

 

 

 

// 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.min(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;

 

 

To make it clear what the Math.max and Math.min do:

   

 

This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
November 28, 2023

Just to be clear, I didn't write the script, I just posted the code with the reference to the original forum posts where it was found. Due to multiple forum software changes, those links don't work anymore.

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/scale-layer-to-current-canvas-size-photoshop/m-p/5217333

 

Another similar script:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/fit-layer-to-canvas-keeping-aspect-ratio/m-p/10519295#M252048

Inspiring
November 29, 2023

My bad. I thought you created it.

Anyway, that script worked like a charm, so thank you for sharing!