欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php生成文字png图片的函数

程序员文章站 2022-05-19 23:05:53
...
  1. /*
  2. php生成文字png图片,调用方式:
  3. http://www.yourdomian.com/text_png.php3?msg=helloworld+class&rot=15&size=48&font=fonts/ARIAL.TTF
  4. */
  5. Header("Content-type: image/png");
  6. class textPNG {
  7. var $font = 'fonts/TIMES.TTF'; //默认字体. 相对于脚本存放目录的相对路径.
  8. var $msg = "undefined"; // 默认文字.
  9. var $size = 24;
  10. var $rot = 0; // 旋转角度.
  11. var $pad = 0; // 填充.
  12. var $transparent = 1; // 文字透明度.
  13. var $red = 0; // 在黑色背景中...
  14. var $grn = 0;
  15. var $blu = 0;
  16. var $bg_red = 255; // 将文字设置为白色.
  17. var $bg_grn = 255;
  18. var $bg_blu = 255;
  19. function draw() {
  20. $width = 0;
  21. $height = 0;
  22. $offset_x = 0;
  23. $offset_y = 0;
  24. $bounds = array();
  25. $image = "";
  26. // 确定文字高度.
  27. $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
  28. if ($this->rot $font_height = abs($bounds[7]-$bounds[1]);
  29. } else if ($this->rot > 0) {
  30. $font_height = abs($bounds[1]-$bounds[7]);
  31. } else {
  32. $font_height = abs($bounds[7]-$bounds[1]);
  33. }
  34. // 确定边框高度.
  35. $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
  36. if ($this->rot $width = abs($bounds[4]-$bounds[0]);
  37. $height = abs($bounds[3]-$bounds[7]);
  38. $offset_y = $font_height;
  39. $offset_x = 0;
  40. } else if ($this->rot > 0) {
  41. $width = abs($bounds[2]-$bounds[6]);
  42. $height = abs($bounds[1]-$bounds[5]);
  43. $offset_y = abs($bounds[7]-$bounds[5])+$font_height;
  44. $offset_x = abs($bounds[0]-$bounds[6]);
  45. } else {
  46. $width = abs($bounds[4]-$bounds[6]);
  47. $height = abs($bounds[7]-$bounds[1]);
  48. $offset_y = $font_height;;
  49. $offset_x = 0;
  50. }
  51. $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
  52. $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
  53. $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
  54. if ($this->transparent) ImageColorTransparent($image, $background);
  55. ImageInterlace($image, false);
  56. // 画图.
  57. ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
  58. // 输出为png格式.
  59. imagePNG($image);
  60. }
  61. }
  62. $text = new textPNG;
  63. if (isset($msg)) $text->msg = $msg; // 需要显示的文字
  64. if (isset($font)) $text->font = $font; // 字体
  65. if (isset($size)) $text->size = $size; // 文字大小
  66. if (isset($rot)) $text->rot = $rot; // 旋转角度
  67. if (isset($pad)) $text->pad = $pad; // padding
  68. if (isset($red)) $text->red = $red; // 文字颜色
  69. if (isset($grn)) $text->grn = $grn; // ..
  70. if (isset($blu)) $text->blu = $blu; // ..
  71. if (isset($bg_red)) $text->bg_red = $bg_red; // 背景颜色.
  72. if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
  73. if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
  74. if (isset($tr)) $text->transparent = $tr; // 透明度 (boolean).
  75. $text->draw();
  76. ?>
复制代码

更多GD库的应用实例,请参考: php中开启gd2扩展的方法介绍 php验证码(GD库生成验证码)的例子 php GD库中文乱码的解决方法 php GD库生成验证码的实例 php GD库上传图片并创建缩略图的代码 php使用GD库生成bmp格式的图片(imagebmp)