2010-04-03 100 views
15

我尝试过类似的东西,但它只是使图像背景变白,而不一定是图像的alpha。我想只是上传所有的JPG格式,所以如果我能以某种方式“透明地”压缩一个PNG图像,默认它只是白色,所以我可以用它作为JPG。感谢任何帮助。谢谢。GD!将png图像转换为jpeg,并使默认的alpha为白色而不是黑色

$old = imagecreatefrompng($upload); 
$background = imagecolorallocate($old,255,255,255); 
imagefill($old, 0, 0, $background); 
imagealphablending($old, false); 
imagesavealpha($old, true);

回答

50
<?php 
$input_file = "test.png"; 
$output_file = "test.jpg"; 

$input = imagecreatefrompng($input_file); 
list($width, $height) = getimagesize($input_file); 
$output = imagecreatetruecolor($width, $height); 
$white = imagecolorallocate($output, 255, 255, 255); 
imagefilledrectangle($output, 0, 0, $width, $height, $white); 
imagecopy($output, $input, 0, 0, 0, 0, $width, $height); 
imagejpeg($output, $output_file); 
+1

哇伟大的人感谢了一堆! – Shawn 2010-04-03 02:23:23

+0

+1,它使输出质量好很多 – dynamic 2011-03-15 10:01:48

+0

thx很多,为我节省了很多时间,并带来了非常好的效果 – chings228 2013-04-03 10:13:44

相关问题