2017-08-18 3 views
0

PHP関数を使用してイメージをトリミングしています。私の問題は、それが黒い背景を作ることです。 例: enter image description here白い背景のPHPトリミングイメージ

enter image description here

私のスクリプトは以下の通りです。関数resize_crop_imageです。親指を中央に動かし、上下を白い色で塗りつぶすことは可能ですか?あなたは白い背景で新たに作成された画像を入力する必要があり

<?php 

//resize and crop image by center 
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){ 
    $imgsize = getimagesize($source_file); 
    $width = $imgsize[0]; 
    $height = $imgsize[1]; 
    $mime = $imgsize['mime']; 

    switch($mime){ 
     case 'image/gif': 
      $image_create = "imagecreatefromgif"; 
      $image = "imagegif"; 
      break; 

     case 'image/png': 
      $image_create = "imagecreatefrompng"; 
      $image = "imagepng"; 
      $quality = 7; 
      break; 

     case 'image/jpeg': 
      $image_create = "imagecreatefromjpeg"; 
      $image = "imagejpeg"; 
      $quality = 80; 
      break; 

     default: 
      return false; 
      break; 
    } 

    $dst_img = imagecreatetruecolor($max_width, $max_height); 
    $src_img = $image_create($source_file); 

    $width_new = $height * $max_width/$max_height; 
    $height_new = $width * $max_height/$max_width; 
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa 
    if($width_new > $width){ 
     //cut point by height 
     $h_point = (($height - $height_new)/2); 
     //copy image 
     imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new); 
    }else{ 
     //cut point by width 
     $w_point = (($width - $width_new)/2); 
     imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height); 
    } 

    $image($dst_img, $dst_dir, $quality); 

    if($dst_img)imagedestroy($dst_img); 
    if($src_img)imagedestroy($src_img); 
} 
//usage 
resize_crop_image(300, 200, "original.png", "thumb.png"); 
?> 

答えて

1

は、次の2つのオプションがあります:

オプション1

$white = imagecolorallocate($dst_img, 255, 255, 255); 
imagefill($image, 0, 0, $white); 

オプションを2

$white = imagecolorallocate($targetImage,255,255,255); 
imagefilledrectangle($targetImage,0,0,$max_width,$max_height,$white); 
関連する問題