PHP 给图片加边框

/**
 * 给图片加边框 by liangjian 2014-06-19
 * @param $ImgUrl	图片地址
 * @param $SavePath	新图片保存路径
 * @param $px	边框像素(2表示左右各一像素)
 * @return Ambigous <boolean, 新图片的路径>
 */
function ImageAddBoard($ImgUrl, $SavePath, $px = 2) {
	$aPathInfo = pathinfo ( $ImgUrl );
	// 文件名
	$sFileName = $aPathInfo ['filename'];
	// 图片扩展名
	$sExtension = $aPathInfo ['extension'];
	// 获取原图大小
	list($img_w, $img_h) = getimagesize ( $ImgUrl );
	
	// 读取图片
	if (strtolower ( $sExtension ) == 'png') {
		$resource = imagecreatefrompng ( $ImgUrl );
	} elseif (strtolower ( $sExtension ) == 'jpg' || strtolower ( $sExtension ) == 'jpeg') {
		$resource = imagecreatefromjpeg ( $ImgUrl );
	}
	
	// 282*282的黑色背景图片
	$im = @imagecreatetruecolor ( ($img_w + $px), ($img_h + $px) ) or die ( "Cannot Initialize new GD image stream" );
	
	// 为真彩色画布创建背景,再设置为透明
	$color = imagecolorallocate ( $im, 0, 0, 0 );
	//imagefill ( $im, 0, 0, $color );
	//imageColorTransparent ( $im, $color );
	
	// 把品牌LOGO图片放到黑色背景图片上,边框是1px
	imagecopy ( $im, $resource, $px / 2, $px / 2, 0, 0, $size [0], $size [1] );
	
	$imgNewUrl = $SavePath . $sFileName . '-n.' . $sExtension;
	if (strtolower ( $sExtension ) == 'png') {
		$ret = imagepng ( $im, $imgNewUrl );
	} elseif (strtolower ( $sExtension ) == 'jpg' || strtolower ( $sExtension ) == 'jpeg') {
		$ret = imagejpeg ( $im, $imgNewUrl );
	}
	imagedestroy ( $im );
	return $ret ? $imgNewUrl : false;
}

使用:

$savePath = './brand/';
$url = 'http://cdn0.xx.cn/store/moudlepic/301_module_images/936001_z.jpg';

var_dump(ImageAddBoard($url, $savePath));
 
 
 

添加前:

添加后:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值