7

php で私のコードを手伝ってもらえますか?

写真を透明にする方法がわかりません。アップロード後、背景が黒くなります。ここにコードがあります。(および小さな投稿とコンテンツのテキスト)

ありがとう。

<?php
function zmensi_obrazok($image_max_width, $image_max_height, $obrazok, $obrazok_tmp, $obrazok_size, $filename){

$postvars          = array(
"image"            => $obrazok,
"image_tmp"        => $obrazok_tmp,
"image_size"       => $obrazok_size,
"image_max_width"  => $image_max_width,
"image_max_height" => $image_max_height
);

$valid_exts = array("jpg","jpeg","png");
$ext = end(explode(".",strtolower($obrazok)));
if($postvars["image_size"] <= 1024000){


if(in_array($ext,$valid_exts)){




if($ext == "jpg" || $ext == "jpeg"){
$image = imagecreatefromjpeg($postvars["image_tmp"]);
}
else if($ext == "png"){
$image = imagecreatefrompng($postvars["image_tmp"]);
}


list($width,$height) = getimagesize($postvars["image_tmp"]);


$old_width      = imagesx($image);
$old_height     = imagesy($image);
$scale          = min($postvars["image_max_width"]/$old_width, $postvars["image_max_height"]/$old_height);
$new_width      = ceil($scale*$old_width);
$new_height     = ceil($scale*$old_height);


$tmp = imagecreatetruecolor($new_width,$new_height);

imagecopyresampled($tmp,$image,0,0,0,0,$new_width,$new_height,$width,$height);


imagejpeg($tmp,$filename,100);
return "";
imagedestroy($image);
imagedestroy($tmp);


}

}

}

?>
4

2 に答える 2

4

このリンクがあなたの質問に答えると思います: http://www.php.net/manual/pl/function.imagecopyresampled.php#104028

コードでは、答えは次のようになります。

// preserve transparency
  if($ext == "gif" or $ext == "png"){
    imagecolortransparent($tmp, imagecolorallocatealpha($tmp, 0, 0, 0, 127));
    imagealphablending($tmp, false);
    imagesavealpha($tmp, true);
  }

を実行する前にこれを貼り付けimagecopyresampledます。

于 2012-07-06T14:33:42.647 に答える