/**
* 把图片转换成圆角
* @param string $imgpath
* @param int $radius
* @return resource
*/
public function radius_img_url($imgpath = './t.png', $radius = 15) {
$ext = pathinfo($imgpath);
$src_img = null;
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefrompng($imgpath);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
return $this->radius_img($src_img, $w,$h, $radius);
}
public function radius_img($src_img, $width,$height, $radius = 15) {
$w = &$width;
$h = &$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($src_img, $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);
}
//上右
$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);
}
//下左
$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);
}
//下右
$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);
}
}
}
}
return $img;
}
直接在背景图片上面画圆
/**
* 直接把圆角图片画在背景图片上
* @param $bg_img 背景图片
* @param $x 背景图片位置
* @param $y 背景图片位置
* @param $src_img 要画的圆角的图片
* @param $width 圆角图片的尺寸
* @param $height 圆角图片的尺寸
* @param int $radius 圆角尺寸
* @return resource
*/
public static function radius_img_bg($bg_img, $x, $y, $src_img, $width,$height, $radius = 15) {
$ob_x = $x;
$ob_y = $y;
$max_x = $ob_x + $width;
$max_y = $ob_y + $height;
$start_y = $y;
// $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
$r = $radius; //圆 角半径
for ($x; $x < $max_x; $x++) {
for ($y=$start_y; $y < $max_y; $y++) {
$rgbColor = imagecolorat($src_img, $x-$ob_x, $y-$ob_y);
if (
($x >= $ob_x + $radius && $x <= ($max_x - $radius)) ||
($y >= $ob_y + $radius && $y <= ($max_y - $radius))
) {
//不在四角的范围内,直接画
imagesetpixel($bg_img, $x, $y, $rgbColor);
} else {
//在四角的范围内选择画
//上左
$y_x = $ob_x + $r; //圆心X坐标
$y_y = $ob_y + $r; //圆心Y坐标
if ((pow($x - $y_x, 2) + pow($y - $y_y, 2) <= pow($r,2))) {
imagesetpixel($bg_img, $x, $y, $rgbColor);
}
//上右
$y_x = $max_x - $r; //圆心X坐标
$y_y = $ob_y + $r; //圆心Y坐标
if ((pow($x - $y_x, 2) + pow($y -$y_y, 2) <= pow($r,2))) {
imagesetpixel($bg_img, $x, $y, $rgbColor);
}
//下左
$y_x = $ob_x + $r; //圆心X坐标
$y_y = $max_y - $r; //圆心Y坐标
if (pow($x - $y_x,2) + pow($y - $y_y,2) <= pow($r, 2)) {
imagesetpixel($bg_img, $x, $y, $rgbColor);
}
//下右
$y_x = $max_x - $r; //圆心X坐标
$y_y = $max_y - $r; //圆心Y坐标
if (pow($x - $y_x,2) + pow($y - $y_y,2) <= pow($r, 2)) {
imagesetpixel($bg_img, $x, $y, $rgbColor);
}
}
}
}
return $bg_img;
}