Transparent Circle Cropped Image with PHP

后端 未结 3 1685
醉话见心
醉话见心 2020-12-17 05:03

I want to crop a circle image using PHP but it seems that my new image have some transparent pixels. Of course, I want ONLY the outside area of the ellipse to have backgroun

3条回答
  •  天涯浪人
    2020-12-17 05:36

    I couln't get it done with Alain's code either. After some time understanding what each line of code does, here is my fix..

        //this creates a pink rectangle of the same size
        $mask = imagecreatetruecolor($imgwidth, $imgheight);
        $pink = imagecolorallocate($mask, 255, 0, 255);
        imagefill($mask, 0, 0, $pink);
        //this cuts a hole in the middle of the pink mask
        $black = imagecolorallocate($mask, 0, 0, 0);
        imagecolortransparent($mask, $black);
        imagefilledellipse($mask, $imgwidth/2, $imgheight/2, $imgwidth, $imgheight, $black);
        //this merges the mask over the pic and makes the pink corners transparent
        imagecopymerge($img, $mask, 0, 0, 0, 0, $imgheight, $imgheight);
        imagecolortransparent($img, $pink);
        imagepng($img, "my_circle.png");
    

提交回复
热议问题