上传带有透明背景的png图片时出现问题,结果为黑色背景。我想要的只是在上传png透明背景后,结果与原始文件相同。 这里是函数的代码
upload.php的
<?php
// Some code here to obtain MySQL data result set, like...
$result_set = mysqli_query( 'SELECT * FROM products_table_name WHERE 1 ORDER BY name ASC' );
$x = 1;
foreach( $result_set as $row ){
?>
<!-- This is product <?php echo($x); ?> code -->
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="sample image 1" alt="" />
<h2><?php echo $row['price']; ?></h2>
<p><?php echo $row['detail']; ?></p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2><?php echo $row['price']; ?></h2>
<h2><?php echo $row['ID']; ?></h2>
<p><?php echo $row['detail']; ?></p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
<?php
$x++;
}
然后这是表单上传提交后的操作
action.php的
function upload_sponsors($nama_file,$folder,$lokasi_file,$lebar,$tinggi){
$nama_folder = "../../../$folder/";
$ext = end((explode(".", $nama_file))); # extra () to prevent notice
list($lebar_asli, $tinggi_asli, $source_type) = getimagesize($lokasi_file);
if ($ext == "gif"){
$gambar_asli = imagecreatefromgif($lokasi_file);
} else if($ext =="png"){
$gambar_asli = imagecreatefrompng($lokasi_file);
} else {
$gambar_asli = imagecreatefromjpeg($lokasi_file);
}
$ukuran_asli = $lebar_asli / $tinggi_asli;
$ukuran = $lebar / $tinggi;
if ($ukuran_asli > $ukuran) {
$tinggi_sementara = $tinggi;
$lebar_sementara = ( int ) ($tinggi * $ukuran_asli);
} else {
/*jika gambar sama atau lebih tinggi*/
$lebar_sementara = $lebar;
$tinggi_sementara = ( int ) ($lebar / $ukuran_asli);
}
$gambar_sementara = imagecreatetruecolor($lebar_sementara, $tinggi_sementara);
imagecopyresampled($gambar_sementara,$gambar_asli,0, 0,0, 0,$lebar_sementara, $tinggi_sementara,$lebar_asli, $tinggi_asli);
/*Copy cropped region from temporary image into the desired GD image*/
$x_absis = ($lebar_sementara - $lebar) / 2;
$y_absis = ($tinggi_sementara - $tinggi) / 2;
$gambar_akhir = imagecreatetruecolor($lebar, $tinggi);
imagecopy($gambar_akhir,$gambar_sementara,0, 0,$x_absis, $y_absis,$lebar, $tinggi);
if ($ext == "gif"){
imagegif($gambar_akhir,$nama_folder.$nama_file);
} else if($ext =="png"){
imagepng($gambar_akhir,$nama_folder.$nama_file);
} else {
imagejpeg($gambar_akhir,$nama_folder.$nama_file);
}
imagedestroy($gambar_akhir);
}
答案 0 :(得分:1)
在复制重新采样之前,您需要使用透明色填充画布,如下所示:
imagefill( $gambar_sementara, 0, 0, 0x7fff0000);
0x7fff0000
是十六进制代码字符串,用于完全透明的颜色。您还可以使用imagecolorallocatealpha()
来构建透明色。
还需要使用imagesavealpha()
启用Alpha通道。
在您的代码中:
$gambar_sementara = imagecreatetruecolor($lebar_sementara, $tinggi_sementara);
imagesavealpha($gambar_sementara, true);
imagefill( $gambar_sementara, 0, 0, 0x7fff0000);
imagecopyresampled($gambar_sementara,$gambar_asli,0, 0,0, 0,$lebar_sementara, $tinggi_sementara,$lebar_asli, $tinggi_asli);
进一步向下:
$gambar_akhir = imagecreatetruecolor($lebar, $tinggi);
imagesavealpha($gambar_akhir, true);
imagefill( $gambar_akhir, 0, 0, 0x7fff0000);
imagecopy($gambar_akhir,$gambar_sementara,0, 0,$x_absis, $y_absis,$lebar, $tinggi);
见:
http://php.net/manual/en/function.imagefill.php http://php.net/manual/en/function.imagesavealpha.php http://php.net/manual/en/function.imagecolorallocatealpha.php