2012-03-01 67 views
1

這裏我的問題:添加一些不透明的圖像與imagecopymerge在PHP

我想改變圖像的透明度,通過複製它在另一個透明圖像。

我的代碼:

$opacity = 50; 

$transparentImage = imagecreatetruecolor($width, $height); 
imagesavealpha($transparentImage, true); 
$transColour = imagecolorallocatealpha($transparentImage , 0, 0, 0, 127); 
imagefill($transparentImage , 0, 0, $transColour); 

imagecopymerge($transparentImage, $image, 0, 0, 0, 0, $width, $height, $opacity); 

$image = $transparentImage; 

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

通過這樣做,當我使用imagecopymerge,$ transparentImage失去了它的透明度......所以$圖像合併黑色圖像上...而不是透明的圖像!

但是,當我在調用imagecopymerge之前顯示$ transparentImage時,圖像在我的導航器中是透明的!

是否有解決方案來改變我的圖像的不透明度,而不是在彩色背景上添加它?

回答

1

看來,imagecopymergedoes not support the alpha(透明度)渠道的圖像。幸運的是,您可以使用imagecopy的解決方法正確執行此操作。這裏有一個函數來做到這一點,從評論上php.net採取:

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    // creating a cut resource 
    $cut = imagecreatetruecolor($src_w, $src_h); 

    // copying relevant section from background to the cut resource 
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 

    // copying relevant section from watermark to the cut resource 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 

    // insert cut resource to destination image 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
} 
+0

你好,其實我想保留我的$ dst_im和,這是透明的,而不是$ src_im分別只是透明度的透明度: ( – Sybio 2012-03-04 15:46:20