php生成圆角图片

这篇博客介绍如何用PHP结合GD库生成带有圆角的图片。首先创建一个正方形图像,然后通过imagecreatetruecolor()、imagecolorallocate()等函数设置背景和前景色,使用imagefilledarc()绘制圆角,并将圆角颜色设为透明。最后通过imagecopymerge()合并四个角的圆角图像,完成圆角图片的制作。示例代码详细展示了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
	/** 圆角
	$radius	 = 100;
	$img	 = imagecreatetruecolor($radius, $radius);	// 创建一个正方形的图像
	$bgcolor	= imagecolorallocate($img, 223, 0, 0);	 // 图像的背景
	$fgcolor	= imagecolorallocate($img, 0, 0, 0);
	imagefill($img, 0, 0, $bgcolor);
	// $radius,$radius:以图像的右下角开始画弧
	// $radius*2, $radius*2:已宽度、高度画弧
	// 180, 270:指定了角度的起始和结束点
	// fgcolor:指定颜色
	imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);
	// 将弧角图片的颜色设置为透明
	imagecolortransparent($img, $fgcolor);
	// 变换角度
	// $img	= imagerotate($img, 90, 0);
	// $img	= imagerotate($img, 180, 0);
	// $img	= imagerotate($img, 270, 0);
	header('Content-Type: image/png');
	imagepng($img);
	**/
	
	function get_lt_rounder_corner($radius) {
		$img	 = imagecreatetruecolor($radius, $radius);	// 创建一个正方形的图像
		$bgcolor	= imagecolorallocate($img, 223, 0, 0);	 // 图像的背景
		$fgcolor	= imagecolorallocate($img, 0, 0, 0);
		imagefill($img, 0, 0, $bgcolor);
		// $radius,$radius:以图像的右下角开始画弧
		// $radius*2, $radius*2:已宽度、高度画弧
		// 180, 270:指定了角度的起始和结束点
		// fgcolor:指定颜色
		imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);
		// 将弧角图片的颜色设置为透明
		imagecolortransparent($img, $fgcolor);
		// 变换角度
		// $img	= imagerotate($img, 90, 0);
		// $img	= imagerotate($img, 180, 0);
		// $img	= imagerotate($img, 270, 0);
		// header('Content-Type: image/png');
		// imagepng($img);
		return $img;
	}

	$image_width	= 300;
	$image_height	= 300;
	$resource	 = imagecreatetruecolor($image_width, $image_height);	// 创建一个正方形的图像
	$bgcolor	 = imagecolorallocate($resource, 223, 223, 0);	 // 图像的背景
	imagefill($resource, 0, 0, $bgcolor);

	// 圆角处理
	$radius	 = 30;
	// lt(左上角)
	$lt_corner	= get_lt_rounder_corner($radius);
	imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
	// lb(左下角)
	$lb_corner	= imagerotate($lt_corner, 90, 0);
	imagecopymerge($resource, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100);
	// rb(右上角)
	$rb_corner	= imagerotate($lt_corner, 180, 0);
	imagecopymerge($resource, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);
	// rt(右下角)
	$rt_corner	= imagerotate($lt_corner, 270, 0);
	imagecopymerge($resource, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100);

	header('Content-Type: image/png');
	imagepng($resource);
	exit;
?>
其他资料
http://blog.youkuaiyun.com/travislu/article/details/4770295
http://bbs.phpchina.com/thread-61685-1-1.html
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值