php截取圆角
余温
2017年11月28日 22时36分
php
compose 安装的tp5 img扩展 在vendor/topthink/think-image/ 目录下 其他扩展应该也差不多
tp5截取圆角图片直接在 Image类里面加个方法就OK了 tp3应该同理
/** * 处理圆角图片 * @param integer $radius 圆角半径长度默认为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); //拾取一个完全透明的颜色,最后一个参数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; } //用的时候直接这样调用就好了 $image = \think\Image::open('./1501425628.png')->thumb(500, 500)->radius(50)->save('./6.png');
或者直接用这个方法也可以 在本地测试OK 服务器上老出不来也不知道什么原因 以后慢慢测试
/** * blog:http://www.zhaokeli.com * 处理圆角图片 * @param string $imgpath 源图片路径 * @param integer $radius 圆角半径长度默认为15,处理成圆型 * @return [type] [description] */ function radius_img($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]; // $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; } header("content-type:image/png"); $imgg = radius_img('./1.png', ); imagepng($imgg); imagedestroy($imgg);
睡觉走起了
请登陆后评论
{{vo.time}} 回复