Skip to main content
RRowe
Inspiring
January 30, 2025
Answered

Any quick way to see the lowest equivalent ratio of either the document size or object size?

  • January 30, 2025
  • 4 replies
  • 602 views

I often need the lowest ratio of a rectangle (or sometimes the document size). Sometimes that is because i need to make a matching selection and I'm limited to using only numbers < 1000, so I need to reduce the ratio. I'm wondering if there is a quick way to do this that I am missing. 

Simple example, I have a rectangle that is 1869 x 4361. That is the exact equivalent ratio of 3 x 7. 
Is there a quick way to see that reduction without having to manually find the greatest common denominator of 623 (in this case)?

To be clear, this is not to hit a certain ratio. This would be to find the existing lowest ratio of something that already exists.
Thanks!

Correct answer Stephen Marsh

@RRowe 

 

I have a script which offers 3 different formulas for calculating aspect ratios, including GCD:

 

https://github.com/MarshySwamp/Photoshop-Document-Dimensions---MP-Value---Aspect-Ratio/blob/main/Photoshop%20Document%20Dimensions%20-%20MP%20Value%20-%20Aspect%20Ratio.jsx

 

It's for the active document, a version could be created for the active layer width and height.

 

EDIT: I forgot that I created a version for a selection, so you can load the layer transparency as a selection and then use that script:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/find-crop-aspect-ratio-from-selection-or-image-solved/m-p/12981892#M647675

4 replies

Stephen Marsh
Community Expert
Community Expert
January 30, 2025

The following script is for a single active layer – otherwise, it will report on the document:

 

/*
Photoshop Active Layer or Document Dimensions - Aspect Ratio.jsx
v1.0, Stephen Marsh 31st January 2025
Based on:
https://github.com/MarshySwamp/Photoshop-Document-Dimensions---MP-Value---Aspect-Ratio/blob/main/Photoshop%20Document%20Dimensions%20-%20MP%20Value%20-%20Aspect%20Ratio.jsx
*/

#target photoshop

// Check for an active layer by jazz-y
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getList(p).count) {

    var savedRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    var bounds = app.activeDocument.activeLayer.bounds;

    var w = bounds[2].value - bounds[0].value; // Width based on layer bounds
    var h = bounds[3].value - bounds[1].value; // Height based on layer bounds
    var r = gcd(w, h);
    var ratio = w / h;

    alert(
        'Layer Dimensions: ' + w + ' x ' + h + ' pixels' + '\n' +
        'Aspect Ratio:' + '\n' + ratio.toFixed(2) + ':1' + ' / ' + ratio.toFixed(2) * 2 + ':2 / ' + ratio.toFixed(2) * 4 + ':4' + ' (Basic)' + '\n' +
        w / r + ':' + h / r + ' (GCD)' + '\n' +
        aspect_ratio(w / h, 50).toString().replace(',', ':') + ' (Farey)'
    );

    app.preferences.rulerUnits = savedRuler;

    function gcd(a, b) {
        /* https://stackoverflow.com/questions/1186414/whats-the-algorithm-to-calculate-aspect-ratio-i-need-an-output-like-43-169 */
        return (b == 0) ? a : gcd(b, a % b);
    }

} else {
    // Fallback to document dimensions if no layer is active
    var savedRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    var doc = app.activeDocument;
    var w = doc.width.value; // Width based on document dimensions
    var h = doc.height.value; // Height based on document dimensions
    var r = gcd(w, h);
    var ratio = w / h;

    alert(
        'Document Dimensions: ' + w + ' x ' + h + ' pixels' + '\n' +
        'Aspect Ratio:' + '\n' + ratio.toFixed(2) + ':1' + ' / ' + (ratio.toFixed(2) * 2) + ':2' + ' / ' + (ratio.toFixed(2) * 4) + ':4' + ' (Basic)' + '\n' +
        (w / r) + ':' + (h / r) + ' (GCD)' + '\n' +
        aspect_ratio(w / h, 50).toString().replace(',', ':') + ' (Farey)'
    );

    app.preferences.rulerUnits = savedRuler;

    function gcd(a, b) {
        /* https://stackoverflow.com/questions/1186414/whats-the-algorithm-to-calculate-aspect-ratio-i-need-an-output-like-43-169 */
        return (b == 0) ? a : gcd(b, a % b);
    }
}


function aspect_ratio(val, lim) {

    var lower = [0, 1];
    var upper = [1, 0];

    while (true) {
        var mediant = [lower[0] + upper[0], lower[1] + upper[1]];

        if (val * mediant[1] > mediant[0]) {
            if (lim < mediant[1]) {
                return upper;
            }
            lower = mediant;
        } else if (val * mediant[1] == mediant[0]) {
            if (lim >= mediant[1]) {
                return mediant;
            }
            if (lower[1] < upper[1]) {
                return lower;
            }
            return upper;
        } else {
            if (lim < mediant[1]) {
                return lower;
            }
            upper = mediant;
        }
    }
}

 

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Rob_Cullen
Community Expert
Community Expert
January 30, 2025

Or use an 'online' calculator-  Ratio Calculator - Lowest Common Denominator 

 

Regards. My System: Windows-11, Lightroom-Classic 15.1.1, Photoshop 27.3.1, ACR 18.1.1, Lightroom 9.0, Lr-iOS 10.4.0, Bridge 16.0.2 .
Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 30, 2025

@RRowe 

 

I have a script which offers 3 different formulas for calculating aspect ratios, including GCD:

 

https://github.com/MarshySwamp/Photoshop-Document-Dimensions---MP-Value---Aspect-Ratio/blob/main/Photoshop%20Document%20Dimensions%20-%20MP%20Value%20-%20Aspect%20Ratio.jsx

 

It's for the active document, a version could be created for the active layer width and height.

 

EDIT: I forgot that I created a version for a selection, so you can load the layer transparency as a selection and then use that script:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/find-crop-aspect-ratio-from-selection-or-image-solved/m-p/12981892#M647675

RRowe
RRoweAuthor
Inspiring
January 31, 2025

That top-link script worked great and is a perfect solution. You ALWAYS nail it! Thank you again.

Stephen Marsh
Community Expert
Community Expert
February 1, 2025
quote

That top-link script worked great and is a perfect solution. You ALWAYS nail it! Thank you again.


By @RRowe

 

Thank you, glad this helped!

c.pfaffenbichler
Community Expert
Community Expert
January 30, 2025

Only integers or, if not, how many digits after the comma? 

RRowe
RRoweAuthor
Inspiring
January 30, 2025

Oh good point! I'd say integers only. So I know that makes it imperfect to be an exact ratio. Down to the nearest integer would be very helpful, even though not exact.

c.pfaffenbichler
Community Expert
Community Expert
January 30, 2025

Well, then you might (sometimes) just get pixelwidth:pixelheight. 

 

If something like 1:1,414… suffices you could try this: 

// 2025, use it at your own risk;
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myDocument = app.activeDocument;
var width = myDocument.width;
var height = myDocument.height;
app.preferences.rulerUnits = originalRulerUnits;
alert ("1:"+height/width);
};