php圖片背景填充執行個體

來源:互聯網
上載者:User
  1. /**

  2. * 添加背景
  3. * @param string $src 圖片路徑
  4. * @param int $w 背景映像寬度
  5. * @param int $h 背景映像高度
  6. * @return 返回加上背景的圖片
  7. * **/
  8. public function addBg($src,$w,$h)
  9. {
  10. $bg = imagecreatetruecolor($w,$h);
  11. $white = imagecolorallocate($bg,255,255,255);
  12. imagefill($bg,0,0,$white);//填充背景

  13. //擷取靶心圖表片資訊

  14. $info=getimagesize($src);
  15. $width=$info[0];//靶心圖表片寬度
  16. $height=$info[1];//靶心圖表片高度
  17. switch ($info[2]){
  18. case 1:
  19. $img = imagecreatefromgif($src);
  20. break;
  21. case 2:
  22. $img = imagecreatefromjpeg($src);
  23. break;
  24. case 3:
  25. $img = imagecreatefrompng($src);
  26. break;
  27. default:
  28. exit('不支援的映像格式');
  29. break;
  30. }
  31. if($height < $h)
  32. {
  33. $x=0;
  34. $y=($h-$height)/2;//垂直置中
  35. }
  36. if($width < $w)
  37. {
  38. $x=($w-$width)/2;//水平置中
  39. $y=0;
  40. }
  41. if($height < $h && $width < $w){
  42. $x = ($w-$width)/2;
  43. $y = ($h-$height)/2;
  44. }
  45. imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
  46. imagejpeg($bg,$src,100);
  47. imagedestroy($bg);
  48. imagedestroy($img);
  49. return $src;
  50. }

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.