2011-03-21 65 views
0

這是我的代碼,但我不知道爲什麼,但是當我上傳一個垂直圖像時,它會調整大小,並且我會在兩側獲得兩個黑色條紋。隨着寬大的圖像,它大部分時間運作良好,但有一些圖像我也得到黑色條紋。我該如何解決它?PHP crop高圖像

我的目的是裁剪每一個圖像,水平只是一個提示,以適應我的盒子,垂直我想裁剪所以寬度是相同的,他們沒有頂部和底部切。

$thumb_height = 200; 
$thumb_width = 300; 

    $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
    if($width >= $height) { 
     // If image is wider than thumbnail (in aspect ratio sense) 
     $new_height = $thumb_height; 
     $new_width = $width/($height/$thumb_height); 
    } else { 
     // If the thumbnail is wider than the image 
     $new_width = $thumb_width; 
     $new_height = $height/($width/$thumb_width); 
    } 
    $output_filename_mid = 'uploads/'.IMG_L.$fileName;  
    imagecopyresampled($thumb, 
         $image, 
         0 - ($new_width - $thumb_width)/2, // Center the image horizontally 
         0 - ($new_height - $thumb_height)/2, // Center the image vertically 
         0, 0, 
         $new_width, $new_height, 
         $width, $height); 
    imagejpeg($thumb, $output_filename_mid, 85); 
+0

這是使用單元測試的最佳例子。 – Jacob 2011-03-21 05:38:59

+0

@Jacob - 什麼是單元測試? – Francesco 2011-03-21 21:25:31

+0

通過[單元測試](http://en.wikipedia.org/wiki/Unit_testing),您可以爲不同場景創建測試用例,讓您確信代碼能夠按預期工作。我會做一個返回x,y,寬度和高度的函數。然後,一旦你有他們使用GD功能。 – Jacob 2011-03-21 22:01:17

回答

1

你幾乎沒有。你已經發現你需要確定舊高度和目標高度之間的比率,以調整裁剪邊的大小。但是,您需要確定這與目標比率有關。

if(($width/$height) > ($thumb_width/$thumb_height)) { 
    // If image is wider than thumbnail (in aspect ratio sense) 
    $new_height = $thumb_height; 
    $new_width = $width/($height/$thumb_height); 
} else { 
    // If the thumbnail is wider than the image 
    $new_width = $thumb_width; 
    $new_height = $height/($width/$thumb_width); 
} 
0

我實際上在處理這個問題。這裏是我的代碼是非常類似於你的。你能自己找到差異/問題嗎?

我的代碼100%正常工作,完全符合您的需求。隨意使用它!

變量解釋:$ imagewidth和$ imageheight自然是輸入圖像的原始像素大小。 $ crop_ratio_w是作物比例寬度,$ crop_ratio_h是作物比例高度。

所以對我的代碼對你的代碼:$ crop_ratio_w = $ thumb_width和$ crop_ratio_h = $ thumb_height

//do this if we can start cropping by width (there's enough space on height) 
    if (($imagewidth * $crop_ratio_h/$crop_ratio_w) < $imageheight){ 
     //count new res 
     $new_width = $imagewidth; 
     $new_height = ($imagewidth * $crop_ratio_h/$crop_ratio_w); 
     //count the height difference, so that new image is cropped in HEIGHT center 
     $difference = ($imageheight - $new_height); 
     $y_offset = ($difference/2); 
     //create new empty image 
     $croppedImage = imagecreatetruecolor($new_width, $new_height); 
     //copy wanted area to the new empty image 
     imagecopy($croppedImage, $originalImage, 0, 0, 0, $y_offset, $new_width, $new_height); 
    } 
//else on previous condition -- do this if we have to start cropping by height (there's not enough space on height) 
    else{ 
     //count new res 
     $new_width = ($imageheight * $crop_ratio_w/$crop_ratio_h); 
     $new_height = $imageheight; 
     //count the height difference, so that new image is cropped in WIDTH center 
     $difference = ($imagewidth - $new_width); 
     $x_offset = ($difference/2); 
     //create new empty image 
     $croppedImage = imagecreatetruecolor($new_width, $new_height); 
     //copy wanted area to the new empty image 
     imagecopy($croppedImage, $originalImage, 0, 0, $x_offset, 0, $new_width, $new_height); 
    }