Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Javascript to resize a Photoshop layer based on the image size

Community Beginner ,
Apr 07, 2020 Apr 07, 2020

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. 

TOPICS
Actions and scripting
7.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Apr 07, 2020 Apr 07, 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.rulerUni
...
Translate
Advocate ,
Apr 07, 2020 Apr 07, 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;
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 07, 2020 Apr 07, 2020
LATEST

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!!!

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2020 Apr 07, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines