2012-02-27 239 views
2

我一直在努力弄清楚如何在PHP中调整上传图片的大小,使其不会小于给定大小(650x650)。但是,如果用户上传的图片在任一边上都小于我的650最小值,则不会采取任何操作。将图片大小调整为最小宽度/高度

场景1 - 一幅2000像素宽的371像的图像被更新 - 这不会被调整大小,因为371像素已经小于我的最小值。

场景2 - 上传一张2000像素到1823px的图像 - 这里我应该调整图像的大小尽可能接近最小,但不允许宽度或高度低于650像素。

这是我一直沿着到目前为止思考(我使用的是优秀的simpleImage脚本,以帮助调整和获取尺寸)行:

$curWidth = $image->getWidth(); 
$curHeight = $image->getHeight();    
$ratio = $curWidth/$curHeight; 

if ($curWidth>$minImageWidth && $curHeight>$minImageHeight) 
{ 
    //both dimensions are above the minimum, so we can try scaling 
    if ($curWidth==$curHeight) 
    { 
     //perfect square :D just resize to what we want 
     $image->resize($minImageWidth,$minImageHeight); 
    } 
    else if ($curWidth>$curHeight) 
    { 
     //height is shortest, scale that. 
     //work out what height to scale to that will allow 
     //width to be at least minImageWidth i.e 650. 
     if ($ratio < 1) 
     { 
      $image->resizeToHeight($minImageWidth*$ratio); 
     } 
     else 
     { 
      $image->resizeToHeight($minImageWidth/$ratio); 
     } 
    } 
    else 
    { 
     //width is shortest, so find minimum we can scale to while keeping 
     //the height above or equal to the minimum height. 
     if ($ratio < 1) 
     { 
      $image->resizeToWidth($minImageHeight*$ratio); 
     } 
     else 
     { 
      $image->resizeToWidth($minImageHeight/$ratio); 
     } 
} 

但是这给了我一些奇怪的结果,有时它仍然会低于最小值。其中唯一符合预期的部分是对尺寸在最小值以上的测试 - 它不会缩小任何太小的尺寸。

我认为我最大的问题在于,我没有完全理解图像宽高比和尺寸之间的关系,以及如何计算出我能够缩放到哪些尺寸高于最小值。有什么建议么?

+1

我对第一句话感到困惑。 “宽度和高度不小于设定的最小值”...然后它说2000x371不会调整大小,因为它太短。你能澄清你需要的规则吗?或者,也许我误解了你。 – jon 2012-02-27 14:58:21

+0

我会更新,使其更清晰 - 基本上我想重新缩小到650最小宽度和高度的东西。也不应少 - 但如果有人上传的图像已经很小,则不应采取任何行动。 – jammypeach 2012-02-27 15:01:29

回答

4

试试这个:

$curWidth = $image->getWidth(); 
$curHeight = $image->getHeight();    
$ratio = min($minImageWidth/$curWidth,$minImageHeight/$curHeight); 
if ($ratio < 1) { 
    $image->resize(floor($ratio*$curWidth),floor($ratio*$curHeight)); 
} 

或本:

$image->maxarea($minImageWidth, $minImageHeight); 
+1

为我工作,谢谢:)像往常一样,我在脑海中解决了这个问题。不得不停止这样做... – jammypeach 2012-02-27 15:17:28

2

试试这个:

$curWidth = $image->getWidth(); 
$curHeight = $image->getHeight();    
if ($curWidth>$minImageWidth && $curHeight>$minImageHeight) { 
    $ratio = $curWidth/$curHeight; 
    $zoom = image_ratio > 1 
     ? $minImageHeight/$curHeight 
     : $minImageWidth/$curWidth 
     ; 

    $newWidth = $curWidth * $zoom; 
    $newHeight = $curHeight * $zoom; 
} 
0

通用片段计算图像的最小尺寸,但只有两个维度大于最小尺寸。

list($width, $height) = getimagesize($image_dir); // Generic width and height of image 

// Define minimum size for image 
$min_width = 700; // Example minimum width 
$min_height = 500; // Example minimum height 

// Only scale if the image is larger than the minimum size 
if ($width > $min_width && $height > $min_height) { 

    // Determine the ratio which the image should be scaled with 
    $ratio = $min_height/$min_width < $height/$width ? $min_height/$height : $min_width/$width; 

    // Set new size 
    $new_width = $width * $ratio; 
    $new_height = $height * $ratio; 

    // Resize image 
    # image_resize($image, $new_width, $new_height) // Generic resize function 
} 

该算法的最小尺寸比($min_height/$min_width),以将当前图像尺寸比($height/$width)进行比较。

  1. 如果当前图像比率越大,图像需要被缩放,以高度等于$min_height。设置$ratio = $height/$min_height
  2. 如果当前图像比率较低,则图像需要缩放,因此宽度等于$min_width。设置$ratio = $width/$min_width

要获得正确的尺寸,请将宽度和高度乘以$ratio

$new_width = $width * $ratio; 
$new_height = $height * $ratio; 
相关问题