PHP中合并具有透明度的两个图像

5
我将尝试使用PHP制作一个由多个具有背景透明度的PNG图像组成的复合图像,并将结果存储在数据库中。但是,当我合并这些图像时,我的问题在于它们的透明部分被丢失了。
以下是创建复合图像的代码:
    $base = imagecreatefrompng('application/assets/images/vel1_bg.png');
    imagealphablending($base, true);
    list($baseWidth, $baseHeight, $type, $attr) = getimagesize('application/assets/images/vel1_bg.png');

    $user_board_items = $this->config->item('user_board_items');

    foreach($array as $key => $value){
        $item = imagecreatefrompng('application/assets/images/items/' . $user_board_items[$value[0]] . '.png');         
        imagealphablending($item, true);
        list($width, $height, $type, $attr) = getimagesize('application/assets/images/items/'. $user_board_items[$value[0]] . '.png');

        imagecopymerge($base,
                    $item,
                    floor(($value[1] / 100) * $baseWidth),
                    floor(($value[2] / 100) * $baseHeight),
                    0,
                    0,
                    $width,
                    $height,
                    100);
        imagedestroy($item);
    }

    //We have to capture the output buffer
    ob_start();
    imagepng($base);
    $baseimg = ob_get_clean();

这将产生这样的图像: 输入图像描述 我正在寻找更像这样的东西: 输入图像描述 (注意透明部分的表示方式)

2
这可能会有所帮助:https://dev59.com/GU_Ta4cB1Zd3GeqPAWUm#3356419。我记得 imagecopymerge 可能会导致问题。尝试使用 imagecopyresampled 替代。 - Mike
2个回答

5

不要使用imagecopymerge()合并透明图像。

在您的脚本中最好使用imagecopyresampled()。


1

如之前所述,经过数小时的尝试不同解决方案后,imagecopyresampled 对我也起作用了。需要传递的参数保持不变,除了你必须删除最后一个参数,但添加源宽度和高度。您的调用可能是:

  imagecopyresampled($base,
                $item,
                floor(($value[1] / 100) * $baseWidth),
                floor(($value[2] / 100) * $baseHeight),
                0,
                0,
                $width,
                $height,
                $width,
                $height);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接