dragon_sa Posted May 15, 2011 Share Posted May 15, 2011 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); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.