2010-04-03 32 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

相關問題