php GD 圆图 -处理成圆图片

 1 <?php
 2  /**
 3  * 处理成圆图片,如果图片不是正方形就取最小边的圆半径,从左边开始剪切成圆形
 4  * @param  string $imgpath [description]
 5  * @return [type]          [description]
 6  */
 7     function yuan_img($imgpath = './code_png/share.jpg') {
 8         $ext     = pathinfo($imgpath);
 9         $src_img = null;
10         switch ($ext['extension']) {
11             case 'jpg':
12                 $src_img = imagecreatefromjpeg($imgpath);
13                 break;
14             case 'png':
15                 $src_img = imagecreatefrompng($imgpath);
16                 break;
17         }
18         $wh  = getimagesize($imgpath);
19         $w   = $wh[0];
20         $h   = $wh[1];
21         $w   = min($w, $h);
22         $h   = $w;
23         $img = imagecreatetruecolor($w, $h);
24         //这一句一定要有
25         imagesavealpha($img, true);
26         //拾取一个完全透明的颜色,最后一个参数127为全透明
27         $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
28         imagefill($img, 0, 0, $bg);
29         $r   = $w / 2; //圆半径
30         $y_x = $r; //圆心X坐标
31         $y_y = $r; //圆心Y坐标
32         for ($x = 0; $x < $w; $x++) {
33             for ($y = 0; $y < $h; $y++) {
34                 $rgbColor = imagecolorat($src_img, $x, $y);
35                 if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
36                     imagesetpixel($img, $x, $y, $rgbColor);
37                 }
38             }
39         }
40 
41         return $img;
42 }
43 
44 Header("Content-Type: image/png");
$img =
yuan_img();

45 imagepng($img);

46 imagedestroy($img);

如果本文章已帮助到您!

原文地址:https://www.cnblogs.com/handle/p/9529576.html