TP圖片處理類新增圖片橢圓處理
阿新 • • 發佈:2019-08-05
/** * 處理圓角圖片 * @param integer $radius 圓角半徑長度預設為50,處理成圓型 */ public function radius($radius = 50) { // $wh = getimagesize($imgpath); $w = $this->width(); $h = $this->height(); // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius; $img = imagecreatetruecolor($w, $h); //這一句一定要有 imagesavealpha($img, true); //拾取一個完全透明的顏色,最後一個引數127為全透明 $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); imagefill($img, 0, 0, $bg); $r = $radius; //圓 角半徑 for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $rgbColor = imagecolorat($this->im, $x, $y); if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) { //不在四角的範圍內,直接畫 imagesetpixel($img, $x, $y, $rgbColor); } else { //在四角的範圍內選擇畫 //上左 $y_x = $r; //圓心X座標 $y_y = $r; //圓心Y座標 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { imagesetpixel($img, $x, $y, $rgbColor); continue; } //上右 $y_x = $w - $r; //圓心X座標 $y_y = $r; //圓心Y座標 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { imagesetpixel($img, $x, $y, $rgbColor); continue; } //下左 $y_x = $r; //圓心X座標 $y_y = $h - $r; //圓心Y座標 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { imagesetpixel($img, $x, $y, $rgbColor); continue; } //下右 $y_x = $w - $r; //圓心X座標 $y_y = $h - $r; //圓心Y座標 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) { imagesetpixel($img, $x, $y, $rgbColor); continue; } } } } $this->im = $img; return $this; }
將以下程式碼拷貝至圖片