• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP把图片转换成圆形png(头像处理)

php 海叔叔 4年前 (2021-05-31) 33次浏览 已收录 0个评论

关键词
PHP 图片 圆形

/* 
 * @fun 图片转换成圆形png,传入源路径和转换后的路径,均用相对于当前程序文件的路径
 * @memo 对于非正方形的图片,以短边作为图片的直径
 * @param string $src 源路径
 * @param string $dst 转换后的路径
 * @return void
 * @call z_image2circle("circleimage.jpg", './circleimage-'.uniqid().'.png');
 */
function z_image2circle($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);
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP把图片转换成圆形png(头像处理)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址