javascript 从图像的宽度和高度获取纵横比(PHP 或 JS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8044052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:05:05  来源:igfitidea点击:

Get aspect ratio from width and height of image (PHP or JS)

javascriptphpaspect-ratio

提问by David Paul Albert

I can't believe I can't find the formula for this. I am using a PHP script called SLIR to resize images. The script asks to specify an aspect ratio for cropping. I'd like to get the aspect ratio based on the width and height of the image being specified in a form I'm letting users enter these values in. For example, if a user enters a 1024x768 image, I would get an aspect ratio of 4:3. For the life of me, I can't find an example of the formula in PHP or Javascript I can use to get the aspect ratio values based on knowing the w,h and plug the aspect ratio into a variable.

我不敢相信我找不到这个公式。我正在使用名为 SLIR 的 PHP 脚本来调整图像大小。该脚本要求指定裁剪的纵横比。我想根据我让用户输入这些值的表单中指定的图像的宽度和高度来获取纵横比。例如,如果用户输入 1024x768 图像,我会得到纵横比4:3。对于我的生活,我找不到 PHP 或 Javascript 中的公式示例,我可以使用它来获取基于 w、h 的纵横比值并将纵横比插入变量中。

采纳答案by mercator

There is no need for you to do any kind of calculation.

您无需进行任何类型的计算。

Just because it says aspect ratio doesn't mean it has to be one of a limited set of commonly used ratios. It can be any pair of numbers separated by a colon.

仅仅因为它说纵横比并不意味着它必须是一组有限的常用比率之一。它可以是由冒号分隔的任何数字对。

Quoting from the SLIR usage guide:

引用SLIR 使用指南

For example, if you want your image to be exactly 150 pixels wide by 100 pixels high, you could do this:

<img src="/slir/w150-h100-c150:100/path/to/image.jpg" alt="Don't forget your alt text" /> 

Or, more concisely:

<img src="/slir/w150-h100-c15:10/path/to/image.jpg" alt="Don't forget your alt text" />

例如,如果您希望图像正好是 150 像素宽 x 100 像素高,您可以这样做:

<img src="/slir/w150-h100-c150:100/path/to/image.jpg" alt="Don't forget your alt text" /> 

或者,更简洁地说:

<img src="/slir/w150-h100-c15:10/path/to/image.jpg" alt="Don't forget your alt text" />

Note that they didn't bother to reduce that even further to c3:2.

请注意,他们并没有费心将其进一步减少到c3:2.

So, simply use the values as entered by the user: 1024:768.

所以,简单地使用值由用户输入:1024:768

If you want to be concise, calculate the greatest common divisorof the width and height and divide both of them by that. That would reduce your 1024:768down to 4:3.

如果要简洁,计算宽度和高度的最大公约数,然后除以它们。这将减少您的1024:768下降到4:3.

回答by David Nguyen

If you can get one of: height, width then you can calculate the missing width height:

如果您可以获得以下之一:高度,宽度,那么您可以计算缺少的宽度高度:

original width * new height / original height = new width;

原始宽度 * 新高度 / 原始高度 = 新宽度;

original height * new width / original width = new height;

原始高度 * 新宽度 / 原始宽度 = 新高度;

Or if you just want a ratio:

或者,如果您只想要一个比率:

original width / original height = ratio

原始宽度 / 原始高度 = 比率

回答by user962284

to get the aspect ratio just simplify the width and height like a fraction for example:

要获得纵横比,只需像分数一样简化宽度和高度,例如:

1024      4
----  =  ---
768       3

the php code:

php代码:

function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return abs( max(abs($a), abs($b)) );

    $r = $a % $b;
    return ($r != 0) ?
        gcd($b, $r) :
        abs($b);
}

  $gcd=gcd(1024,768);

  echo "Aspect ratio = ". (1024/$gcd) . ":" . (768/$gcd);

回答by neokio

Here's a much simpler alternative for greatest common divisor integer ratios:

这是最大公约数整数比的一个更简单的替代方案:

function ratio( $x, $y ){
    $gcd = gmp_strval(gmp_gcd($x, $y));
    return ($x/$gcd).':'.($y/$gcd);
}

The request echo ratio(25,5);returns 5:1.

请求echo ratio(25,5);返回5:1

If your server wasn't compiled with GMP functions ...

如果您的服务器没有使用 GMP 功能编译...

function gcd( $a, $b ){
    return ($a % $b) ? gcd($b,$a % $b) : $b;
}
function ratio( $x, $y ){
    $gcd = gcd($x, $y);
    return ($x/$gcd).':'.($y/$gcd);
}