/**
* 将图片转为圆角图片
* @param string $image_path 图片路径,生成的圆角图片会覆盖传入的图片
* @param integer $radius 圆角曲值
* @return array 返回值 ['status' => '状态码,1-成功,0-失败', 'msg' => '返回消息', 'image_path' => '圆角图片路径']
*/
function get_radius_image($image_path, $radius = 15)
{
try {
if (empty($image_path) || !file_exists($image_path)) {
throw new Exception('图片路径为空或图片不存在');
}
$info = getimagesize($image_path);
$w = $info[0];
$h = $info[1];
switch ($info['mime']) {
case 'image/jpeg':
$src = imagecreatefromjpeg($image_path);
break;
case 'image/gif':
$src = imagecreatefromgif($image_path);
break;
case 'image/png':
$src = imagecreatefrompng($image_path);
break;
default:
// 如需其他类型可自己扩展
throw new Exception('图片类型仅支持: jpeg,gif,png');
}
$q = 10;
$radius *= $q;
do {
$r = rand(0, 255);
$g = rand(0, 255);
$b = rand(0, 255);
} while (imagecolorexact($src, $r, $g, $b) < 0);
$nw = $w * $q;
$nh = $h * $q;
$img = imagecreatetruecolor($nw, $nh);
$alphacolor = imagecolorallocatealpha($img, $r, $g, $b, 127);
imagealphablending($img, false);
imagesavealpha($img, true);
imagefilledrectangle($img, 0, 0, $nw, $nh, $alphacolor);
imagefill($img, 0, 0, $alphacolor);
imagecopyresampled($img, $src, 0, 0, 0, 0, $nw, $nh, $w, $h);
imagearc($img, $radius - 1, $radius - 1, $radius * 2, $radius * 2, 180, 270, $alphacolor);
imagefilltoborder($img, 0, 0, $alphacolor, $alphacolor);
imagearc($img, $nw - $radius, $radius - 1, $radius * 2, $radius * 2, 270, 0, $alphacolor);
imagefilltoborder($img, $nw - 1, 0, $alphacolor, $alphacolor);
imagearc($img, $radius - 1, $nh - $radius, $radius * 2, $radius * 2, 90, 180, $alphacolor);
imagefilltoborder($img, 0, $nh - 1, $alphacolor, $alphacolor);
imagearc($img, $nw - $radius, $nh - $radius, $radius * 2, $radius * 2, 0, 90, $alphacolor);
imagefilltoborder($img, $nw - 1, $nh - 1, $alphacolor, $alphacolor);
imagealphablending($img, true);
imagecolortransparent($img, $alphacolor);
$dest = imagecreatetruecolor($w, $h);
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagefilledrectangle($dest, 0, 0, $w, $h, $alphacolor);
imagecopyresampled($dest, $img, 0, 0, 0, 0, $w, $h, $nw, $nh);
imagedestroy($src);
imagedestroy($img);
imagepng($dest, $image_path);
imagedestroy($dest);
return ['status' => 1, 'msg' => 'success', 'image_path' => $image_path];
} catch (\Exception $e) {
return ['status' => 0, 'msg' => $e->getMessage()];
}
}生成后的图片(现在不能上传图片了吗 - - ):最佳答案