php gd库怎么把一个图片裁剪成圆形的

微信的头像现在要加水印到一张图片上 弄成 圆形的 怎么裁剪呢 
Imagick提供的有 gd库 怎么做呢

因为要在微信中生成这个图片发送给用户 所以必须是生成这样的图片的 css的border-radius是只能显示网页中 怎么生成一个图片呢

已找到方法

function resize_img($url,$path='./'){
    $imgname = $path.uniqid().'.jpg';
    $file = $url;
    list($width, $height) = getimagesize($file); //获取原图尺寸
    $percent = (110/$width);
    //缩放尺寸
    $newwidth = $width * $percent;
    $newheight = $height * $percent;
    $src_im = imagecreatefromjpeg($file);
    $dst_im = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    imagejpeg($dst_im, $imgname); //输出压缩后的图片
    imagedestroy($dst_im);
    imagedestroy($src_im);
    return $imgname;
}

function test($url,$path='./'){  
        $w = 132;  $h=132; // original size  微信默认头像大小 高132,宽132
        $original_path= $url;  
        $dest_path = $path.uniqid().'.png';  
        $src = imagecreatefromstring(file_get_contents($original_path));  
        $newpic = imagecreatetruecolor($w,$h);  
        imagealphablending($newpic,false);  
        $transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);  
        $r=$w/2;  
        for($x=0;$x<$w;$x++)  
            for($y=0;$y<$h;$y++){  
                $c = imagecolorat($src,$x,$y);  
                $_x = $x - $w/2;  
                $_y = $y - $h/2;  
                if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){  
                    imagesetpixel($newpic,$x,$y,$c);  
                }else{  
                    imagesetpixel($newpic,$x,$y,$transparent);  
                }  
            }  
        imagesavealpha($newpic, true);  
        imagepng($newpic, $dest_path);  
        imagedestroy($newpic);  
        imagedestroy($src);  
       // unlink($url);  
        return $dest_path;  
    }  

 

 

 

 

 

 

 

 

 转载:https://segmentfault.com/q/1010000007501202

posted @ 2020-07-09 10:46  搬砖小伙子  阅读(1079)  评论(0编辑  收藏  举报