Heim  >  Artikel  >  Backend-Entwicklung  >  php为图片增加背景实例代码

php为图片增加背景实例代码

WBOY
WBOYOriginal
2016-07-25 08:53:423850Durchsuche
  1. function overlayjpg($imgsrc,$imgdst,$width,$height="")

  2. {
  3. //$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
  4. //取得图片的宽度,高度值
  5. $arr = getimagesize($imgsrc);
  6. //计算图片X轴位置
  7. $img_X = ($width - $arr[0])/2;
  8. if($height == ""){
  9. $heights = $arr[1];
  10. $img_Y = 0;
  11. }
  12. else{
  13. if($height $heights = $arr[1];
  14. $img_Y = 0;
  15. }
  16. else{
  17. $heights = $height;
  18. $img_Y = ($height - $arr[1])/2;
  19. }
  20. }
  21. //$w = $arr[0];

  22. //$h = $arr[1];
  23. // Create image and define colors
  24. $image = imagecreatetruecolor($width,$heights); //创建一个彩色的底图
  25. $bg = imagecolorallocate($image, 255, 255, 255);
  26. imagefill($image,0,0,$bg);
  27. $imgsrc = LoadIMG($imgsrc,$arr['mime']);
  28. imagecopy($image,$imgsrc,$img_X,$img_Y,0,0,$arr[0],$arr[1]);
  29. imagejpeg($image,$imgdst,90);
  30. //imagedestroy($image);
  31. }
  32. // 加载背景图片
  33. function LoadIMG($imgname,$mime)
  34. {
  35. if($mime == "image/gif"){
  36. $im = @imagecreatefromgif($imgname); /* Attempt to open */
  37. }
  38. elseif ($mime == "image/png"){
  39. $im = @imagecreatefrompng($imgname); /* Attempt to open */
  40. }
  41. else{
  42. $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
  43. }
  44. if(!$im) { /* See if it failed */
  45. $im = imagecreatetruecolor(150, 30); /* Create a blank image */
  46. $bgc = imagecolorallocate($im, 255, 255, 255);
  47. $tc = imagecolorallocate($im, 0, 0, 0);
  48. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  49. /* Output an errmsg */
  50. imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
  51. }
  52. return $im;
  53. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn