PHP 給圖片加邊框

來源:互聯網
上載者:User

標籤: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));



添加前:

添加後:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.