Maison  >  Article  >  développement back-end  >  php图片背景填充实例

php图片背景填充实例

WBOY
WBOYoriginal
2016-07-25 08:53:441824parcourir
  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. }
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:php汉字unicode编码与解码 Article suivant:php面试题及答案分享