/**
* 给图片加边框 by liangjian 2014-06-19
* @param $ImgUrl图片地址
* @param $SavePath新图片保存路径
* @param $px边框像素
* @return Ambigous
*/
function ImageAddBoard($ImgUrl, $SavePath, $px = 2) {
$aPathInfo = pathinfo ( $ImgUrl );
// 文件名
$sFileName = $aPathInfo ['filename'];
// 图片扩展名
$sExtension = $aPathInfo ['extension'];
// 获取原图大小
$size = getimagesize ( $ImgUrl );
$img_w = $size [0];
$img_h = $size [1];
// 读取图片
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));
添加前:
添加后:
原文:http://blog.csdn.net/wsliangjian/article/details/32328345