2011-10-11 118 views
7

我使用此代碼從另一個png圖片創建一個圖像,該背景是黑色默認情況下。我的問題是如何設置透明背景?PHP imagecopy的具有透明背景

$input = imagecreatefrompng('image.png'); 
$output = imagecreatetruecolor(50, 50); 

imagecopy($output, $input, 4,0, 8,8, 8,8); 
imagecopy... etc. 

header('Content-Type: image/png'); 
imagepng($output); 

有沒有簡單的方法做到這一點?由於

回答

11

設置在給定的圖像透明色。

int imagecolortransparent (resource $image [, int $color ]) 

這裏的link

+1

是的!這工作:'$黑色= imagecolorallocate($輸出,0,0,0); 了imagecolortransparent($輸出,$黑色);'之前imagecopy的 – 2by

8

由於PHP函數imagecopymerge不與Alpha通道的工作,你會希望這個頁面imagecopymerge_alpha上使用的函數從第一個註釋: http://php.net/manual/en/function.imagecopymerge.php

只要有透明圖像爲基準並將它與您需要的圖像合併在一起。

我已經嘗試過了,它爲我的一個項目工作正常。

+0

這爲我工作。 – sinsuren

+0

也爲我工作。 –

2
imagealphablending($input, true); 
imagesavealpha($input, true); 

imagealphablending($output, true); 
imagesavealpha($output, true); 
+4

這是不正確,彎曲應該被設置爲'FALSE'在運行'imagesagealpha'之前。 – Martin