简体   繁体   English

PHP图像调整大小最小宽度高度

[英]PHP Image resize minimum width height

Can someone help me with an equation. 有人可以帮我解决一个问题。 I'd like to resize an image down so that it has a minimum width or height of 200px. 我想缩小图像的大小,使其最小宽度或高度为200像素。 So, if the original is 500 x 300 it'd end up being 333 x 200 and if the original was 500 x 900 it'd end up being 200 x 360 and so on. 因此,如果原始尺寸为500 x 300,则最终尺寸为333 x 200,如果原始尺寸为500 x 900,则最终尺寸为200 x 360,依此类推。 If either of the original dimensions are smaller than 200px, no resizing is done. 如果两个原始尺寸均小于200像素,则不会进行大小调整。

I'd appreciate any input on this. 我对此表示感谢。 I am sure its quite simple, I just can't seem to work it out. 我确信它非常简单,我似乎无法解决。

You just need to work out the existing image ratio and do the math from that. 您只需要计算出现有的图像比例,然后从中进行数学计算即可。 The ratio is the images width compared to height or vice versa (usually which ever is longer is divided by the shorter to give you a ratio >= 1). 该比率是图像的宽度与高度的比值,反之亦然(通常是将较长的时间除以较短的时间,以得到大于等于1的比率)。 for example width 450 and height 300 has a ratio of 1.5 wide. 例如宽度450和高度300的比率为1.5宽。 meaning that the image is 1.5 times wider than it is tall. 表示图像的宽度是其高度的1.5倍。 Then you can multiply your "minimum" value by the ratio to get the other size or as in the example if your height is 200 the other side will be minimum * ratio or 200 * 1.5 which makes the size 300 wide and 200 tall. 然后,您可以将“最小”值乘以比例以获得其他尺寸,或者在示例中,如果您的身高为200,则另一侧将是最小*比例或200 * 1.5,这将使尺寸为300宽,200高。

$height = 500;
$width = 300;
$min = 200;
if($width > $height){
    $ratio = $width/$height;
    $height = $min;
    $width = round($min * $ratio);
} else  {
    $ratio = $height/$width;
    $width = $min;
    $height = round($min * $ratio);
}

echo "Width: $width<br>";
echo "Height: $height<br>";

http://codepad.viper-7.com/RsxAsC http://codepad.viper-7.com/RsxAsC

Aparently, you'd like to have the smallest side to fit 200px, but dont upscale. 显然,您希望最小的一面适合200像素,但不要放大。 I'd do something like this 我会做这样的事情

if ($width > 200 || $height > 200) {
     if ($width > $height) {
          $ratio = (200 / $height);
     } else {
          $ratio = (200 / $width);
     }

     $resizedWidth = $ratio * $width;
     $resizedHeight = $ratio * $height;
}

Not complete code, just a starting point, hope this helps 不完整的代码,只是一个起点,希望对您有所帮助

if your server has the php imagick extension installed you can use the function cropThumbnailImage() 如果您的服务器安装了php imagick扩展名,则可以使用函数cropThumbnailImage()

http://php.net/manual/en/imagick.cropthumbnailimage.php http://php.net/manual/zh/imagick.cropthumbnailimage.php

I altered Jonahtan answer to be a bit shorter and where you only calculate a ratio once: 我将乔纳丹(Jonahtan)的答案改短了一些,只计算了一个比率:

// Set image dimensions for testing
$imgWidth = 100;
$imgHeight = 250;

// Minimal dimension requirements 
$minDim = 300;

if ($imgWidth < $minDim || $imgHeight < $minDim) {
    $ratio = $imgWidth / $imgHeight;
    if( $ratio > 1) {   // Width is greater
        $imgHeight = $minDim;
        $imgWidth = round($minDim * $ratio);
    }
    else {  // Height is greater
        $imgWidth = $minDim;
        $imgHeight = round($minDim / $ratio);
    }
}

echo "Width: $imgWidth<br>";
echo "Height: $imgHeight<br>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM