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

Cas*_*ynn 5 php png gd alphablending

我正在尝试通过 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();
Run Code Online (Sandbox Code Playgroud)

这会产生如下图像: 在此输入图像描述

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

8xx*_*xx8 5

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

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