Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取图像纵横比_Php - Fatal编程技术网

Php 获取图像纵横比

Php 获取图像纵横比,php,Php,我目前正在尝试使用PHP获取图像的纵横比。例如,使用这些值: $thumb_width = 912; $thumb_height = 608; 我可以得到纵横比(16:9,3:2,2:3,等等)。因此,在这种情况下: $ratio = '3:2'; 问题是:我不知道它将是什么新的宽度或高度。所以我不能这样做:(原始高度/原始宽度)x新宽度=新高度 有什么想法吗 注意:我不想调整图像的大小,只需计算其宽高比。因此需要获得图像的大小 所以这里您已经准备好了$width和$height 然后您可

我目前正在尝试使用PHP获取图像的纵横比。例如,使用这些值:

$thumb_width = 912;
$thumb_height = 608;
我可以得到纵横比(16:9,3:2,2:3,等等)。因此,在这种情况下:

$ratio = '3:2';
问题是:我不知道它将是什么新的宽度或高度。所以我不能这样做:
(原始高度/原始宽度)x新宽度=新高度

有什么想法吗


注意:我不想调整图像的大小,只需计算其宽高比。

因此需要获得图像的大小

所以这里您已经准备好了
$width
$height


然后您可以在这里使用这个答案将
$width/$height
的结果转换为一个比率。

我总是喜欢编码中最简洁的解决方案。它们大多更快、更健壮、更易于阅读

虽然在中提到了这一点,但我认为以下解决方案更为优雅,更具可读性:

$imageWidth = 912;
$imageHeight = 608;

$divisor = gmp_intval( gmp_gcd( $imageWidth, $imageHeight ) );
$aspectRatio = $imageWidth / $divisor . ':' . $imageHeight / $divisor;
有关和的PHP手册中函数的更多信息。

这对您有帮助吗:?
$imageWidth = 912;
$imageHeight = 608;

$divisor = gmp_intval( gmp_gcd( $imageWidth, $imageHeight ) );
$aspectRatio = $imageWidth / $divisor . ':' . $imageHeight / $divisor;