2012-04-19 65 views
0

我使用代码来创建一个图像与PHP GD需要一个完全透明的背景。当我创建它时,它在浏览器中显示正常,但在我的iPhone应用程序中显示错误。我不知道为什么,但在我的iPhone中,它显示黑色的所有透明度。这似乎是一个GD问题,因为当我将我的GD映像加载到Web编辑器并重新导出它时,它在我的iPhone应用程序中显示得很好。是否有一种特殊的方式,我应该从GD或其他东西导出PNG图像,或者这是一种错误?这里是代码:PHP的GD透明不适用于iPhone

$filename = "./me.jpg"; 

$image_s = imagecreatefromjpeg($filename); 

list($current_width, $current_height) = getimagesize($filename); 

$left = isset($_GET['pl']) ? abs($_GET['pl']) : 0; 
$top = isset($_GET['pt']) ? abs($_GET['pt']) : 0; 

$width = isset($_GET['cs']) ? abs($_GET['cs']) : 65; 
$height = isset($_GET['cs']) ? abs($_GET['cs']) : 65; 

$canvas = imagecreatetruecolor($width, $height); 
$current_image = imagecreatefromjpeg($filename); 
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); 

$newwidth = 65; 
$newheight = 65; 

$image = imagecreatetruecolor($newwidth, $newheight); 
imagealphablending($image, true); 
imagecopyresampled($image, $canvas, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

$mask = imagecreatetruecolor($newwidth, $newheight); 

$transparent = imagecolorallocate($mask, 255, 255, 255); 
imagecolortransparent($mask, $transparent); 

imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent); 

$red = imagecolorallocate($mask, 0, 0, 0); 
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth + 10, $newheight + 10, 100); 
imagecolortransparent($image, $red); 
imagefill($image,0,0, $red); 

header('Content-type: image/png'); 
imagepng($image); 
imagedestroy($image); 
imagedestroy($mask); 

回答

0

您是否尝试过使用imagesavealpha而不是imagesettransparency?将alpha混合设置为false,然后将imagesavealpha设置为true。最后,您将调用imagecolorallocatealpha函数来获取透明/ alpha颜色而不是imagesettransparency:

imagealphablending($image, false); 
imagesavealpha($image, true); 

$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127); 

imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent); 
etc...