2017-10-27 12 views
0

これは重複している可能性がありますが、私が調べたすべての解決策は私にとってはうまくいかないと思われます。PHPでイメージを切り取ると黒いスペースが残る

まだ画像操作についてあまり理解していませんが、imagecopyresampledを使用して別の画像の上に配置すると、画像の右下に黒いスペースがたくさん表示されるようです。それらはすべてJPEGです。

これは私のクロップ機能である:

function thumbImage($thumb_img,$img_size,$shape){ 
    $width = 250; 
    $height = 250; 

    list($w, $h) = $img_size; 

    if($w > $h) { 
      $new_height = $height; 
      $new_width = floor($w * ($new_height/$h)); 
      $crop_x  = ceil(($w - $h)/2); 
      $crop_y  = 0; 
    } else { 
      $new_width = $width; 
      $new_height = floor($h * ($new_width/$w)); 
      $crop_x  = 0; 
      $crop_y  = ceil(($h - $w)/2); 
    } 

    $tmp_img = imagecreatetruecolor($width,$height); 
    imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h); 

    return $tmp_img; 
} 

それが大幅に高く評価されるだろうどのように動作するかの説明と任意のソリューション。

答えて

0

はこれを試してみてください:

function thumbImage($source_img,$max_size){ 

    list($w, $h) = getimagesize($source_img); 

    if($w>$h){ 
     $width = ($max_size/$h)*$w; 
     $height = $max_size; 
    }else{ 
     $width = $max_size; 
     $height = ($max_size/$w)*$h; 
    } 

    $x = ($max_size-$width)/2; 
    $y = ($max_size-$height)/2; 

    $thumb_img = imagecreatetruecolor($max_size,$max_size); 
    imagecopyresampled($thumb_img, $source_img, $x, $y, 0, 0, $width, $height, $w, $h); 

    return $thumb_img; 
} 
+0

ありがとうございましたが、それはまったく同じ結果で問題はおそらくがある可能性があれば、私は別の上に画像を追加していたコードを追加することができますか? – Danny

+0

はい、投稿してください。私が投稿したコードはうまくいくはずです、私は問題なく自分で使っています。 –

関連する問題