• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

微信的头像现在要加水印到一张图片上 弄成 圆形的 怎么裁剪呢 
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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php-fetion开源PHP飞信发送类发布时间:2022-07-10
下一篇:
7 种流行 PHP IDE 的比较发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap