1. 程式人生 > >php GD 圓圖 -處理成圓圖片

php GD 圓圖 -處理成圓圖片

case urn nbsp @param string share extension 如果 images

 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"); 45 imagepng(yuan_img());

php GD 圓圖 -處理成圓圖片