Skip to main content
Participant
April 7, 2020
Answered

Javascript to resize a Photoshop layer based on the image size

  • April 7, 2020
  • 2 replies
  • 8065 views

I would like a Javascript that would resize a layer based on the image size. I would need one for 4x5, 5x7, 8x10, 11x4, 16x20. It could be based on pixel width and length. I am trying to tranform a layer based on percentage. Example (If the image size is 1200px x 1500 px transform layer 50%, if image size is 1500 px x 2100 px transform layer by 40% continued through the sizes mentioned above). If anyone can help me with this Javascript, I would greatly appreciate it. - THANK YOU and BE BLESSED and SAFE. 

This topic has been closed for replies.
Correct answer Geppetto Luis

This is an example and you can configure it as you like

if(documents.length){
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
var Width = app.activeDocument.width.value;
var Height = app.activeDocument.height.value;
var Min = Math.min(Width,Height);
var Max = Math.max(Width,Height);


if(Max >= 1200 && Max <= 1500){
var startRulerUnits = app.preferences.rulerUnits; 
app.preferences.rulerUnits = Units.PERCENT; 
var doc = activeDocument; 
var res= doc.resolution; 
var LB = activeDocument.activeLayer.bounds; 
var Width= LB[2].value - LB[0].value; 
var onePrc = 100/Width; 
var newSize = onePrc * 50;  // percent
var newHSize = onePrc * 50;  // percent
doc.activeLayer.resize( newSize ,  newHSize, AnchorPosition.MIDDLECENTER);  
app.preferences.rulerUnits = startRulerUnits; 

}


if(Max >= 1501 && Max <= 2100){
  var startRulerUnits = app.preferences.rulerUnits; 
app.preferences.rulerUnits = Units.PERCENT; 
var doc = activeDocument; 
var res= doc.resolution; 
var LB = activeDocument.activeLayer.bounds; 
var Width= LB[2].value - LB[0].value; 
var onePrc = 100/Width; 
var newSize = onePrc * 40;  // percent
var newHSize = onePrc * 40;  // percent
doc.activeLayer.resize( newSize ,  newHSize, AnchorPosition.MIDDLECENTER);  
app.preferences.rulerUnits = startRulerUnits; 
      
}

app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
}

2 replies

JJMack
Community Expert
Community Expert
April 7, 2020

The sizes you noted 4x5, 5x7, 8x10, 11x14, 16x20.  Look like standard print sizes.   4x5, 8x10 and 16x20 have  a 4:5 Aspect ratios  If you have Image with that Aspect ratio you can print them at those sizes by just changing the image's print resolution setting. An image with a canvas 1200px x  1500px is an image that has a 12:15 Aspect Ratio.  Resampling that image any percentage is only going to loose some image quality.  Its not going to change the imag'es Aspect Ratio unless it is a distorting resize.   The print sized you posted have 4:5, 5:7 and 11:14  aspect ratios.  You have to decide how you want to handle the image aspect ratio miss matches with the print aspect ratios.  Do you want to Crop your image, add border canvas or distort the image to your print aspect ratios.  All can be scripted how do you want to handle miss matches?

JJMack
Geppetto Luis
Geppetto LuisCorrect answer
Legend
April 7, 2020

This is an example and you can configure it as you like

if(documents.length){
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
var Width = app.activeDocument.width.value;
var Height = app.activeDocument.height.value;
var Min = Math.min(Width,Height);
var Max = Math.max(Width,Height);


if(Max >= 1200 && Max <= 1500){
var startRulerUnits = app.preferences.rulerUnits; 
app.preferences.rulerUnits = Units.PERCENT; 
var doc = activeDocument; 
var res= doc.resolution; 
var LB = activeDocument.activeLayer.bounds; 
var Width= LB[2].value - LB[0].value; 
var onePrc = 100/Width; 
var newSize = onePrc * 50;  // percent
var newHSize = onePrc * 50;  // percent
doc.activeLayer.resize( newSize ,  newHSize, AnchorPosition.MIDDLECENTER);  
app.preferences.rulerUnits = startRulerUnits; 

}


if(Max >= 1501 && Max <= 2100){
  var startRulerUnits = app.preferences.rulerUnits; 
app.preferences.rulerUnits = Units.PERCENT; 
var doc = activeDocument; 
var res= doc.resolution; 
var LB = activeDocument.activeLayer.bounds; 
var Width= LB[2].value - LB[0].value; 
var onePrc = 100/Width; 
var newSize = onePrc * 40;  // percent
var newHSize = onePrc * 40;  // percent
doc.activeLayer.resize( newSize ,  newHSize, AnchorPosition.MIDDLECENTER);  
app.preferences.rulerUnits = startRulerUnits; 
      
}

app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
}
Participant
April 7, 2020

WOW,

Thank you so much.

 

This works wonderful. I made adjustment for the other sizes that I needed and with your script I made one for landscape images as well.

 

Please have a blessed day and thank you so much again!!!