Yus*_*sef 2 php png transparency gd alpha-transparency
我有一个不透明的图像:

当我尝试加载它时:
$img = imagecreatefrompng('cheek-01.png');
header('Content-Type: image/png');
imagepng($img);
Run Code Online (Sandbox Code Playgroud)
它会变成:

我想添加(合并)此图像与另一个图像(我知道如何合并两个png图像).
问题是什么?如何在GD不丢失不透明度的情况下将其加载到库中?
更新
我的目标是合并一些图像,其中一些图像具有alpha(不同的不透明度,如上图),但是当我合并它们时,alpha信息会丢失.
<?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);
Run Code Online (Sandbox Code Playgroud)
您需要启用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);
?>
Run Code Online (Sandbox Code Playgroud)
只需使用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);
Run Code Online (Sandbox Code Playgroud)
输出PNG图像:

上例中使用的三个PNG图像(内容为50%透明,背景为透明,不是白色):
