PHP function to generate text png images

WBOY
Release: 2016-07-25 08:56:08
Original
931 people have browsed it
  1. /*
  2. php generates text png images, calling method:
  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'; //Default font. Relative to the script storage directory Path.
  8. var $msg = "undefined"; // Default text.
  9. var $size = 24;
  10. var $rot = 0; // Rotation angle.
  11. var $pad = 0; // Padding.
  12. var $transparent = 1; // Text transparency.
  13. var $red = 0; // On a black background...
  14. var $grn = 0;
  15. var $blu = 0;
  16. var $bg_red = 255; // Set the text is white.
  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. // Determine the text height.
  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. // Determine the border height.
  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. // Draw.
  59. ImageTTFText($image , $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg) ;
  60. // Output in png format.
  61. imagePNG($image);
  62. }
  63. }
  64. $text = new textPNG;
  65. if (isset($msg)) $text->msg = $msg; / / Text to be displayed
  66. if (isset($font)) $text->font = $font; // Font
  67. if (isset($size)) $text->size = $size; // Text size
  68. if (isset($rot)) $text->rot = $rot; // Rotation angle
  69. if (isset($pad)) $text->pad = $pad; // padding
  70. if (isset( $red)) $text->red = $red; // Text color
  71. if (isset($grn)) $text->grn = $grn; // ..
  72. if (isset($blu)) $text->blu = $blu; // ..
  73. if (isset($bg_red)) $text->bg_red = $bg_red; // Background color.
  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; // Transparency (boolean).
  77. $text->draw();
  78. ?>
Copy code

For more application examples of the GD library, please refer to: Introduction to how to enable gd2 extension in php Example of php verification code (GD library generates verification code) Solution to Chinese garbled characters in php GD library Example of php GD library generating verification code php GD library code to upload images and create thumbnails php uses the GD library to generate images in bmp format (imagebmp)



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 admin@php.cn
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!