/**
* 给图片加边框 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));
添加前:
添加后: