2013-02-02 94 views
0

我正在嘗試使用WideImage庫調整上傳的圖像的大小。圖像調整大小出錯

我需要將其轉換爲可能的最高分辨率。 的輸出中的大小應該是寬度:460像素高度:345px

這是我爲resize來源:

list($w, $h) = getimagesize($_FILES['image']['tmp_name']); 
$wpercent = ($w/460)*100; 
$hpercent = ($h/345)*100; 
if ($wpercent >= 100 && $hpercent >= 100) { 
    // Both is over 100% of the allowed size, then resize so the smaller side match. 
    if ($wpercent > $hpercent) { 
     // height is smallest 
     $remove_percent = $hpercent - 100; 
    }else{ 
     // width is smallest 
     $remove_percent = $wpercent - 100; 
    } 
    $new_h_percent = $hpercent - $remove_percent; 
    $new_w_percent = $wpercent - $remove_percent; 
    $new_w = ($w/460)*$new_w_percent; 
    $new_h = ($h/345)*$new_h_percent; 
    $img = $img->resize($new_w, $new_h, 'inside'); 
    $img = $img->crop("center", "middle", 460, 345); 
}else { 
    // En af dem er for små 
    if ($wpercent > $hpercent) { 
     // height is smallest 
     $add_percent = 100 - $hpercent; 
    }else{ 
     // width is smallest 
     $add_percent = 100 - $wpercent; 
    } 
    $new_h_percent = $hpercent + $add_percent; 
    $new_w_percent = $wpercent + $add_percent; 
    $new_w = ($w/460)*$new_w_percent; 
    $new_h = ($h/345)*$new_h_percent; 
    $img = $img->resize($new_w, $new_h, 'inside'); 
    $img = $img->crop("center", "middle", 460, 345); 
} 
$img->saveToFile('uploads/tmp/'.$new_name); 

它不調整,只爲更小的尺寸,然後我想要的。

任何想法?

此外,它應該在需要時進行裁剪。

回答

0
 list($w, $h) = getimagesize($_FILES['image']['tmp_name']); 
     if ($w != 460 || $h != 345) { 
      $img = $img->resize(460, 345, 'inside'); 
      $img = $img->crop("center", "middle", 460, 345); 
     } 
     $img->saveToFile('uploads/tmp/'.$new_name); 

你想用所有這些new_h_percent和new_w_percent完成什麼?