Jump to content

upload png with transparent background


dragon_sa

Recommended Posts

I have the following I use for uploading images, but I am having an issue with transparent background png images, they all get changed from transparent background to a black background. After doing some hunting around I see I neet to use imagealphablending and imagesavealpha to keep the trransparency, but it doesnt seem to be working I still get the black backgrounds, any ideas?

 

Here is the script

 

<?php
// Process new logo image
if ($_FILES['logo']['tmp_name']&&$_FILES['logo']['tmp_name']!="") {  
// set memory limit for image
ini_set("memory_limit","32M");
// Check file extension
$checkfile=$_FILES['logo']['name'];
$ext=substr($checkfile, -3);
// Redirect if not correct image type
if (!in_array($ext, array('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG'))) {
	header("Location: ../control_index.php?sponsors=error1");
// Close database connection
	mysql_close($link);
	exit ();
}
// Create logo image (300px wide)
$file=$_FILES['logo']['tmp_name'];
list($width, $height)=getimagesize($file);
// Scale to width
	$main_width=300;
	$main_height=$height*($main_width/$width);

if ($ext=="jpg"||$ext=="JPG") {
	$image=imagecreatefromjpeg($file);
	$newname="sponsor".time().".jpg";
}
if ($ext=="gif"||$ext=="GIF") {
	$image=imagecreatefromgif($file);
	$newname="sponsor".time().".gif";
}
if ($ext=="png"||$ext=="PNG") {
	$image=imagecreatefrompng($file);
	$newname="sponsor".time().".png";
}
// Create main image file
$main_image=imagecreatetruecolor($main_width, $main_height);
imagecopyresampled($main_image, $image, 0, 0, 0, 0, $main_width, $main_height, $width, $height);
$cp_file="../images/sponsors/".$newname;
$site_file="../../images/sponsors/".$newname;
if ($ext=="jpg"||$ext=="JPG") {
	imagejpeg($main_image, $cp_file, 100);
	imagejpeg($main_image, $site_file, 100);
}
if ($ext=="gif"||$ext=="GIF") {
	imagegif($main_image, $cp_file, 100);
	imagegif($main_image, $site_file, 100);
}
if ($ext=="png"||$ext=="PNG") {
// Turn off alpha blending and set alpha flag
	imagealphablending($main_image, false);
	imagesavealpha($main_image, true);
	imagepng($main_image, $cp_file, 9);
	imagepng($main_image, $site_file, 9);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/236489-upload-png-with-transparent-background/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.