从图像的宽度和高度获取宽高比(PHP或JS) [英] Get aspect ratio from width and height of image (PHP or JS)

查看:342
本文介绍了从图像的宽度和高度获取宽高比(PHP或JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法相信我找不到这个公式。我正在使用一个名为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.

引自 SLIR使用指南


例如,如果你想让你的图像正好是150像素宽,100像素高,你可以这样做:

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" /> 

或者,更简洁:

<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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆