php 把图片变成圆形,边框透明, 头像改成圆形

比如头像是方形的

 

 

我要一个圆形的  边框透明的

(我这个是截图的.png   实际是透明边框)

 

 

代码如下

function circular_img($imgurl,$dest_path){
        //第一个参数为网络图片 或者改一下为本地图片

            // $src = imagecreatefromstring(file_get_contents('http://xiaohe520.club/Public/Home/default/images/qr.png')); //获取网络资源文件
    
    
        //本地图片改一下参数
            // $ext=pathinfo($img);
            // $src = null;
            // switch ($ext['extension']) {
            //     case 'jpg':
            //         $src=imagecreatefromjpeg($imgurl);
            //         break;
            //     case 'png':
            //         $src=imagecreatefrompng($img);
            //         break;
            // }

            $src = imagecreatefromstring(file_get_contents($img)); //获取网络资源文件
            $wh= getimagesize($img);
            $w=$wh[0];
            $h=$wh[1];
            $w=min($w,$h);
            $h= $w;
    
            $newpic = imagecreatetruecolor($w,$h); 
            //建立的是一幅大小为 x和 y的黑色图像(默认为黑色),如想改变背景颜色则需要用填充颜色函数imagefill($img,0,0,$color);
            // $img = imagecreatetruecolor(100,100);    //创建真彩图像资源
            // $color = imagecolorAllocate($img,200,200,200);   //分配一个灰色
            // imagefill($img,0,0,$color);
    
    
            // 启用混色模式
            imagealphablending($newpic,false); //设定图像的混色模式
    
            //imagealphablending() 允许在真彩色图像上使用两种不同的绘画模式。
            // 在混色(blending)模式下,alpha 通道色彩成分提供给所有的绘画函数,例如 imagesetpixel() 决定底层的颜色应在何种程度上被允许照射透过。作为结果,GD 自动将该点现有的颜色和画笔颜色混合,并将结果储存在图像中。结果的像素是不透明的。
            // 在非混色模式下,画笔颜色连同其 alpha 通道信息一起被拷贝,替换掉目标像素。混色模式在画调色板图像时不可用。
            // 如果 blendmode 为 TRUE,则启用混色模式,否则关闭。成功时返回 TRUE, 或者在失败时返回 FALSE。
    
    
            $transparent = imagecolorallocatealpha($newpic, 255, 255, 255, 127);//边缘透明
    
            //imagecolorallocatealpha(resource $image , int $red , int $green , int $blue, int $alpha )
            // $image 图像资源,通过创造的图像功能,如,一返回imagecreatetruecolor()。
            // $red 红色分量的价值。
            // $green 价值的绿色成分。
            // $blue 蓝色成分的价值。
            // $alpha 一个介于0和127的价值。 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);
                        //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
                    }
                }
    
            //imagesavealpha() 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)
            imagesavealpha($newpic, true);
            // header('Content-Type: image/png');
            imagepng($newpic, $dest_path);
            imagedestroy($newpic);
            imagedestroy($src);
    
            // unlink() 函数删除文件。
            // 若成功,则返回 true,失败则返回 false。
            // unlink($url);
    
            return $dest_path;
}

echo
circular_img('http://xiaohe520.club/Public/Home/default/images/qr.png','QQ496631085.png');

本地图片取消注释改一下参数   

 

 

posted @ 2020-04-08 10:43  xiaohe520  阅读(1253)  评论(0编辑  收藏  举报