Skip to main content
Participant
February 19, 2024
解決済み

Action or script to automatically make image square regardless of original size or orientation

  • February 19, 2024
  • 返信数 1.
  • 1869 ビュー

Hi all, I 'm looking to hopefully find (preferred) or create (if needed) an action or script that will automatically determine what the longest side of an opened file is and then change the shorter side of the canvas to the same pixel dimension as the longest one was so the file ends up with a square aspect ratio. I want this to work regardless of the original pixel dimensions of the file, the action/script should not need that to make the file square. The reason I want it to work this way is because we have many images that are all over the map in size and I want them, at this point in time, to stay that way. I just need them to be square instead of portrait or landscape. Secondly any extra canvas that is created should be filled with white. Does this exist or is there a specific way to record the action? I have tried in vain to create one or find one and have exceeded my ability level. Thank you in advance for any insight!

解決に役立った回答 Stephen Marsh

@alimedi77729338 

 

You're welcome. Please mark the appropriate reply or replies as a correct answer to resolve the status of the discussion.

 

Searching for keywords of square or 1:1 does take some time! Most topics would likely be using fit image and canvas size as the requirement would be to normalise files to specific pixel dimensions. Using the rotated layer trick is less common, however, it's required in this case as you need variable dimensions based off each individual image. Without the rotated layer trick you would likely need a script for performing such logic.

返信数 1

Stephen Marsh
Community Expert
Community Expert
February 19, 2024

Yes, an action can (mostly) do this, there's no need for a script unless you prefer one.

 

The original image is copied to a new layer and rotated 90 degrees and a reveal all step to expand the canvas. Then delete the rotated layer. There are other steps, but these are the key ones.

 

Various steps will depend on whether you only have flattened images, or layered images and whether you wish to flatten the result of layers or retain the layers.

 

EDIT: Original action modified and download link updated!

 

 

Action download:

https://www.dropbox.com/scl/fi/15uyh98xpqce58x114gk9/Resize-to-Square-with-White-BG.atn?rlkey=6edi3kc83q4f14onj4clo525y

 

P.S. There are many topics in the forum for creating square 1:1 ratio images.

 

Participant
February 20, 2024

Thank you Stephen! This is super helpful and a solution I wouldn't have thought of. I did try to search for the topic but was not having great success with my search terms to find this type of answer. Thanks again!!

Inspiring
February 20, 2024

I propose 2 solutions
the first normal

    var doc = app.activeDocument;
    var docW = parseFloat(doc.width.as("px"));  
    var docH = parseFloat(doc.height.as("px")); 
    if (docW > docH){  
 
        doc.resizeCanvas(UnitValue(docW, "px"), UnitValue(docW, "px"));
    }else{
        doc.resizeCanvas(UnitValue(docH, "px"), UnitValue(docH, "px"));
      
    }

 

The second solution allows you to resize the image

 

resizeARC(900);
function resizeARC(size){
    var doc = app.activeDocument;
    var docW = parseFloat(doc.width.as("px"));  
    var docH = parseFloat(doc.height.as("px")); 
    if (docW > docH){  
      
        doc.resizeCanvas(UnitValue(docW, "px"), UnitValue(docW, "px"));
           doc.resizeImage(UnitValue(size, "px"));
    }else{
        
        doc.resizeCanvas(UnitValue(docH, "px"), UnitValue(docH, "px"));
        doc.resizeImage(undefined, UnitValue(size, "px"),);
    }
}

 

Please note that the background color is used as a color fill.