/**
* 處理圓角圖片
* @param integer $radius 圓角半徑長(zhǎng)度默認(rèn)為15,處理成圓型
* @return [type] [description]
*/
public function radius($radius = 15) {
// $wh = getimagesize($imgpath);
$w = $this->width();
$h = $this->height();
// $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
$img = imagecreatetruecolor($w, $h);
//這一句一定要有
imagesavealpha($img, true);
//拾取一個(gè)完全透明的顏色,最后一個(gè)參數(shù)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))) {
//不在四角的范圍內(nèi),直接畫(huà)
imagesetpixel($img, $x, $y, $rgbColor);
} else {
//在四角的范圍內(nèi)選擇畫(huà)
//上左
$y_x = $r; //圓心X坐標(biāo)
$y_y = $r; //圓心Y坐標(biāo)
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坐標(biāo)
$y_y = $r; //圓心Y坐標(biāo)
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坐標(biāo)
$y_y = $h - $r; //圓心Y坐標(biāo)
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坐標(biāo)
$y_y = $h - $r; //圓心Y坐標(biāo)
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;
}
放到使用TP5開(kāi)發(fā)手冊(cè)雜項(xiàng)中圖像處理插件
使用方法:
Image::open('./logo.png')->thumb(100,100,\think\Image::THUMB_SOUTHEAST)->radius(20)->save('./logo.png','png');
注意:save保存時(shí)一定要加第二個(gè)參數(shù),否則不是透明的
附:獲取url大圖片處理方法
$ch = curl_init($imgUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$logoImg = curl_exec($ch);