php產生文字png圖片的函數

WBOY
發布: 2016-07-25 08:56:08
原創
894 人瀏覽過
  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 < 0) {
  29. $font_height = abs($bounds[7]-$bounds[1]);
  30. } else if ($this->rot > 0) {
  31. $font_height = abs($bounds[1]-$bounds[7]);
  32. } else {
  33. $font_height = abs($bounds[7]-$bounds[1]);
  34. }
  35. // 确定边框高度.
  36. $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
  37. if ($this->rot < 0) {
  38. $width = abs($bounds[4]-$bounds[0]);
  39. $height = abs($bounds[3]-$bounds[7]);
  40. $offset_y = $font_height;
  41. $offset_x = 0;
  42. } else if ($this->rot > 0) {
  43. $width = abs($bounds[2]-$bounds[6]);
  44. $height = abs($bounds[1]-$bounds[5]);
  45. $offset_y = abs($bounds[7]-$bounds[5])+$font_height;
  46. $offset_x = abs($bounds[0]-$bounds[6]);
  47. } else {
  48. $width = abs($bounds[4]-$bounds[6]);
  49. $height = abs($bounds[7]-$bounds[1]);
  50. $offset_y = $font_height;;
  51. $offset_x = 0;
  52. }
  53. $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
  54. $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
  55. $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
  56. if ($this->transparent) ImageColorTransparent($image, $background);
  57. ImageInterlace($image, false);
  58. // 画图.
  59. ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
  60. // 输出为png格式.
  61. imagePNG($image);
  62. }
  63. }
  64. $text = new textPNG;
  65. if (isset($msg)) $text->msg = $msg; // 需要显示的文字
  66. if (isset($font)) $text->font = $font; // 字体
  67. if (isset($size)) $text->size = $size; // 文字大小
  68. if (isset($rot)) $text->rot = $rot; // 旋转角度
  69. if (isset($pad)) $text->pad = $pad; // padding
  70. if (isset($red)) $text->red = $red; // 文字颜色
  71. if (isset($grn)) $text->grn = $grn; // ..
  72. if (isset($blu)) $text->blu = $blu; // ..
  73. if (isset($bg_red)) $text->bg_red = $bg_red; // 背景颜色.
  74. if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
  75. if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
  76. if (isset($tr)) $text->transparent = $tr; // 透明度 (boolean).
  77. $text->draw();
  78. ?>
复制代码

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



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!