TP5 GD处理 头像处理圆形

function imagecircle($src, $dst)
{
    
    
    //获取原图尺寸,并设置新图片的宽度和高度
    //$src 原路径  $dst 保存路径
    list($w, $h) = getimagesize($src);
    if ($w > $h) {
    
    
        $w = $h;
    } else {
    
    
        $h = $w;
    }
    $oimgSrc = imagecreatefromstring(file_get_contents($src));
    $oimgDst = imagecreatetruecolor($w, $h);
    imagealphablending($oimgDst, false);
    $transparent = imagecolorallocatealpha($oimgDst, 0, 0, 0, 127);
    $r = $w / 2;
    for ($x = 0; $x < $w; $x++) {
    
    
        for ($y = 0; $y < $h; $y++) {
    
    
            $c = imagecolorat($oimgSrc, $x, $y);
            $_x = $x - $w / 2;
            $_y = $y - $h / 2;
            if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {
    
    
                imagesetpixel($oimgDst, $x, $y, $c);
            } else {
    
    
                imagesetpixel($oimgDst, $x, $y, $transparent);
            }
        }
    }
    imagesavealpha($oimgDst, true);
    imagepng($oimgDst, $dst);
    imagedestroy($oimgDst);
    imagedestroy($oimgSrc);
}

猜你喜欢

转载自blog.csdn.net/weixin_43784997/article/details/115235405