作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在上传透明背景的png图片时遇到问题,结果会是黑色背景。我想要的只是在上传 png 透明背景后,结果将与原始文件相同。这里是函数代码
上传.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);
}
那么这是表单上传提交后的 Action
Action .php
include "../../config/upload.php";
$module=$_GET['module'];
$act=$_GET['act'];
if ($module=='sponsors' AND $act=='input'){
$ukuran_maksimal = 30000000;
$acak = rand(0000,9999);
$image_info = getimagesize($_FILES["fupload"]["tmp_name"]);
$lebar = $image_info[0]; // width of image
$tinggi = $image_info[1]; // height of image
//$lebar = 800;
//$tinggi = 534;
//$fold = $_POST['folder'];
$id_kategori = $_POST['id_kategori_blog'];
$folder = "images/";
$lokasi_file = $_FILES['fupload']['tmp_name'];
$tipe_file = $_FILES['fupload']['type'];
$nama_file = $_FILES['fupload']['name'];
$ukuran_file = $_FILES['fupload']['size'];
$nama_file = str_replace(' ', '_', $nama_file);
$nama_file = $acak.$nama_file;
upload_sponsors($nama_file,$folder,$lokasi_file,$lebar,$tinggi);
mysql_query("INSERT INTO sponsors(judul,link,isi,gambar)
VALUES('$_POST[judul]','$_POST[link]','$_POST[isi]','$nama_file')");
echo "<script>window.alert('Upload image succes');
window.location=('../../main.php?module=sponsors')</script>";
}
最佳答案
你需要用透明颜色填充 Canvas ,然后复制重采样,像这样:
imagefill( $gambar_sementara, 0, 0, 0x7fff0000);
0x7fff0000
是全透明颜色的十六进制代码串。您还可以使用 imagecolorallocatealpha()
构建透明色。
还需要使用imagesavealpha()
来开启alpha channel 。
在您的代码中:
$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
关于javascript - 透明背景PNG用于上传功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928810/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!