2012-10-25 196 views
0

我有兩個圖像。一個是旋轉的寶麗來幀polaroid.jpg的jpg圖像。另一個只是一個普通的圖像image.jpg在背景圖像上旋轉圖像

我試圖旋轉圖像,然後把它放在寶麗來圖像的頂部,然後將合併的圖像顯示爲一個jpg圖像。

我想我和下面的代碼非常接近,但我無法設法讓透明度工作。旋轉圖像的未覆蓋區域是黑色而不是透明區域。我究竟做錯了什麼?我已經爲與獲得頂部圖像透明背景相關的各行添加了評論。

$bg_src = "polaroid.jpg"; 

$img_src = "image.jpg"; 

$outputImage = imagecreatefromjpeg($bg_src); 

$img = imagecreatefromjpeg($img_src); 

// This should create transparent background. 
$bgd_color = imagecolorallocatealpha($img, 0, 0, 0, 127); 

// This should assign the transparent background to the uncovered zone after rotation 
$img = imagerotate($img, 10, $bgd_color); 

// This should make sure the alpha transparency gets saved 
imagesavealpha($img, true); 

$img_x = imagesx($img); 

$img_y = imagesy($img); 

imagecopymerge($outputImage,$img,156,50,0,0,$img_x,$img_y,100); 

header('Content-type: image/jpeg'); 

imagejpeg($outputImage); 

imagedestroy($outputImage); 

回答

1

在一些升沉搜索後找出它。結果很簡單。 我只是改變這一行:

imagesavealpha($img, true); 

這樣:

imagecolortransparent($img,$bgd_color); 

耶! :)