標籤:des class blog code http ext
/** * 給圖片加邊框 by liangjian 2014-06-19 * @param $ImgUrl圖片地址 * @param $SavePath新圖片儲存路徑 * @param $px邊框像素 * @return Ambigous <boolean, 新圖片的路徑> */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圖片放到黑色背景圖片上,邊框是1pximagecopy ( $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));
添加前:
添加後: