搜索
系统检测到您的用户名不符合规范:

分享一个生成圆角图片的函数

浏览:779 发布日期:2020年03月26日 分类:技术分享 关键字: 图片圆角 图片
这个方法比较独立,不依赖TP的类,复制后可以直接使用。/**
* 将图片转为圆角图片
* @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()];
}
}
生成后的图片(现在不能上传图片了吗 - - ):

最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
sym_lu
积分:799 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /