php图片背景填充实例

WBOY
Release: 2016-07-25 08:53:44
Original
1733 people have browsed it
  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 {
  32. $x=0;
  33. $y=($h-$height)/2;//垂直居中
  34. }
  35. if($width {
  36. $x=($w-$width)/2;//水平居中
  37. $y=0;
  38. }
  39. if($height $x = ($w-$width)/2;
  40. $y = ($h-$height)/2;
  41. }
  42. imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
  43. imagejpeg($bg,$src,100);
  44. imagedestroy($bg);
  45. imagedestroy($img);
  46. return $src;
  47. }
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!