2011-03-21 22 views
0

これは私のコードですが、私は知らない理由が、私はそれのサイズが変更された垂直方向の画像をアップロードし、私は両側にある2本のブラックストライプを得るとき。ワイド画像ではほとんどの場合うまく動作しますが、画像によっては黒い縞も見えます。どうすれば修正できますか?PHP作物背の高い画像

私の目的は、すべての画像をトリミングすることで、私はトリミングしたい水平ちょうど私のボックスに合わせてヒント、および垂直はとても幅が同じであり、彼らは何の上下をカットされていません。

$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

+0

@Jacob - ユニットテストは何ですか? [ユニットテスト](http://en.wikipedia.org/wiki/Unit_testing)で – Francesco

+0

あなたが意図したとおりに、あなたのコードが動作していることを自信を与えるために、さまざまなシナリオのためのテストケースを作成することができます。私はx、y、幅と高さを返す関数を作るだろう。その後、gd関数を使用します。 – Jacob

答えて

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

私は実際にこれを多く使っています。ここに私のコードはVERYあなたと同じです。違いや問題を自分で見つけられますか?

は私のコードでは、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); 
    }