Odd transparency effect when merging two .png'

2019-09-19 06:23发布

问题:

Merging two images with transparent sections produces the following composite image:

I'm wondering why the transparent section of the image I've overlayed onto the green background shows up as such? Anyone?

$base = imagecreatefrompng('application/assets/images/vel1_bg.png');
        imagealphablending($base, false);
        imagesavealpha($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, false);
            imagesavealpha($item, true);    
            list($width, $height, $type, $attr) = getimagesize('application/assets/images/items/'. $user_board_items[$value[0]] . '.png');

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

            imagedestroy($item);
        }

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

回答1:

GD does not support transparency in 32-bit PNG. You have to use either 8-bit with one transparent 'color' or 24-bit (officially 24-bit does not support transparency, but Photoshop can do it when using 'save for web' with 24bit png).