2014-12-07 74 views
2

我有一個具有圖像正常工作不透明度:imagecopymerge不與部分透明的圖像

enter image description here

當我嘗試加載它:

$img = imagecreatefrompng('cheek-01.png'); 
header('Content-Type: image/png'); 
imagepng($img); 

就會變成:

enter image description here

我w螞蟻添加(合併)此圖像與另一個圖像(我知道如何可以合併兩個PNG圖像)。 問題是怎麼回事,如何在不丟失透明度的情況下使用GD庫加載它?

更新

我的目標是合併一些圖片,其中有的已經阿爾法(不透明度不同,像上面的圖片),但是當我把它們合併阿爾法信息丟失。

<?php 
$background = imagecreatetruecolor(3508, 2480); 
// set background to white 
$white = imagecolorallocate($background, 255, 255, 255); 
imagefill($background, 0, 0, $white); 
$dest = $background; 
$items = array(
    'cloth-1763-1249' => 'cloth-01.png', 
    'skin-167-59' => "skin-01.png", 
    'cheek-2247-1193' => 'cheek-09.png', 
    'hair-167-59' => 'hair-07.png' 
); 
foreach ($items as $i => $item) { 
    $src = imagecreatefrompng($item); 
    imagealphablending($src, true); 
    imagesavealpha($src, true); 
    imagecolortransparent($src, 2130706432); 
    $src_x = imagesx($src); 
    $src_y = imagesy($src); 
    $list = explode('-', $i); 
    //var_dump($list); 
    imagecopymerge($dest, $src, intval($list[1]), intval($list[2]), 0, 0, $src_x, $src_y, 100); 
    imagealphablending($dest, true); 
    imagesavealpha($dest, true); 
} 
header('Content-Type: image/png'); 
imagepng($dest); 

回答

3

您需要啓用alpha混合和imagecopymerge從未被設計尊重alpha通道(人試圖解決這個問題早在2009年,但PHP開發者忽略了這一點,並說,這是預期的行爲)。根據PHP文檔評論部分,這應該工作。

<?php 
    /** 
    * PNG ALPHA CHANNEL SUPPORT for imagecopymerge(); 
    * by Sina Salek 
    * 
    * Bugfix by Ralph Voigt (bug which causes it 
    * to work only for $src_x = $src_y = 0. 
    * Also, inverting opacity is not necessary.) 
    * 08-JAN-2011 
    * 
    **/ 
    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); 
    } 
    // ...Do your previous stuff... 
    imagecopymerge_alpha(....); 

    // do the other stuff 

    /* Output image to browser */ 
    header('Content-type: image/png'); 
    imagepng($imgPng); 
?> 
+0

tnx爲您的答案,它只是工作,但我有問題,但我更新了問題。 – zhilevan 2014-12-08 06:04:04

+0

你知道爲什麼嗎? – zhilevan 2014-12-15 14:28:42

+0

編輯我的代碼的功能,可能會幫助 – jabbink 2014-12-15 23:01:37

5

只需使用imagecopy功能。您可以這樣做,因爲您的圖像內置透明度,並且不需要功能提供的「pct」參數。

您的代碼,簡化了:

<?php 
$image = imagecreatetruecolor(600, 600); 
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255)); 
$items = array(
    'layer1' => 'test-1.png', 
    'layer2' => 'test-2.png', 
    'layer3' => 'test-3.png', 
); 
foreach ($items as $i => $item) { 
    $src = imagecreatefrompng($item); 
    $srcw = imagesx($src); 
    $srch = imagesy($src); 
    imagecopy($image, $src, 0, 0, 0, 0, $srcw, $srch); 
} 
header('Content-Type: image/png'); 
imagepng($image); 

輸出PNG圖像:

Output PNG

在上述實施例中使用(含量爲50%透明和背景是三個PNG圖像透明,不是白色):

test-1.png test-2.png test-3.png