问题描述
我无法相信我找不到这个公式。我正在使用一个名为SLIR的PHP脚本来调整图像大小。该脚本要求指定裁剪的宽高比。我想根据我允许用户输入这些值的形式指定图像的宽度和高度来获得宽高比。例如,如果用户输入1024x768图像,我会得到宽高比4:3。对于我的生活,我找不到PHP或Javascript中的公式示例我可以用来获得基于知道w,h的宽高比值并将宽高比插入变量。
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.
推荐答案
您无需进行任何计算。
仅仅因为它表示宽高比并不意味着它必须是有限的一组。它可以是用冒号分隔的任意数字对。
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.
引自:
<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" />
请注意,他们没有费心去减少 c3:2
。
Note that they didn't bother to reduce that even further to c3:2
.
因此,只需使用用户输入的值: 1024:768
。
So, simply use the values as entered by the user: 1024:768
.
如果你想简明扼要,请计算并将它们分开。这会将你的 1024:768
降低到 4:3
。
If you want to be concise, calculate the greatest common divisor of the width and height and divide both of them by that. That would reduce your 1024:768
down to 4:3
.
这篇关于从图像的宽度和高度获取宽高比(PHP或JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!