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);
}

 

展开阅读全文

没有更多推荐了,返回首页