Agnes Puspasari Agnes Puspasari - 5 months ago 30
CSS Question

Transparancy Background PNG for upload function

I have problem when upload png picture with transparant background, the result will be black background. All i want is after upload the png transparent background the result will be same the original file.
here the code for function

upload.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);
}


then this is the action after form upload submitted

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>";

}

Answer Source

You need to fill canvas with transparent color, before copy resampled, like this:

imagefill( $gambar_sementara, 0, 0, 0x7fff0000);

0x7fff0000 is hexadecimal code string for fully transparent color. You can also use imagecolorallocatealpha() to build transparent color.

Also need to use imagesavealpha() to enable alpha channel.

In your code:

$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);

And further down:

$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);

See:

http://php.net/manual/en/function.imagefill.php http://php.net/manual/en/function.imagesavealpha.php http://php.net/manual/en/function.imagecolorallocatealpha.php