PHP把png转成透明png

function createPng($png, $basepng){
    $picture = imagecreatefrompng($png);
    $img_w = imagesx($picture);
    $img_h = imagesy($picture);
    $newPicture = imagecreatetruecolor( $img_w, $img_h );
    imagesavealpha( $newPicture, true );
    $rgb = imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 );
    imagefill( $newPicture, 0, 0, $rgb );

    $color = imagecolorat( $picture, $img_w-1, 1);

    for( $x = 0; $x < $img_w; $x++ ) {
        for( $y = 0; $y < $img_h; $y++ ) {
            $c = imagecolorat( $picture, $x, $y );
            if($color!=$c){
                imagesetpixel( $newPicture, $x, $y, $c);
            }
        }
    }

    imagepng($newPicture, $basepng);
    imagedestroy($newPicture);
    imagedestroy($picture);
}

猜你喜欢

转载自blog.csdn.net/glfxml/article/details/82787297
PNG
今日推荐