Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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,我想检查图像的高度或宽度是否更大。根据这一点,将定义一个最大值。如果是横向图像$limit=1000如果是垂直格式$limit=600 在此之后,可以正确缩放图像(最大宽度为1000px或600px,具体取决于图像的格式)。如果图像小于该值,则使用图像值。(只是缩小规模) 根据具体情况,我不知道如何实现这两个不同的极限值: $tmp = max($width, $height); # give me the bigger value $limit = min($limit, $tmp); # i

我想检查图像的高度或宽度是否更大。根据这一点,将定义一个最大值。如果是横向图像
$limit=1000如果是垂直格式
$limit=600

在此之后,可以正确缩放图像(最大宽度为1000px或600px,具体取决于图像的格式)。如果图像小于该值,则使用图像值。(只是缩小规模)

根据具体情况,我不知道如何实现这两个不同的极限值:

$tmp = max($width, $height); # give me the bigger value
$limit = min($limit, $tmp); # if image dimension is lower, take that value
$factor = $tmp / $limit; # calculate factor to get the new dimensions

$new_width = $width / $factor;
$new_height = $height / $factor;
因此,如果只有一个极限值(即1000),这将非常有效

我只需要这些值,不需要对图像进行真正的变换…

min(1,
仅当您只想缩小大图像而不想调整小图像的大小时才需要:

$maxWidth = 1000;
$maxWidthIfVertical = 600;
$factor = $width > $height ? min(1, $maxWidth / $width) : min(1, $maxWidthIfVertical / $width);
$newWidth = $width * $factor;
$newHeight = $height * $factor;
注意使用这种方法,请参阅我在这里添加的示例:我认为这并不是您真正想要的

因此,我建议这样做(宽度限制为1000,高度限制为800):

不确定是否存在误解。如果图像为4000x3000,则应将其转换为1000x750(最大宽度1000和750,以保持相同的比率)。如果图像为3000x4000,则结果应为600x800(最大宽度(!)600,计算高度为800。一张400x300的图像应该保留,因为它太小了。好的,你确定它是相关的,因为你会得到宽度大于1000(横向)和高度大于600(垂直格式)的图像。所以我的第一个代码:(建议使用滑块、图库等)或者你要求的:(我不建议这种做法)。