2014-12-07 17 views
2

부분적으로 투명한 이미지를 제대로 작동하지 않습니다imagecopymerge는 불투명 내가 가지고있는 이미지가

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

당신은 알파 블렌딩을 활성화해야하고 imagecopymerge은 알파 채널을 존중하도록 설계되지 않았다 (사람들은 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

+0

이유를 알고 계십니까? – zhilevan

+0

내 코드를 편집 할 수있는 기능이있는 경우 – jabbink

5

그냥 imagecopy 기능을 사용하십시오. 이미지가 투명하게 만들어 졌기 때문에 imagecopymerge 기능이 제공하는 "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 이미지 (함량이 50 % 투명 백그라운드 인 상기 예에서 사용

Output PNG

세 PNG 이미지 투명하지 흰색) :

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