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);

 

 

如果本文章已幫助到您!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM